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

[exporter/datadog]: Add container tags to attributes package #6086

Merged
merged 6 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make changes based on feedback
  • Loading branch information
mackjmr committed Nov 3, 2021
commit 69b23029ca824330aa753f2736ec2ed8f30db413
95 changes: 52 additions & 43 deletions exporter/datadogexporter/internal/attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ var (
conventions.AttributeServiceName: "service",
conventions.AttributeServiceVersion: "version",

// Containers
conventions.AttributeContainerID: "container_id",
conventions.AttributeContainerName: "container_name",
conventions.AttributeContainerImageName: "image_name",
conventions.AttributeContainerImageTag: "image_tag",

// Cloud conventions
// https://www.datadoghq.com/blog/tagging-best-practices/
conventions.AttributeCloudProvider: "cloud_provider",
Expand All @@ -40,29 +46,14 @@ var (

// ECS conventions
// https://github.com/DataDog/datadog-agent/blob/e081bed/pkg/tagger/collectors/ecs_extract.go
conventions.AttributeAWSECSTaskFamily: "task_family",
conventions.AttributeAWSECSClusterARN: "ecs_cluster_name",
"aws.ecs.task.revision": "task_version",
conventions.AttributeAWSECSTaskFamily: "task_family",
conventions.AttributeAWSECSTaskARN: "task_arn",
conventions.AttributeAWSECSClusterARN: "ecs_cluster_name",
conventions.AttributeAWSECSTaskRevision: "task_version",
conventions.AttributeAWSECSContainerARN: "ecs_container_name",

// Kubernetes resource name (via semantic conventions)
// https://github.com/DataDog/datadog-agent/blob/e081bed/pkg/util/kubernetes/const.go
conventions.AttributeK8SPodName: "pod_name",
conventions.AttributeK8SDeploymentName: "kube_deployment",
conventions.AttributeK8SReplicaSetName: "kube_replica_set",
conventions.AttributeK8SStatefulSetName: "kube_stateful_set",
conventions.AttributeK8SDaemonSetName: "kube_daemon_set",
conventions.AttributeK8SJobName: "kube_job",
conventions.AttributeK8SCronJobName: "kube_cronjob",
}

containersMapping = map[string]string{
// Containers
conventions.AttributeContainerID: "container_id",
conventions.AttributeContainerName: "container_name",
conventions.AttributeContainerImageName: "image_name",
conventions.AttributeContainerImageTag: "image_tag",

//Kubernetes
conventions.AttributeK8SContainerName: "kube_container_name",
conventions.AttributeK8SClusterName: "kube_cluster_name",
conventions.AttributeK8SDeploymentName: "kube_deployment",
Expand All @@ -73,19 +64,32 @@ var (
conventions.AttributeK8SCronJobName: "kube_cronjob",
conventions.AttributeK8SNamespaceName: "kube_namespace",
conventions.AttributeK8SPodName: "pod_name",
}

// Cloud conventions
// https://www.datadoghq.com/blog/tagging-best-practices/
conventions.AttributeCloudProvider: "cloud_provider",
conventions.AttributeCloudRegion: "region",
conventions.AttributeCloudAvailabilityZone: "zone",

// ECS Conventions
conventions.AttributeAWSECSTaskFamily: "task_family",
conventions.AttributeAWSECSTaskARN: "task_arn",
conventions.AttributeAWSECSClusterARN: "ecs_cluster_name",
conventions.AttributeAWSECSTaskRevision: "task_version",
conventions.AttributeAWSECSContainerARN: "ecs_container_name",
// containerTagsAttributes contains a set of attributes that will be extracted as Datadog container tags.
containerTagsAttributes = []string{
conventions.AttributeContainerID,
conventions.AttributeContainerName,
conventions.AttributeContainerImageName,
conventions.AttributeContainerImageTag,
conventions.AttributeK8SContainerName,
conventions.AttributeK8SClusterName,
conventions.AttributeK8SDeploymentName,
conventions.AttributeK8SReplicaSetName,
conventions.AttributeK8SStatefulSetName,
conventions.AttributeK8SDaemonSetName,
conventions.AttributeK8SJobName,
conventions.AttributeK8SCronJobName,
conventions.AttributeK8SNamespaceName,
conventions.AttributeK8SPodName,
conventions.AttributeCloudProvider,
conventions.AttributeCloudRegion,
conventions.AttributeCloudAvailabilityZone,
conventions.AttributeAWSECSTaskFamily,
conventions.AttributeAWSECSTaskARN,
conventions.AttributeAWSECSClusterARN,
conventions.AttributeAWSECSTaskRevision,
conventions.AttributeAWSECSContainerARN,
}

// Kubernetes mappings defines the mapping between Kubernetes conventions (both general and Datadog specific)
Expand Down Expand Up @@ -155,16 +159,21 @@ func TagsFromAttributes(attrs pdata.AttributeMap) []string {
return tags
}

// ContainerTagsFromAttributes converts a selected list of attribute keys into
// ContainerTagFromAttributes converts a selected list of attribute keys into
// their equivalent Datadog container keys to be added to __dd.tags.container
func ContainerTagsFromAttributes(spanTags map[string]string) string {
var b strings.Builder

for k, v := range containersMapping {
if val, ok := spanTags[k]; ok {
b.WriteString(fmt.Sprintf("%s:%s,", v, val))
}
}

return strings.TrimSuffix(b.String(), ",")
func ContainerTagFromAttributes(attr map[string]string) string {
var str strings.Builder
for _, key := range containerTagsAttributes {
val, ok := attr[key]
if !ok {
continue
}
if str.Len() > 0 {
str.WriteByte(',')
}
str.WriteString(conventionsMapping[key])
str.WriteByte(':')
str.WriteString(val)
}
return str.String()
}
30 changes: 4 additions & 26 deletions exporter/datadogexporter/internal/attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package attributes

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestTagsFromAttributesEmpty(t *testing.T) {
assert.Equal(t, []string{}, TagsFromAttributes(attrs))
}

func TestContainerTagsFromAttributes(t *testing.T) {
func TestContainerTagFromAttributes(t *testing.T) {
attributeMap := map[string]string{
conventions.AttributeContainerName: "sample_app",
conventions.AttributeContainerImageTag: "sample_app_image_tag",
Expand All @@ -73,33 +72,12 @@ func TestContainerTagsFromAttributes(t *testing.T) {
"empty_string_val": "",
}

containerMap := make(map[string]string)
containerTags := ContainerTagsFromAttributes(attributeMap)

for _, v := range strings.Split(containerTags, ",") {
tag := strings.Split(v, ":")
containerMap[tag[0]] = tag[1]
}

assert.Equal(t, map[string]string{
"container_name": "sample_app",
"image_tag": "sample_app_image_tag",
"kube_container_name": "kube_sample_app",
"kube_replica_set": "sample_replica_set",
"kube_daemon_set": "sample_daemonset_name",
"pod_name": "sample_pod_name",
"cloud_provider": "sample_cloud_provider",
"region": "sample_region",
"zone": "sample_zone",
"task_family": "sample_task_family",
"ecs_cluster_name": "sample_ecs_cluster_name",
"ecs_container_name": "sample_ecs_container_name",
}, containerMap)
assert.Equal(t, "container_name:sample_app,image_tag:sample_app_image_tag,kube_container_name:kube_sample_app,kube_replica_set:sample_replica_set,kube_daemon_set:sample_daemonset_name,pod_name:sample_pod_name,cloud_provider:sample_cloud_provider,region:sample_region,zone:sample_zone,task_family:sample_task_family,ecs_cluster_name:sample_ecs_cluster_name,ecs_container_name:sample_ecs_container_name", ContainerTagFromAttributes(attributeMap))
}

func TestContainerTagsFromAttributesEmpty(t *testing.T) {
func TestContainerTagFromAttributesEmpty(t *testing.T) {
var empty string
attributeMap := map[string]string{}

assert.Equal(t, empty, ContainerTagsFromAttributes(attributeMap))
assert.Equal(t, empty, ContainerTagFromAttributes(attributeMap))
}
2 changes: 1 addition & 1 deletion exporter/datadogexporter/translate_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func aggregateSpanTags(span pdata.Span, datadogTags map[string]string) map[strin
})

// we don't want to normalize these tags since `_dd` is a special case
spanTags[tagContainersTags] = attributes.ContainerTagsFromAttributes(spanTags)
spanTags[tagContainersTags] = attributes.ContainerTagFromAttributes(spanTags)
return spanTags
}

Expand Down