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

Propagate gostatsd source into otlp backend dimension #724

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions pkg/backends/otlp/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func (bd *Backend) SendMetricsAsync(ctx context.Context, mm *gostatsd.MetricMap,

mm.Counters.Each(func(name, _ string, cm gostatsd.Counter) {
resources, attributes := data.SplitMetricTagsByKeysAndConvert(cm.Tags, bd.resourceKeys)
if cm.Source != "" {
attributes.Insert("source", string(cm.Source))
}

rate := data.NewMetric(name).SetGauge(
data.NewGauge(
Expand Down Expand Up @@ -173,6 +176,9 @@ func (bd *Backend) SendMetricsAsync(ctx context.Context, mm *gostatsd.MetricMap,

mm.Gauges.Each(func(name, _ string, gm gostatsd.Gauge) {
resources, attributes := data.SplitMetricTagsByKeysAndConvert(gm.Tags, bd.resourceKeys)
if gm.Source != "" {
attributes.Insert("source", string(gm.Source))
}

m := data.NewMetric(name).SetGauge(
data.NewGauge(
Expand All @@ -189,6 +195,9 @@ func (bd *Backend) SendMetricsAsync(ctx context.Context, mm *gostatsd.MetricMap,

mm.Sets.Each(func(name, _ string, sm gostatsd.Set) {
resources, attributes := data.SplitMetricTagsByKeysAndConvert(sm.Tags, bd.resourceKeys)
if sm.Source != "" {
attributes.Insert("source", string(sm.Source))
}

m := data.NewMetric(name).SetGauge(
data.NewGauge(
Expand All @@ -205,6 +214,9 @@ func (bd *Backend) SendMetricsAsync(ctx context.Context, mm *gostatsd.MetricMap,

mm.Timers.Each(func(name, _ string, t gostatsd.Timer) {
resources, attributes := data.SplitMetricTagsByKeysAndConvert(t.Tags, bd.resourceKeys)
if t.Source != "" {
attributes.Insert("source", string(t.Source))
}

switch bd.convertTimersToGauges {
case true:
Expand Down
85 changes: 85 additions & 0 deletions pkg/backends/otlp/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,91 @@ func TestBackendSendAsyncMetrics(t *testing.T) {
}
},
},
{
name: "valid metric data with source",
mm: func() *gostatsd.MetricMap {
mm := gostatsd.NewMetricMap(false)
mm.Receive(&gostatsd.Metric{
Name: "my-metric",
Value: 100.0,
Rate: 1,
Tags: gostatsd.Tags{"service.name:my-awesome-service"},
Timestamp: gostatsd.Nanotime(time.Unix(100, 0).UnixNano()),
Type: gostatsd.COUNTER,
Source: "fake-source",
})
return mm
}(),
handler: func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
assert.NoError(t, err, "Must not error reading body")
assert.NotEmpty(t, body, "Must not have an empty body")

req := &v1export.ExportMetricsServiceRequest{}
err = proto.Unmarshal(body, req)
assert.NoError(t, err, "Must not error unmarshalling body")

ms := req.GetResourceMetrics()[0].GetScopeMetrics()[0].GetMetrics()
dpCountAttrs := ms[1].GetSum().DataPoints[0].GetAttributes()
for _, attr := range dpCountAttrs {
if attr.Key == "source" {
assert.Equal(t, "fake-source", attr.Value.GetStringValue())
return
}

assert.Error(t, fmt.Errorf("source attribute not found"))
}
},
enableHistograms: false,
validate: func(t *testing.T) func(errs []error) {
return func(errs []error) {
if !assert.Len(t, errs, 0, "Must not error") {
return
}
}
},
},
{
name: "valid metric data without source",
mm: func() *gostatsd.MetricMap {
mm := gostatsd.NewMetricMap(false)
mm.Receive(&gostatsd.Metric{
Name: "my-metric",
Value: 100.0,
Rate: 1,
Tags: gostatsd.Tags{"service.name:my-awesome-service"},
Timestamp: gostatsd.Nanotime(time.Unix(100, 0).UnixNano()),
Type: gostatsd.COUNTER,
})
return mm
}(),
handler: func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
assert.NoError(t, err, "Must not error reading body")
assert.NotEmpty(t, body, "Must not have an empty body")

req := &v1export.ExportMetricsServiceRequest{}
err = proto.Unmarshal(body, req)
assert.NoError(t, err, "Must not error unmarshalling body")

ms := req.GetResourceMetrics()[0].GetScopeMetrics()[0].GetMetrics()
dpCountAttrs := ms[1].GetSum().DataPoints[0].GetAttributes()
for _, attr := range dpCountAttrs {
if attr.Key == "source" {
assert.Error(t, fmt.Errorf("source attribute not found"))
return
}
}
},
enableHistograms: false,
validate: func(t *testing.T) func(errs []error) {
return func(errs []error) {
if !assert.Len(t, errs, 0, "Must not error") {
return
}
}
},
},
{
name: "valid metric data with histogram conversion",
mm: func() *gostatsd.MetricMap {
Expand Down
Loading