diff --git a/pkg/backends/otlp/backend.go b/pkg/backends/otlp/backend.go index 1e3c2ad9..3927fdbe 100644 --- a/pkg/backends/otlp/backend.go +++ b/pkg/backends/otlp/backend.go @@ -145,10 +145,10 @@ func (bd *Backend) SendMetricsAsync(ctx context.Context, mm *gostatsd.MetricMap, currentGroup := newGroups(bd.metricsPerBatch) 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)) + if !cm.Tags.Exists("host") && cm.Source != "" { + cm.Tags.Concat(gostatsd.Tags{"host:" + string(cm.Source)}) } + resources, attributes := data.SplitMetricTagsByKeysAndConvert(cm.Tags, bd.resourceKeys) rate := data.NewMetric(name).SetGauge( data.NewGauge( @@ -175,10 +175,10 @@ 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)) + if !gm.Tags.Exists("host") && gm.Source != "" { + gm.Tags.Concat(gostatsd.Tags{"host:" + string(gm.Source)}) } + resources, attributes := data.SplitMetricTagsByKeysAndConvert(gm.Tags, bd.resourceKeys) m := data.NewMetric(name).SetGauge( data.NewGauge( @@ -194,10 +194,10 @@ 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)) + if !sm.Tags.Exists("host") && sm.Source != "" { + sm.Tags.Concat(gostatsd.Tags{"host:" + string(sm.Source)}) } + resources, attributes := data.SplitMetricTagsByKeysAndConvert(sm.Tags, bd.resourceKeys) m := data.NewMetric(name).SetGauge( data.NewGauge( @@ -213,10 +213,10 @@ 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)) + if !t.Tags.Exists("host") && t.Source != "" { + t.Tags.Concat(gostatsd.Tags{"host:" + string(t.Source)}) } + resources, attributes := data.SplitMetricTagsByKeysAndConvert(t.Tags, bd.resourceKeys) switch bd.convertTimersToGauges { case true: diff --git a/pkg/backends/otlp/backend_test.go b/pkg/backends/otlp/backend_test.go index 74709bce..93f08093 100644 --- a/pkg/backends/otlp/backend_test.go +++ b/pkg/backends/otlp/backend_test.go @@ -326,7 +326,7 @@ func TestBackendSendAsyncMetrics(t *testing.T) { ms := req.GetResourceMetrics()[0].GetScopeMetrics()[0].GetMetrics() dpCountAttrs := ms[1].GetSum().DataPoints[0].GetAttributes() for _, attr := range dpCountAttrs { - if attr.Key == "source" { + if attr.Key == "host" { assert.Equal(t, "fake-source", attr.Value.GetStringValue()) return } @@ -369,7 +369,7 @@ func TestBackendSendAsyncMetrics(t *testing.T) { ms := req.GetResourceMetrics()[0].GetScopeMetrics()[0].GetMetrics() dpCountAttrs := ms[1].GetSum().DataPoints[0].GetAttributes() for _, attr := range dpCountAttrs { - if attr.Key == "source" { + if attr.Key == "host" { assert.Error(t, fmt.Errorf("source attribute not found")) return } diff --git a/tags.go b/tags.go index 3fce0628..ca4d9c10 100644 --- a/tags.go +++ b/tags.go @@ -56,6 +56,17 @@ func (tags Tags) Copy() Tags { return tagCopy } +// Exists returns true if the tag exists in the tags +func (tags Tags) Exists(tag string) bool { + for _, t := range tags { + key, _ := parseTag(t) + if key == tag { + return true + } + } + return false +} + // ToMap converts all the tags into a format that can be translated // to send to a different vendor if required. // - If the tag exists without a value it is converted to: "unknown:" diff --git a/tags_test.go b/tags_test.go index 5346d672..7b10ce71 100644 --- a/tags_test.go +++ b/tags_test.go @@ -37,3 +37,11 @@ func TestTagsToMap(t *testing.T) { }) } } + +func TestTagsExist(t *testing.T) { + t.Parallel() + tags := Tags{"a:b", "c:d"} + assert.True(t, tags.Exists("a")) + assert.True(t, tags.Exists("c")) + assert.False(t, tags.Exists("e")) +}