Skip to content

Commit

Permalink
ddtrace/tracer: support default origin on dynamic config to support A…
Browse files Browse the repository at this point in the history
…ctive Tracing telemetry spec (#2623)
  • Loading branch information
darccio authored May 30, 2024
1 parent 9126b4f commit 4e98120
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 60 deletions.
9 changes: 5 additions & 4 deletions ddtrace/tracer/dynamic_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type dynamicConfig[T any] struct {
current T // holds the current configuration value
startup T // holds the startup configuration value
cfgName string // holds the name of the configuration, has to be compatible with telemetry.Configuration.Name
cfgOrigin string // holds the origin of the current configuration value (currently only supports remote_config, empty otherwise)
cfgOrigin telemetry.Origin // holds the origin of the current configuration value (currently only supports remote_config, empty otherwise)
apply func(T) bool // executes any config-specific operations to propagate the update properly, returns whether the update was applied
equal func(x, y T) bool // compares two configuration values, this is used to avoid unnecessary config and telemetry updates
}
Expand All @@ -42,7 +42,7 @@ func (dc *dynamicConfig[T]) get() T {
}

// update applies a new configuration value
func (dc *dynamicConfig[T]) update(val T, origin string) bool {
func (dc *dynamicConfig[T]) update(val T, origin telemetry.Origin) bool {
dc.Lock()
defer dc.Unlock()
if dc.equal(dc.current, val) {
Expand All @@ -61,15 +61,16 @@ func (dc *dynamicConfig[T]) reset() bool {
return false
}
dc.current = dc.startup
dc.cfgOrigin = ""
// TODO: set the origin to the startup value's origin
dc.cfgOrigin = telemetry.OriginDefault
return dc.apply(dc.startup)
}

// handleRC processes a new configuration value from remote config
// Returns whether the configuration value has been updated or not
func (dc *dynamicConfig[T]) handleRC(val *T) bool {
if val != nil {
return dc.update(*val, "remote_config")
return dc.update(*val, telemetry.OriginRemoteConfig)
}
return dc.reset()
}
Expand Down
20 changes: 15 additions & 5 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema"
"gopkg.in/DataDog/dd-trace-go.v1/internal/normalizer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"gopkg.in/DataDog/dd-trace-go.v1/internal/traceprof"
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"

Expand Down Expand Up @@ -336,14 +337,18 @@ func newConfig(opts ...StartOption) *config {
}
c.headerAsTags = newDynamicConfig("trace_header_tags", nil, setHeaderTags, equalSlice[string])
if v := os.Getenv("DD_TRACE_HEADER_TAGS"); v != "" {
WithHeaderTags(strings.Split(v, ","))(c)
c.headerAsTags.update(strings.Split(v, ","), telemetry.OriginEnvVar)
// Required to ensure that the startup header tags are set on reset.
c.headerAsTags.startup = c.headerAsTags.current
}
if v := os.Getenv("DD_TAGS"); v != "" {
tags := internal.ParseTagString(v)
internal.CleanGitMetadataTags(tags)
for key, val := range tags {
WithGlobalTag(key, val)(c)
}
// TODO: should we track the origin of these tags individually?
c.globalTags.cfgOrigin = telemetry.OriginEnvVar
}
if _, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); ok {
// AWS_LAMBDA_FUNCTION_NAME being set indicates that we're running in an AWS Lambda environment.
Expand All @@ -354,6 +359,9 @@ func newConfig(opts ...StartOption) *config {
c.runtimeMetrics = internal.BoolEnv("DD_RUNTIME_METRICS_ENABLED", false)
c.debug = internal.BoolEnv("DD_TRACE_DEBUG", false)
c.enabled = newDynamicConfig("tracing_enabled", internal.BoolEnv("DD_TRACE_ENABLED", true), func(b bool) bool { return true }, equal[bool])
if _, ok := os.LookupEnv("DD_TRACE_ENABLED"); ok {
c.enabled.cfgOrigin = telemetry.OriginEnvVar
}
c.profilerEndpoints = internal.BoolEnv(traceprof.EndpointEnvVar, true)
c.profilerHotspots = internal.BoolEnv(traceprof.CodeHotspotsEnvVar, true)
c.enableHostnameDetection = internal.BoolEnv("DD_CLIENT_HOSTNAME_ENABLED", true)
Expand Down Expand Up @@ -509,7 +517,8 @@ func newConfig(opts ...StartOption) *config {
}
// Re-initialize the globalTags config with the value constructed from the environment and start options
// This allows persisting the initial value of globalTags for future resets and updates.
c.initGlobalTags(c.globalTags.get())
globalTagsOrigin := c.globalTags.cfgOrigin
c.initGlobalTags(c.globalTags.get(), globalTagsOrigin)

return c
}
Expand Down Expand Up @@ -898,7 +907,7 @@ func WithPeerServiceMapping(from, to string) StartOption {
func WithGlobalTag(k string, v interface{}) StartOption {
return func(c *config) {
if c.globalTags.get() == nil {
c.initGlobalTags(map[string]interface{}{})
c.initGlobalTags(map[string]interface{}{}, telemetry.OriginDefault)
}
c.globalTags.Lock()
defer c.globalTags.Unlock()
Expand All @@ -907,13 +916,14 @@ func WithGlobalTag(k string, v interface{}) StartOption {
}

// initGlobalTags initializes the globalTags config with the provided init value
func (c *config) initGlobalTags(init map[string]interface{}) {
func (c *config) initGlobalTags(init map[string]interface{}, origin telemetry.Origin) {
apply := func(map[string]interface{}) bool {
// always set the runtime ID on updates
c.globalTags.current[ext.RuntimeID] = globalconfig.RuntimeID()
return true
}
c.globalTags = newDynamicConfig[map[string]interface{}]("trace_tags", init, apply, equalMap[string])
c.globalTags = newDynamicConfig("trace_tags", init, apply, equalMap[string])
c.globalTags.cfgOrigin = origin
}

// WithSampler sets the given sampler to be used with the tracer. By default
Expand Down
3 changes: 3 additions & 0 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"gopkg.in/DataDog/dd-trace-go.v1/internal/traceprof"
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"

Expand Down Expand Up @@ -548,6 +549,7 @@ func TestTracerOptionsDefaults(t *testing.T) {
defer tracer.Stop()
c := tracer.config
assert.True(t, c.enabled.current)
assert.Equal(t, c.enabled.cfgOrigin, telemetry.OriginDefault)
})

t.Run("override", func(t *testing.T) {
Expand All @@ -556,6 +558,7 @@ func TestTracerOptionsDefaults(t *testing.T) {
defer tracer.Stop()
c := tracer.config
assert.False(t, c.enabled.current)
assert.Equal(t, c.enabled.cfgOrigin, telemetry.OriginEnvVar)
})
})

Expand Down
Loading

0 comments on commit 4e98120

Please sign in to comment.