-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[chore]: datadog receiver: Tags translation #33922
Merged
jpkrohling
merged 2 commits into
open-telemetry:main
from
grafana:jvp/datadog-receiver-tags-translation
Jul 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package datadogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver" | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.16.0" | ||
) | ||
|
||
// See: | ||
// https://docs.datadoghq.com/opentelemetry/schema_semantics/semantic_mapping/ | ||
// https://github.com/DataDog/opentelemetry-mapping-go/blob/main/pkg/otlp/attributes/attributes.go | ||
var datadogKnownResourceAttributes = map[string]string{ | ||
"env": semconv.AttributeDeploymentEnvironment, | ||
"service": semconv.AttributeServiceName, | ||
"version": semconv.AttributeServiceVersion, | ||
|
||
// Container-related attributes | ||
"container_id": semconv.AttributeContainerID, | ||
"container_name": semconv.AttributeContainerName, | ||
"image_name": semconv.AttributeContainerImageName, | ||
"image_tag": semconv.AttributeContainerImageTag, | ||
"runtime": semconv.AttributeContainerRuntime, | ||
|
||
// Cloud-related attributes | ||
"cloud_provider": semconv.AttributeCloudProvider, | ||
"region": semconv.AttributeCloudRegion, | ||
"zone": semconv.AttributeCloudAvailabilityZone, | ||
|
||
// ECS-related attributes | ||
"task_family": semconv.AttributeAWSECSTaskFamily, | ||
"task_arn": semconv.AttributeAWSECSTaskARN, | ||
"ecs_cluster_name": semconv.AttributeAWSECSClusterARN, | ||
"task_version": semconv.AttributeAWSECSTaskRevision, | ||
"ecs_container_name": semconv.AttributeAWSECSContainerARN, | ||
|
||
// K8-related attributes | ||
"kube_container_name": semconv.AttributeK8SContainerName, | ||
"kube_cluster_name": semconv.AttributeK8SClusterName, | ||
"kube_deployment": semconv.AttributeK8SDeploymentName, | ||
"kube_replica_set": semconv.AttributeK8SReplicaSetName, | ||
"kube_stateful_set": semconv.AttributeK8SStatefulSetName, | ||
"kube_daemon_set": semconv.AttributeK8SDaemonSetName, | ||
"kube_job": semconv.AttributeK8SJobName, | ||
"kube_cronjob": semconv.AttributeK8SCronJobName, | ||
"kube_namespace": semconv.AttributeK8SNamespaceName, | ||
"pod_name": semconv.AttributeK8SPodName, | ||
|
||
// Other | ||
"process_id": semconv.AttributeProcessPID, | ||
"error.stacktrace": semconv.AttributeExceptionStacktrace, | ||
"error.msg": semconv.AttributeExceptionMessage, | ||
} | ||
|
||
// translateDatadogTagToKeyValuePair translates a Datadog tag to a key value pair | ||
func translateDatadogTagToKeyValuePair(tag string) (key string, value string) { | ||
if tag == "" { | ||
return "", "" | ||
} | ||
|
||
key, val, ok := strings.Cut(tag, ":") | ||
if !ok { | ||
// Datadog allows for two tag formats, one of which includes a key such as 'env', | ||
// followed by a value. Datadog also supports inputTags without the key, but OTel seems | ||
// to only support key:value pairs. | ||
// The following is a workaround to map unnamed inputTags to key:value pairs and its subject to future | ||
// changes if OTel supports unnamed inputTags in the future or if there is a better way to do this. | ||
key = fmt.Sprintf("unnamed_%s", tag) | ||
val = tag | ||
} | ||
return key, val | ||
} | ||
|
||
// translateDataDogKeyToOtel translates a Datadog key to an OTel key | ||
func translateDataDogKeyToOtel(k string) string { | ||
jesusvazquez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if otelKey, ok := datadogKnownResourceAttributes[strings.ToLower(k)]; ok { | ||
return otelKey | ||
} | ||
return k | ||
} | ||
|
||
type StringPool struct { | ||
sync.RWMutex | ||
pool map[string]string | ||
} | ||
|
||
func newStringPool() *StringPool { | ||
return &StringPool{ | ||
pool: make(map[string]string), | ||
} | ||
} | ||
|
||
func (s *StringPool) Intern(str string) string { | ||
s.RLock() | ||
interned, ok := s.pool[str] | ||
s.RUnlock() | ||
|
||
if ok { | ||
return interned | ||
} | ||
|
||
s.Lock() | ||
// Double check if another goroutine has added the string after releasing the read lock | ||
interned, ok = s.pool[str] | ||
if !ok { | ||
interned = str | ||
s.pool[str] = str | ||
} | ||
s.Unlock() | ||
|
||
return interned | ||
} | ||
|
||
func tagsToAttributes(tags []string, host string, stringPool *StringPool) (pcommon.Map, pcommon.Map, pcommon.Map) { | ||
resourceAttrs := pcommon.NewMap() | ||
scopeAttrs := pcommon.NewMap() | ||
dpAttrs := pcommon.NewMap() | ||
|
||
if host != "" { | ||
resourceAttrs.PutStr(semconv.AttributeHostName, host) | ||
} | ||
|
||
var key, val string | ||
for _, tag := range tags { | ||
key, val = translateDatadogTagToKeyValuePair(tag) | ||
if attr, ok := datadogKnownResourceAttributes[key]; ok { | ||
val = stringPool.Intern(val) // No need to intern the key if we already have it | ||
resourceAttrs.PutStr(attr, val) | ||
} else { | ||
key = stringPool.Intern(translateDataDogKeyToOtel(key)) | ||
val = stringPool.Intern(val) | ||
dpAttrs.PutStr(key, val) | ||
} | ||
} | ||
|
||
return resourceAttrs, scopeAttrs, dpAttrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package datadogreceiver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func TestGetMetricAttributes(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
tags []string | ||
host string | ||
expectedResourceAttrs pcommon.Map | ||
expectedScopeAttrs pcommon.Map | ||
expectedDpAttrs pcommon.Map | ||
}{ | ||
{ | ||
name: "empty", | ||
tags: []string{}, | ||
host: "", | ||
expectedResourceAttrs: pcommon.NewMap(), | ||
expectedScopeAttrs: pcommon.NewMap(), | ||
expectedDpAttrs: pcommon.NewMap(), | ||
}, | ||
{ | ||
name: "host", | ||
tags: []string{}, | ||
host: "host", | ||
expectedResourceAttrs: newMapFromKV(t, map[string]any{ | ||
"host.name": "host", | ||
}), | ||
expectedScopeAttrs: pcommon.NewMap(), | ||
expectedDpAttrs: pcommon.NewMap(), | ||
}, | ||
{ | ||
name: "provides both host and tags where some tag keys have to replaced by otel conventions", | ||
tags: []string{"env:prod", "service:my-service", "version:1.0"}, | ||
host: "host", | ||
expectedResourceAttrs: newMapFromKV(t, map[string]any{ | ||
"host.name": "host", | ||
"deployment.environment": "prod", | ||
"service.name": "my-service", | ||
"service.version": "1.0", | ||
}), | ||
expectedScopeAttrs: pcommon.NewMap(), | ||
expectedDpAttrs: pcommon.NewMap(), | ||
}, | ||
{ | ||
name: "provides host, tags and unnamed tags", | ||
tags: []string{"env:prod", "foo"}, | ||
host: "host", | ||
expectedResourceAttrs: newMapFromKV(t, map[string]any{ | ||
"host.name": "host", | ||
"deployment.environment": "prod", | ||
}), | ||
expectedScopeAttrs: pcommon.NewMap(), | ||
expectedDpAttrs: newMapFromKV(t, map[string]any{ | ||
"unnamed_foo": "foo", | ||
}), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
pool := newStringPool() | ||
resourceAttrs, scopeAttrs, dpAttrs := tagsToAttributes(c.tags, c.host, pool) | ||
|
||
assert.Equal(t, c.expectedResourceAttrs.Len(), resourceAttrs.Len()) | ||
c.expectedResourceAttrs.Range(func(k string, _ pcommon.Value) bool { | ||
ev, _ := c.expectedResourceAttrs.Get(k) | ||
av, ok := resourceAttrs.Get(k) | ||
assert.True(t, ok) | ||
assert.Equal(t, ev, av) | ||
return true | ||
}) | ||
|
||
assert.Equal(t, c.expectedScopeAttrs.Len(), scopeAttrs.Len()) | ||
c.expectedScopeAttrs.Range(func(k string, _ pcommon.Value) bool { | ||
ev, _ := c.expectedScopeAttrs.Get(k) | ||
av, ok := scopeAttrs.Get(k) | ||
assert.True(t, ok) | ||
assert.Equal(t, ev, av) | ||
return true | ||
}) | ||
|
||
assert.Equal(t, c.expectedDpAttrs.Len(), dpAttrs.Len()) | ||
c.expectedDpAttrs.Range(func(k string, _ pcommon.Value) bool { | ||
ev, _ := c.expectedDpAttrs.Get(k) | ||
av, ok := dpAttrs.Get(k) | ||
assert.True(t, ok) | ||
assert.Equal(t, ev, av) | ||
return true | ||
}) | ||
}) | ||
|
||
} | ||
|
||
} | ||
|
||
func newMapFromKV(t *testing.T, kv map[string]any) pcommon.Map { | ||
m := pcommon.NewMap() | ||
err := m.FromRaw(kv) | ||
assert.NoError(t, err) | ||
return m | ||
} | ||
|
||
func TestDatadogTagToKeyValuePair(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input string | ||
expectedKey string | ||
expectedValue string | ||
}{ | ||
{ | ||
name: "empty", | ||
input: "", | ||
expectedKey: "", | ||
expectedValue: "", | ||
}, | ||
{ | ||
name: "kv tag", | ||
input: "foo:bar", | ||
expectedKey: "foo", | ||
expectedValue: "bar", | ||
}, | ||
{ | ||
name: "unnamed tag", | ||
input: "foo", | ||
expectedKey: "unnamed_foo", | ||
expectedValue: "foo", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
key, value := translateDatadogTagToKeyValuePair(c.input) | ||
if key != c.expectedKey { | ||
jesusvazquez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Errorf("Expected key %s, got %s", c.expectedKey, key) | ||
} | ||
if value != c.expectedValue { | ||
t.Errorf("Expected value %s, got %s", c.expectedValue, value) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestTranslateDataDogKeyToOtel(t *testing.T) { | ||
// make sure all known keys are translated | ||
for k, v := range datadogKnownResourceAttributes { | ||
t.Run(k, func(t *testing.T) { | ||
assert.Equal(t, v, translateDataDogKeyToOtel(k)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this common practice elsewhere as well? As a user that tagged something with "foo", I'd probably expect to see it translated to
foo=foo
orfoo=true
, or even just "foo=" (empty value).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem doing that is that perhaps there was already a label name called
foo
?Because we don't know in advance, I thought about prefixing the label with something that is not common, such as
unnamed_
.I particularly don't make use of unnamed tags so I wonder what would be the best approach here too. We can always change it when some users try it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with changing it later is that we'd break users who start depending on the behavior introduced on this PR here. I don't have a good solution for the moment, and I think it would be OK to add a note to the readme stating that it currently is "unnamed_" and might change later, and that the appropriate solution is to perhaps be more explicit (foo:foo, foo:, :foo, foo:true).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change to the readme can be done on a subsequent PR. I'm merging this.