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

ddtrace/tracer: add WithUniversalVersion option #1272

Merged
merged 4 commits into from May 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions ddtrace/tracer/option.go
Expand Up @@ -66,6 +66,10 @@ type config struct {
// serviceName specifies the name of this application.
serviceName string

// serviceNameMatch, reports whether span service name and config service name
// should match to set application version tag. Defaults to true
serviceNameMatch bool

// version specifies the version of this application
version string

Expand Down Expand Up @@ -179,6 +183,7 @@ func newConfig(opts ...StartOption) *config {
c.sampler = NewAllSampler()
c.agentAddr = resolveAgentAddr()
c.httpClient = defaultHTTPClient()
c.serviceNameMatch = true

if internal.BoolEnv("DD_TRACE_ANALYTICS_ENABLED", false) {
globalconfig.SetAnalyticsRate(1.0)
Expand Down Expand Up @@ -532,6 +537,14 @@ func WithServiceName(name string) StartOption {
}
}

// WithServiceNameMatch allows specifying whether span service name and config service name
// should match to set application version tag
dianashevchenko marked this conversation as resolved.
Show resolved Hide resolved
func WithServiceNameMatch(shouldMatch bool) StartOption {
dianashevchenko marked this conversation as resolved.
Show resolved Hide resolved
return func(c *config) {
c.serviceNameMatch = shouldMatch
}
}

// WithService sets the default service name for the program.
func WithService(name string) StartOption {
return func(c *config) {
Expand Down
6 changes: 4 additions & 2 deletions ddtrace/tracer/tracer.go
Expand Up @@ -421,8 +421,10 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
// all top level spans are measured. So the measured tag is redundant.
delete(span.Metrics, keyMeasured)
}
if t.config.version != "" && span.Service == t.config.serviceName {
span.setMeta(ext.Version, t.config.version)
if t.config.version != "" {
if !t.config.serviceNameMatch || (t.config.serviceNameMatch && span.Service == t.config.serviceName) {
span.setMeta(ext.Version, t.config.version)
}
}
if t.config.env != "" {
span.setMeta(ext.Environment, t.config.env)
Expand Down
15 changes: 13 additions & 2 deletions ddtrace/tracer/tracer_test.go
Expand Up @@ -1391,8 +1391,19 @@ func TestVersion(t *testing.T) {
assert.Equal("4.5.6", v)
})

t.Run("unset", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithServiceVersion("4.5.6"), WithService("servenv"))
t.Run("unset/match-disabled", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithServiceVersion("4.5.6"),
WithService("servenv"), WithServiceNameMatch(false))
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request", ServiceName("otherservenv")).(*span)
_, ok := sp.Meta[ext.Version]
assert.True(ok)
})
t.Run("unset/match-enabled", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithServiceVersion("4.5.6"),
WithService("servenv"), WithServiceNameMatch(true))
defer stop()

assert := assert.New(t)
Expand Down