Skip to content

Commit

Permalink
chore: Add config comp for static tags (#32085)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedos authored Dec 13, 2024
1 parent 8a25c9c commit 81ca6f9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion comp/core/tagger/collectors/workloadmeta_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *WorkloadMetaCollector) Run(ctx context.Context, datadogConfig config.Co
}

func (c *WorkloadMetaCollector) collectStaticGlobalTags(ctx context.Context, datadogConfig config.Component) {
c.staticTags = util.GetStaticTags(ctx)
c.staticTags = util.GetStaticTags(ctx, datadogConfig)
if _, exists := c.staticTags[clusterTagNamePrefix]; flavor.GetFlavor() == flavor.ClusterAgent && !exists {
// If we are running the cluster agent, we want to set the kube_cluster_name tag as a global tag if we are able
// to read it, for the instances where we are running in an environment where hostname cannot be detected.
Expand Down
2 changes: 1 addition & 1 deletion comp/dogstatsd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func newServerCompat(cfg model.Reader, log log.Component, capture replay.Compone

// if the server is running in a context where static tags are required, add those
// to extraTags.
if staticTags := util.GetStaticTagsSlice(context.TODO()); staticTags != nil {
if staticTags := util.GetStaticTagsSlice(context.TODO(), cfg); staticTags != nil {
extraTags = append(extraTags, staticTags...)
}
util.SortUniqInPlace(extraTags)
Expand Down
5 changes: 3 additions & 2 deletions comp/otelcol/otlp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (

"go.uber.org/multierr"

"github.com/go-viper/mapstructure/v2"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/exporter/serializerexporter"
"github.com/DataDog/datadog-agent/comp/otelcol/otlp/configcheck"
coreconfig "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/util"
"github.com/go-viper/mapstructure/v2"
)

func portToUint(v int) (port uint, err error) {
Expand Down Expand Up @@ -51,7 +52,7 @@ func FromAgentConfig(cfg config.Reader) (PipelineConfig, error) {
metricsConfigMap["apm_stats_receiver_addr"] = fmt.Sprintf("http://localhost:%s/v0.6/stats", coreconfig.Datadog().GetString("apm_config.receiver_port"))
}

tags := strings.Join(util.GetStaticTagsSlice(context.TODO()), ",")
tags := strings.Join(util.GetStaticTagsSlice(context.TODO(), cfg), ",")
if tags != "" {
metricsConfigMap["tags"] = tags
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/util/static_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"context"
"strings"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/pkg/config/env"
"github.com/DataDog/datadog-agent/pkg/config/model"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
configUtils "github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/util/fargate"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
Expand All @@ -24,7 +23,7 @@ import (
// included in host tags. In environments with no host metadata (such as where
// the hostname is empty), tags that would otherwise be included in host
// metadata are generated by this function.
func GetStaticTagsSlice(ctx context.Context) []string {
func GetStaticTagsSlice(ctx context.Context, datadogConfig config.Reader) []string {
// fargate (ECS or EKS) does not have host tags, so we need to
// add static tags to each container manually

Expand All @@ -35,7 +34,7 @@ func GetStaticTagsSlice(ctx context.Context) []string {
tags := []string{}

// DD_TAGS / DD_EXTRA_TAGS
tags = append(tags, configUtils.GetConfiguredTags(pkgconfigsetup.Datadog(), false)...)
tags = append(tags, configUtils.GetConfiguredTags(datadogConfig, false)...)

// EKS Fargate specific tags
if env.IsFeaturePresent(env.EKSFargate) {
Expand Down Expand Up @@ -74,8 +73,8 @@ func GetStaticTagsSlice(ctx context.Context) []string {

// GetStaticTags is similar to GetStaticTagsSlice, but returning a map[string][]string containing
// <key>:<value> pairs for tags. Tags not matching this pattern are omitted.
func GetStaticTags(ctx context.Context) map[string][]string {
tags := GetStaticTagsSlice(ctx)
func GetStaticTags(ctx context.Context, datadogConfig config.Component) map[string][]string {
tags := GetStaticTagsSlice(ctx, datadogConfig)
if tags == nil {
return nil
}
Expand All @@ -85,7 +84,7 @@ func GetStaticTags(ctx context.Context) map[string][]string {
// GetGlobalEnvTags is similar to GetStaticTags, but returning a map[string][]string containing
// <key>:<value> pairs for all global environment tags on the cluster agent. This includes:
// DD_TAGS, DD_EXTRA_TAGS, DD_CLUSTER_CHECKS_EXTRA_TAGS, and DD_ORCHESTRATOR_EXPLORER_EXTRA_TAGS
func GetGlobalEnvTags(config model.Reader) map[string][]string {
func GetGlobalEnvTags(config config.Reader) map[string][]string {
if flavor.GetFlavor() != flavor.ClusterAgent {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/static_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestStaticTags(t *testing.T) {
t.Run("just tags", func(t *testing.T) {
mockConfig.SetWithoutSource("tags", []string{"some:tag", "another:tag", "nocolon"})
defer mockConfig.SetWithoutSource("tags", []string{})
staticTags := GetStaticTags(context.Background())
staticTags := GetStaticTags(context.Background(), mockConfig)
assert.Equal(t, map[string][]string{
"some": {"tag"},
"another": {"tag"},
Expand All @@ -39,7 +39,7 @@ func TestStaticTags(t *testing.T) {
mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"})
defer mockConfig.SetWithoutSource("tags", []string{})
defer mockConfig.SetWithoutSource("extra_tags", []string{})
staticTags := GetStaticTags(context.Background())
staticTags := GetStaticTags(context.Background(), mockConfig)
assert.Equal(t, map[string][]string{
"some": {"tag"},
"extra": {"tag"},
Expand All @@ -50,7 +50,7 @@ func TestStaticTags(t *testing.T) {
t.Run("cluster name already set", func(t *testing.T) {
mockConfig.SetWithoutSource("tags", []string{"kube_cluster_name:foo"})
defer mockConfig.SetWithoutSource("tags", []string{})
staticTags := GetStaticTags(context.Background())
staticTags := GetStaticTags(context.Background(), mockConfig)
assert.Equal(t, map[string][]string{
"eks_fargate_node": {"eksnode"},
"kube_cluster_name": {"foo"},
Expand All @@ -68,7 +68,7 @@ func TestStaticTagsSlice(t *testing.T) {
t.Run("just tags", func(t *testing.T) {
mockConfig.SetWithoutSource("tags", []string{"some:tag", "another:tag", "nocolon"})
defer mockConfig.SetWithoutSource("tags", []string{})
staticTags := GetStaticTagsSlice(context.Background())
staticTags := GetStaticTagsSlice(context.Background(), mockConfig)
assert.ElementsMatch(t, []string{
"nocolon",
"some:tag",
Expand All @@ -82,7 +82,7 @@ func TestStaticTagsSlice(t *testing.T) {
mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"})
defer mockConfig.SetWithoutSource("tags", []string{})
defer mockConfig.SetWithoutSource("extra_tags", []string{})
staticTags := GetStaticTagsSlice(context.Background())
staticTags := GetStaticTagsSlice(context.Background(), mockConfig)
assert.ElementsMatch(t, []string{
"nocolon",
"missingcolon",
Expand Down

0 comments on commit 81ca6f9

Please sign in to comment.