Skip to content
New issue

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

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

Already on GitHub? # to your account

Update host tag on OTLP backend from source #725

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
24 changes: 12 additions & 12 deletions pkg/backends/otlp/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/backends/otlp/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:<tag>"
Expand Down
8 changes: 8 additions & 0 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Loading