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
48 changes: 48 additions & 0 deletions exporter/datadogexporter/internal/attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package attributes

import (
"fmt"
"strings"

"go.opentelemetry.io/collector/model/pdata"
conventions "go.opentelemetry.io/collector/model/semconv/v1.5.0"
Expand Down Expand Up @@ -54,6 +55,39 @@ var (
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",
conventions.AttributeK8SReplicaSetName: "kube_replica_set",
conventions.AttributeK8SStatefulSetName: "kube_stateful_set",
conventions.AttributeK8SDaemonSetName: "kube_daemon_set",
conventions.AttributeK8SJobName: "kube_job",
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",
}

// Kubernetes mappings defines the mapping between Kubernetes conventions (both general and Datadog specific)
// and Datadog Agent conventions. The Datadog Agent conventions can be found at
// https://github.com/DataDog/datadog-agent/blob/e081bed/pkg/tagger/collectors/const.go and
Expand Down Expand Up @@ -120,3 +154,17 @@ func TagsFromAttributes(attrs pdata.AttributeMap) []string {

return tags
}

// ContainerTagsFromAttributes 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(), ",")
}
51 changes: 51 additions & 0 deletions exporter/datadogexporter/internal/attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package attributes

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -52,3 +53,53 @@ func TestTagsFromAttributesEmpty(t *testing.T) {

assert.Equal(t, []string{}, TagsFromAttributes(attrs))
}

func TestContainerTagsFromAttributes(t *testing.T) {
attributeMap := map[string]string{
conventions.AttributeContainerName: "sample_app",
conventions.AttributeContainerImageTag: "sample_app_image_tag",
conventions.AttributeK8SContainerName: "kube_sample_app",
conventions.AttributeK8SReplicaSetName: "sample_replica_set",
conventions.AttributeK8SDaemonSetName: "sample_daemonset_name",
conventions.AttributeK8SPodName: "sample_pod_name",
conventions.AttributeCloudProvider: "sample_cloud_provider",
conventions.AttributeCloudRegion: "sample_region",
conventions.AttributeCloudAvailabilityZone: "sample_zone",
conventions.AttributeAWSECSTaskFamily: "sample_task_family",
conventions.AttributeAWSECSClusterARN: "sample_ecs_cluster_name",
conventions.AttributeAWSECSContainerARN: "sample_ecs_container_name",
"custom_tag": "example_custom_tag",
"": "empty_string_key",
"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)
}

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

assert.Equal(t, empty, ContainerTagsFromAttributes(attributeMap))
}
22 changes: 1 addition & 21 deletions exporter/datadogexporter/translate_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

"github.com/DataDog/datadog-agent/pkg/trace/exportable/pb"
Expand Down Expand Up @@ -389,29 +388,10 @@ 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] = buildDatadogContainerTags(spanTags)
spanTags[tagContainersTags] = attributes.ContainerTagsFromAttributes(spanTags)
return spanTags
}

// buildDatadogContainerTags returns container and orchestrator tags belonging to containerID
// as a comma delimeted list for datadog's special container tag key
func buildDatadogContainerTags(spanTags map[string]string) string {
var b strings.Builder

if val, ok := spanTags[conventions.AttributeContainerID]; ok {
b.WriteString(fmt.Sprintf("%s:%s,", "container_id", val))
}
if val, ok := spanTags[conventions.AttributeK8SPodName]; ok {
b.WriteString(fmt.Sprintf("%s:%s,", "pod_name", val))
}

if val, ok := spanTags[conventions.AttributeAWSECSTaskARN]; ok {
b.WriteString(fmt.Sprintf("%s:%s,", "task_arn", val))
}

return strings.TrimSuffix(b.String(), ",")
}

// inferDatadogTypes returns a string for the datadog type based on metadata
// in the otel span. DB semantic conventions state that what datadog
// would mark as a db or cache span type, otel marks as a CLIENT span kind, but
Expand Down