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

[tagger] Extract list of tags to its own package #26884

Merged
merged 3 commits into from
Jun 19, 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
278 changes: 133 additions & 145 deletions comp/core/tagger/taggerimpl/collectors/workloadmeta_extract.go

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions comp/core/tagger/tags/kubernetes_kinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package tags

import (
"fmt"

"github.com/DataDog/datadog-agent/pkg/util/kubernetes"
)

var kindToTagName = map[string]string{
kubernetes.PodKind: KubePod,
kubernetes.DeploymentKind: KubeDeployment,
kubernetes.ReplicaSetKind: KubeReplicaSet,
kubernetes.ReplicationControllerKind: KubeReplicationController,
kubernetes.StatefulSetKind: KubeStatefulSet,
kubernetes.DaemonSetKind: KubeDaemonSet,
kubernetes.JobKind: KubeJob,
kubernetes.CronJobKind: KubeCronjob,
kubernetes.ServiceKind: KubeService,
kubernetes.NamespaceKind: KubeNamespace,
}

// GetTagForKind returns the tag name for the given kind
func GetTagForKind(kind string) (string, error) {
tagName, found := kindToTagName[kind]
if !found {
return "", fmt.Errorf("no tag found for kind %s", kind)
}

return tagName, nil
}
171 changes: 171 additions & 0 deletions comp/core/tagger/tags/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

// Package tags contains the list of tags that are added by the tagger
package tags

const (
// STANDARD TAGS

// Env is the standard tag for the environment
Env = "env"
// Version is the standard tag for the version
Version = "version"
// Service is the standard tag for the service
Service = "service"

// LOW CARDINALITY

// ClusterName is the tag for the cluster name
ClusterName = "cluster_name"

// ImageName is the tag for the image name
ImageName = "image_name"
// ShortImage is the tag for the short image name
ShortImage = "short_image"
// ImageTag is the tag for the image tag
ImageTag = "image_tag"
// ImageID is the tag for the image ID
ImageID = "image_id"
// DockerImage is the tag for the docker image
DockerImage = "docker_image"
// OSName is the tag for the OS name of the container image
OSName = "os_name"
// OSVersion is the tag for the OS version of the container image
OSVersion = "os_version"
// Architecture is the tag for the architecture of the container image
Architecture = "architecture"

// PodPhase is the tag for the pod phase
PodPhase = "pod_phase"
// DisplayContainerName is the tag for the display container name
DisplayContainerName = "display_container_name"
// KubePriorityClass is the tag for the Kubernetes priority class
KubePriorityClass = "kube_priority_class"
// KubeQOS is the tag for the Kubernetes QoS (Quality of Service)
KubeQOS = "kube_qos"
// KubeContainerName is the tag for the Kubernetes container name
KubeContainerName = "kube_container_name"
// KubeOwnerRefKind is the tag for the Kubernetes owner reference kind
KubeOwnerRefKind = "kube_ownerref_kind"

// KubePod is the tag for the pod name
KubePod = "pod_name"
// KubeDeployment is the tag for the deployment name
KubeDeployment = "kube_deployment"
// KubeReplicaSet is the tag for the replica set name
KubeReplicaSet = "kube_replica_set"
// KubeReplicationController is the tag for the replication controller name
KubeReplicationController = "kube_replication_controller"
// KubeStatefulSet is the tag for the stateful set name
KubeStatefulSet = "kube_stateful_set"
// KubeDaemonSet is the tag for the daemon set name
KubeDaemonSet = "kube_daemon_set"
// KubeJob is the tag for the job name
KubeJob = "kube_job"
// KubeCronjob is the tag for the cronjob name
KubeCronjob = "kube_cronjob"
// KubeService is the tag for the service name
KubeService = "kube_service"
// KubeNamespace is the tag for the namespace name
KubeNamespace = "kube_namespace"
// KubePersistentVolumeClaim is the tag for the persistent volume name
KubePersistentVolumeClaim = "persistentvolumeclaim"

// KubeAppName is the tag for the "app.kubernetes.io/name" Kubernetes label
KubeAppName = "kube_app_name"
// KubeAppInstance is the tag for the "app.kubernetes.io/instance" Kubernetes label
KubeAppInstance = "kube_app_instance"
// KubeAppVersion is the tag for the "app.kubernetes.io/version" Kubernetes label
KubeAppVersion = "kube_app_version"
// KubeAppComponent is the tag for the "app.kubernetes.io/component" Kubernetes label
KubeAppComponent = "kube_app_component"
// KubeAppPartOf is the tag for the "app.kubernetes.io/part-of" Kubernetes label
KubeAppPartOf = "kube_app_part_of"
// KubeAppManagedBy is the tag for the "app.kubernetes.io/managed-by" Kubernetes label
KubeAppManagedBy = "kube_app_managed_by"

// OpenshiftDeploymentConfig is the tag for the OpenShift deployment config name
OpenshiftDeploymentConfig = "oshift_deployment_config"

// TaskName is the tag for the ECS task name
TaskName = "task_name"
// TaskFamily is the tag for the ECS task family
TaskFamily = "task_family"
// TaskVersion is the tag for the ECS task version
TaskVersion = "task_version"
// Region is the tag for the ECS region
Region = "region"
// AvailabilityZone is the tag for the ECS availability zone
AvailabilityZone = "availability-zone"
// AvailabilityZoneDeprecated is the tag for the ECS availability zone (deprecated)
AvailabilityZoneDeprecated = "availability_zone"
// EcsContainerName is the tag for the ECS container name
EcsContainerName = "ecs_container_name"
// EcsClusterName is the tag for the ECS cluster name
EcsClusterName = "ecs_cluster_name"

// Language is the tag for the process language
Language = "language"

// MarathonApp is the tag for the Marathon app ID
MarathonApp = "marathon_app"

// ChronosJob is the tag for the Chronos job
ChronosJob = "chronos_job"
// ChronosJobOwner is the tag for the Chronos job owner
ChronosJobOwner = "chronos_job_owner"

// NomadTask is the tag for the Nomad task
NomadTask = "nomad_task"
// NomadJob is the tag for the Nomad job
NomadJob = "nomad_job"
// NomadGroup is the tag for the Nomad group
NomadGroup = "nomad_group"
// NomadNamespace is the tag for the Nomad namespace
NomadNamespace = "nomad_namespace"
// NomadDC is the tag for the Nomad datacenter
NomadDC = "nomad_dc"

// SwarmService is the tag for the Docker Swarm service
SwarmService = "swarm_service"
// SwarmNamespace is the tag for the Docker Swarm namespace
SwarmNamespace = "swarm_namespace"

// RancherStack is the tag for the Rancher stack
RancherStack = "rancher_stack"
// RancherService is the tag for the Rancher service
RancherService = "rancher_service"

// GitCommitSha is the tag for the Git commit SHA
GitCommitSha = "git.commit.sha"
// GitRepository is the tag for the Git repository URL
GitRepository = "git.repository_url"

// RemoteConfigID is the tag for the remote config ID
RemoteConfigID = "dd_remote_config_id"
// RemoteConfigRevision is the tag for the remote config revision
RemoteConfigRevision = "dd_remote_config_rev"

// ORCHESTRATOR CARDINALITY

// KubeOwnerRefName is the tag for the Kubernetes owner reference name
KubeOwnerRefName = "kube_ownerref_name"
// OpenshiftDeployment is the tag for the OpenShift deployment name
OpenshiftDeployment = "oshift_deployment"
// TaskARN is the tag for the task ARN (Amazon Resource Name)
TaskARN = "task_arn"
// MesosTask is the tag for the Mesos task
MesosTask = "mesos_task"

// HIGH CARDINALITY

// ContainerName is the tag for the container name
ContainerName = "container_name"
// ContainerID is the tag for the container ID
ContainerID = "container_id"
// RancherContainer is the tag for the Rancher container name
RancherContainer = "rancher_container"
)
31 changes: 16 additions & 15 deletions pkg/collector/corechecks/cluster/ksm/kubernetes_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/comp/core/tagger/tags"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
Expand Down Expand Up @@ -541,16 +542,16 @@ func (k *KSMCheck) processMetrics(sender sender.Sender, metrics map[string][]ksm
if transform, found := k.metricTransformers[metricFamily.Name]; found {
lMapperOverride := labelsMapperOverride(metricFamily.Name)
for _, m := range metricFamily.ListMetrics {
hostname, tags := k.hostnameAndTags(m.Labels, labelJoiner, lMapperOverride)
transform(sender, metricFamily.Name, m, hostname, tags, now)
hostname, tagList := k.hostnameAndTags(m.Labels, labelJoiner, lMapperOverride)
transform(sender, metricFamily.Name, m, hostname, tagList, now)
}
continue
}
if ddname, found := k.metricNamesMapper[metricFamily.Name]; found {
lMapperOverride := labelsMapperOverride(metricFamily.Name)
for _, m := range metricFamily.ListMetrics {
hostname, tags := k.hostnameAndTags(m.Labels, labelJoiner, lMapperOverride)
sender.Gauge(ksmMetricPrefix+ddname, m.Val, hostname, tags)
hostname, tagList := k.hostnameAndTags(m.Labels, labelJoiner, lMapperOverride)
sender.Gauge(ksmMetricPrefix+ddname, m.Val, hostname, tagList)
}
continue
}
Expand Down Expand Up @@ -581,7 +582,7 @@ func (k *KSMCheck) hostnameAndTags(labels map[string]string, labelJoiner *labelJ
labelsToAdd := labelJoiner.getLabelsToAdd(labels)

// generate a dedicated tags slice
tags := make([]string, 0, len(labels)+len(labelsToAdd))
tagList := make([]string, 0, len(labels)+len(labelsToAdd))

ownerKind, ownerName := "", ""
for key, value := range labels {
Expand All @@ -592,7 +593,7 @@ func (k *KSMCheck) hostnameAndTags(labels map[string]string, labelJoiner *labelJ
ownerName = value
default:
tag, hostTag := k.buildTag(key, value, lMapperOverride)
tags = append(tags, tag)
tagList = append(tagList, tag)
if hostTag != "" {
if k.clusterNameRFC1123 != "" {
hostname = hostTag + "-" + k.clusterNameRFC1123
Expand All @@ -612,7 +613,7 @@ func (k *KSMCheck) hostnameAndTags(labels map[string]string, labelJoiner *labelJ
ownerName = label.value
default:
tag, hostTag := k.buildTag(label.key, label.value, lMapperOverride)
tags = append(tags, tag)
tagList = append(tagList, tag)
if hostTag != "" {
if k.clusterNameRFC1123 != "" {
hostname = hostTag + "-" + k.clusterNameRFC1123
Expand All @@ -624,10 +625,10 @@ func (k *KSMCheck) hostnameAndTags(labels map[string]string, labelJoiner *labelJ
}

if owners := ownerTags(ownerKind, ownerName); len(owners) != 0 {
tags = append(tags, owners...)
tagList = append(tagList, owners...)
}

return hostname, tags
return hostname, tagList
}

// familyFilter is a metric families filter for label joins
Expand Down Expand Up @@ -952,25 +953,25 @@ func ownerTags(kind, name string) []string {
return nil
}

tagKey, found := kubernetes.KindToTagName[kind]
if !found {
tagKey, err := tags.GetTagForKind(kind)
if err != nil {
return nil
}

tagFormat := "%s:%s"
tags := []string{fmt.Sprintf(tagFormat, tagKey, name)}
tagList := []string{fmt.Sprintf(tagFormat, tagKey, name)}
switch kind {
case kubernetes.JobKind:
if cronjob, _ := kubernetes.ParseCronJobForJob(name); cronjob != "" {
return append(tags, fmt.Sprintf(tagFormat, kubernetes.CronJobTagName, cronjob))
return append(tagList, fmt.Sprintf(tagFormat, tags.KubeCronjob, cronjob))
}
case kubernetes.ReplicaSetKind:
if deployment := kubernetes.ParseDeploymentForReplicaSet(name); deployment != "" {
return append(tags, fmt.Sprintf(tagFormat, kubernetes.DeploymentTagName, deployment))
return append(tagList, fmt.Sprintf(tagFormat, tags.KubeDeployment, deployment))
}
}

return tags
return tagList
}

// labelsMapperOverride allows overriding the default label mapping for
Expand Down
53 changes: 28 additions & 25 deletions pkg/collector/corechecks/cluster/ksm/kubernetes_state_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

package ksm

import "github.com/DataDog/datadog-agent/pkg/util/kubernetes"
import (
"github.com/DataDog/datadog-agent/comp/core/tagger/tags"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes"
)

// ksmMetricPrefix defines the KSM metrics namespace
const ksmMetricPrefix = "kubernetes_state."
Expand Down Expand Up @@ -94,38 +97,38 @@ func defaultMetricNamesMapper() map[string]string {
// defaultLabelsMapper returns a map that contains the default labels to tag names mapping
func defaultLabelsMapper() map[string]string {
return map[string]string{
"namespace": "kube_namespace",
"job_name": "kube_job",
"cronjob": "kube_cronjob",
"pod": "pod_name",
"priority_class": "kube_priority_class",
"daemonset": "kube_daemon_set",
"replicationcontroller": "kube_replication_controller",
"replicaset": "kube_replica_set",
"statefulset": "kube_stateful_set",
"deployment": "kube_deployment",
"namespace": tags.KubeNamespace,
"job_name": tags.KubeJob,
"cronjob": tags.KubeCronjob,
"pod": tags.KubePod,
"priority_class": tags.KubePriorityClass,
"daemonset": tags.KubeDaemonSet,
"replicationcontroller": tags.KubeReplicationController,
"replicaset": tags.KubeReplicaSet,
"statefulset": tags.KubeStatefulSet,
"deployment": tags.KubeDeployment,
"container": tags.KubeContainerName,
"container_id": tags.ContainerID,
"image": tags.ImageName,
"endpoint": "kube_endpoint",
"container": "kube_container_name",
"container_id": "container_id",
"image": "image_name",
"label_topology_kubernetes_io_region": "kube_region",
"label_topology_kubernetes_io_zone": "kube_zone",
"label_failure_domain_beta_kubernetes_io_region": "kube_region",
"label_failure_domain_beta_kubernetes_io_zone": "kube_zone",
"ingress": "kube_ingress",

// Standard Datadog labels
"label_tags_datadoghq_com_env": "env",
"label_tags_datadoghq_com_service": "service",
"label_tags_datadoghq_com_version": "version",
"label_tags_datadoghq_com_env": tags.Env,
"label_tags_datadoghq_com_service": tags.Service,
"label_tags_datadoghq_com_version": tags.Version,

// Standard Kubernetes labels
"label_app_kubernetes_io_name": "kube_app_name",
"label_app_kubernetes_io_instance": "kube_app_instance",
"label_app_kubernetes_io_version": "kube_app_version",
"label_app_kubernetes_io_component": "kube_app_component",
"label_app_kubernetes_io_part_of": "kube_app_part_of",
"label_app_kubernetes_io_managed_by": "kube_app_managed_by",
"label_app_kubernetes_io_name": tags.KubeAppName,
"label_app_kubernetes_io_instance": tags.KubeAppInstance,
"label_app_kubernetes_io_version": tags.KubeAppVersion,
"label_app_kubernetes_io_component": tags.KubeAppComponent,
"label_app_kubernetes_io_part_of": tags.KubeAppPartOf,
"label_app_kubernetes_io_managed_by": tags.KubeAppManagedBy,

// Standard Helm labels
"label_helm_sh_chart": "helm_chart",
Expand Down Expand Up @@ -241,7 +244,7 @@ func getLabelToMatchForKind(kind string) []string {

func defaultAnnotationsAsTags() map[string]map[string]string {
return map[string]map[string]string{
"pod": {kubernetes.RcIDAnnotKey: kubernetes.RcIDTagName, kubernetes.RcRevisionAnnotKey: kubernetes.RcRevisionTagName},
"deployment": {kubernetes.RcIDAnnotKey: kubernetes.RcIDTagName, kubernetes.RcRevisionAnnotKey: kubernetes.RcRevisionTagName},
"pod": {kubernetes.RcIDAnnotKey: tags.RemoteConfigID, kubernetes.RcRevisionAnnotKey: tags.RemoteConfigRevision},
"deployment": {kubernetes.RcIDAnnotKey: tags.RemoteConfigID, kubernetes.RcRevisionAnnotKey: tags.RemoteConfigRevision},
}
}
Loading
Loading