Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages when formatting telemetry #81

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions senders/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func MetricLine(name string, value float64, ts int64, source string, tags map[st

for k, v := range tags {
if v == "" {
return "", errors.New("metric point tag value cannot be blank")
return "", fmt.Errorf("tag values cannot be empty: metric=%s tag=%s", name, k)
}
sb.WriteString(" ")
sb.WriteString(strconv.Quote(sanitizeInternal(k)))
Expand All @@ -66,11 +66,11 @@ func HistoLine(name string, centroids histogram.Centroids, hgs map[histogram.Gra
}

if len(centroids) == 0 {
return "", errors.New("distribution should have at least one centroid")
return "", fmt.Errorf("distribution should have at least one centroid: histogram=%s", name)
}

if len(hgs) == 0 {
return "", errors.New("histogram granularities cannot be empty")
return "", fmt.Errorf("histogram granularities cannot be empty: histogram=%s", name)
}

if source == "" {
Expand Down Expand Up @@ -98,7 +98,7 @@ func HistoLine(name string, centroids histogram.Centroids, hgs map[histogram.Gra

for k, v := range tags {
if v == "" {
return "", errors.New("histogram tag value cannot be blank")
return "", fmt.Errorf("tag values cannot be empty: histogram=%s tag=%s", name, k)
}
sb.WriteString(" ")
sb.WriteString(strconv.Quote(sanitizeInternal(k)))
Expand All @@ -125,18 +125,18 @@ func HistoLine(name string, centroids histogram.Centroids, hgs map[histogram.Gra
// parent=2f64e538-9457-11e8-9eb6-529269fb1459 application=Wavefront http.method=GET 1533531013 343500"
func SpanLine(name string, startMillis, durationMillis int64, source, traceId, spanId string, parents, followsFrom []string, tags []SpanTag, spanLogs []SpanLog, defaultSource string) (string, error) {
if name == "" {
return "", errors.New("empty span name")
return "", errors.New("span name cannot be empty")
}

if source == "" {
source = defaultSource
}

if !isUUIDFormat(traceId) {
return "", errors.New("traceId is not in UUID format")
return "", fmt.Errorf("traceId is not in UUID format: span=%s", name)
oppegard marked this conversation as resolved.
Show resolved Hide resolved
}
if !isUUIDFormat(spanId) {
return "", errors.New("spanId is not in UUID format")
return "", fmt.Errorf("spanId is not in UUID format: span=%s", name)
oppegard marked this conversation as resolved.
Show resolved Hide resolved
}

sb := internal.GetBuffer()
Expand Down Expand Up @@ -168,8 +168,11 @@ func SpanLine(name string, startMillis, durationMillis int64, source, traceId, s
}

for _, tag := range tags {
if tag.Key == "" || tag.Value == "" {
return "", errors.New("span tag key/value cannot be blank")
if tag.Key == "" {
return "", fmt.Errorf("tag keys cannot be empty: span=%s", name)
}
if tag.Value == "" {
return "", fmt.Errorf("tag values cannot be empty: span=%s tag=%s", name, tag.Key)
}
sb.WriteString(" ")
sb.WriteString(strconv.Quote(sanitizeInternal(tag.Key)))
Expand Down
28 changes: 28 additions & 0 deletions senders/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/wavefronthq/wavefront-sdk-go/histogram"
)

Expand Down Expand Up @@ -192,6 +194,32 @@ func TestSpanLine(t *testing.T) {
assert.Equal(t, expected, line)
}

func TestSpanLineErrors(t *testing.T) {
uuid := "00000000-0000-0000-0000-000000000000"

_, err := SpanLine("", 0, 0, "", uuid, uuid, nil, nil, nil, nil, "")
require.Error(t, err)
assert.Equal(t, "span name cannot be empty", err.Error())

_, err = SpanLine("a_name", 0, 0, "00-00", "", uuid, nil, nil, nil, nil, "")
require.Error(t, err)
assert.Equal(t, "traceId is not in UUID format: span=a_name", err.Error())

_, err = SpanLine("a_name", 0, 0, "00-00", uuid, "", nil, nil, nil, nil, "")
require.Error(t, err)
assert.Equal(t, "spanId is not in UUID format: span=a_name", err.Error())

_, err = SpanLine("a_name", 0, 0, "a_source", uuid, uuid, nil, nil,
[]SpanTag{{Key: "", Value: ""}}, nil, "")
oppegard marked this conversation as resolved.
Show resolved Hide resolved
require.Error(t, err)
assert.Equal(t, "tag keys cannot be empty: span=a_name", err.Error())

_, err = SpanLine("a_name", 0, 0, "a_source", uuid, uuid, nil, nil,
[]SpanTag{{Key: "a_tag", Value: ""}}, nil, "")
require.Error(t, err)
assert.Equal(t, "tag values cannot be empty: span=a_name tag=a_tag", err.Error())
}

func makeCentroids() []histogram.Centroid {
centroids := []histogram.Centroid{
{
Expand Down