|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package datadogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver" |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 12 | + semconv "go.opentelemetry.io/collector/semconv/v1.16.0" |
| 13 | +) |
| 14 | + |
| 15 | +// See: |
| 16 | +// https://docs.datadoghq.com/opentelemetry/schema_semantics/semantic_mapping/ |
| 17 | +// https://github.com/DataDog/opentelemetry-mapping-go/blob/main/pkg/otlp/attributes/attributes.go |
| 18 | +var datadogKnownResourceAttributes = map[string]string{ |
| 19 | + "env": semconv.AttributeDeploymentEnvironment, |
| 20 | + "service": semconv.AttributeServiceName, |
| 21 | + "version": semconv.AttributeServiceVersion, |
| 22 | + |
| 23 | + // Container-related attributes |
| 24 | + "container_id": semconv.AttributeContainerID, |
| 25 | + "container_name": semconv.AttributeContainerName, |
| 26 | + "image_name": semconv.AttributeContainerImageName, |
| 27 | + "image_tag": semconv.AttributeContainerImageTag, |
| 28 | + "runtime": semconv.AttributeContainerRuntime, |
| 29 | + |
| 30 | + // Cloud-related attributes |
| 31 | + "cloud_provider": semconv.AttributeCloudProvider, |
| 32 | + "region": semconv.AttributeCloudRegion, |
| 33 | + "zone": semconv.AttributeCloudAvailabilityZone, |
| 34 | + |
| 35 | + // ECS-related attributes |
| 36 | + "task_family": semconv.AttributeAWSECSTaskFamily, |
| 37 | + "task_arn": semconv.AttributeAWSECSTaskARN, |
| 38 | + "ecs_cluster_name": semconv.AttributeAWSECSClusterARN, |
| 39 | + "task_version": semconv.AttributeAWSECSTaskRevision, |
| 40 | + "ecs_container_name": semconv.AttributeAWSECSContainerARN, |
| 41 | + |
| 42 | + // K8-related attributes |
| 43 | + "kube_container_name": semconv.AttributeK8SContainerName, |
| 44 | + "kube_cluster_name": semconv.AttributeK8SClusterName, |
| 45 | + "kube_deployment": semconv.AttributeK8SDeploymentName, |
| 46 | + "kube_replica_set": semconv.AttributeK8SReplicaSetName, |
| 47 | + "kube_stateful_set": semconv.AttributeK8SStatefulSetName, |
| 48 | + "kube_daemon_set": semconv.AttributeK8SDaemonSetName, |
| 49 | + "kube_job": semconv.AttributeK8SJobName, |
| 50 | + "kube_cronjob": semconv.AttributeK8SCronJobName, |
| 51 | + "kube_namespace": semconv.AttributeK8SNamespaceName, |
| 52 | + "pod_name": semconv.AttributeK8SPodName, |
| 53 | + |
| 54 | + // Other |
| 55 | + "process_id": semconv.AttributeProcessPID, |
| 56 | + "error.stacktrace": semconv.AttributeExceptionStacktrace, |
| 57 | + "error.msg": semconv.AttributeExceptionMessage, |
| 58 | +} |
| 59 | + |
| 60 | +// translateDatadogTagToKeyValuePair translates a Datadog tag to a key value pair |
| 61 | +func translateDatadogTagToKeyValuePair(tag string) (key string, value string) { |
| 62 | + if tag == "" { |
| 63 | + return "", "" |
| 64 | + } |
| 65 | + |
| 66 | + key, val, ok := strings.Cut(tag, ":") |
| 67 | + if !ok { |
| 68 | + // Datadog allows for two tag formats, one of which includes a key such as 'env', |
| 69 | + // followed by a value. Datadog also supports inputTags without the key, but OTel seems |
| 70 | + // to only support key:value pairs. |
| 71 | + // The following is a workaround to map unnamed inputTags to key:value pairs and its subject to future |
| 72 | + // changes if OTel supports unnamed inputTags in the future or if there is a better way to do this. |
| 73 | + key = fmt.Sprintf("unnamed_%s", tag) |
| 74 | + val = tag |
| 75 | + } |
| 76 | + return key, val |
| 77 | +} |
| 78 | + |
| 79 | +// translateDatadogKeyToOTel translates a Datadog key to an OTel key |
| 80 | +func translateDatadogKeyToOTel(k string) string { |
| 81 | + if otelKey, ok := datadogKnownResourceAttributes[strings.ToLower(k)]; ok { |
| 82 | + return otelKey |
| 83 | + } |
| 84 | + return k |
| 85 | +} |
| 86 | + |
| 87 | +type StringPool struct { |
| 88 | + sync.RWMutex |
| 89 | + pool map[string]string |
| 90 | +} |
| 91 | + |
| 92 | +func newStringPool() *StringPool { |
| 93 | + return &StringPool{ |
| 94 | + pool: make(map[string]string), |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (s *StringPool) Intern(str string) string { |
| 99 | + s.RLock() |
| 100 | + interned, ok := s.pool[str] |
| 101 | + s.RUnlock() |
| 102 | + |
| 103 | + if ok { |
| 104 | + return interned |
| 105 | + } |
| 106 | + |
| 107 | + s.Lock() |
| 108 | + // Double check if another goroutine has added the string after releasing the read lock |
| 109 | + interned, ok = s.pool[str] |
| 110 | + if !ok { |
| 111 | + interned = str |
| 112 | + s.pool[str] = str |
| 113 | + } |
| 114 | + s.Unlock() |
| 115 | + |
| 116 | + return interned |
| 117 | +} |
| 118 | + |
| 119 | +func tagsToAttributes(tags []string, host string, stringPool *StringPool) (pcommon.Map, pcommon.Map, pcommon.Map) { |
| 120 | + resourceAttrs := pcommon.NewMap() |
| 121 | + scopeAttrs := pcommon.NewMap() |
| 122 | + dpAttrs := pcommon.NewMap() |
| 123 | + |
| 124 | + if host != "" { |
| 125 | + resourceAttrs.PutStr(semconv.AttributeHostName, host) |
| 126 | + } |
| 127 | + |
| 128 | + var key, val string |
| 129 | + for _, tag := range tags { |
| 130 | + key, val = translateDatadogTagToKeyValuePair(tag) |
| 131 | + if attr, ok := datadogKnownResourceAttributes[key]; ok { |
| 132 | + val = stringPool.Intern(val) // No need to intern the key if we already have it |
| 133 | + resourceAttrs.PutStr(attr, val) |
| 134 | + } else { |
| 135 | + key = stringPool.Intern(translateDatadogKeyToOTel(key)) |
| 136 | + val = stringPool.Intern(val) |
| 137 | + dpAttrs.PutStr(key, val) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return resourceAttrs, scopeAttrs, dpAttrs |
| 142 | +} |
0 commit comments