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 3 commits
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
18 changes: 17 additions & 1 deletion ddtrace/tracer/option.go
Expand Up @@ -66,6 +66,10 @@ type config struct {
// serviceName specifies the name of this application.
serviceName string

// universalVersion, reports whether span service name and config service name
// should match to set application version tag. False by default
universalVersion bool

// version specifies the version of this application
version string

Expand Down Expand Up @@ -656,10 +660,22 @@ func WithSamplingRules(rules []SamplingRule) StartOption {
}

// WithServiceVersion specifies the version of the service that is running. This will
// be included in spans from this service in the "version" tag.
// be included in spans from this service in the "version" tag, provided that
// span service name and config service name match. Do NOT use with WithUniversalVersion.
func WithServiceVersion(version string) StartOption {
return func(cfg *config) {
cfg.version = version
cfg.universalVersion = false
}
}

// WithUniversalVersion specifies the version of the service that is running, and will be applied to all spans,
// regardless of whether span service name and config service name match.
// See: WithService, WithServiceVersion. Do NOT use with WithServiceVersion.
func WithUniversalVersion(version string) StartOption {
return func(c *config) {
c.version = version
c.universalVersion = true
}
}

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.universalVersion || (!t.config.universalVersion && 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
35 changes: 33 additions & 2 deletions ddtrace/tracer/tracer_test.go
Expand Up @@ -1390,9 +1390,40 @@ func TestVersion(t *testing.T) {
v := sp.Meta[ext.Version]
assert.Equal("4.5.6", v)
})
t.Run("service", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithServiceVersion("4.5.6"),
WithService("servenv"))
defer stop()

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

assert := assert.New(t)
sp := tracer.StartSpan("http.request", ServiceName("otherservenv")).(*span)
v, ok := sp.Meta[ext.Version]
assert.True(ok)
assert.Equal("4.5.6", v)
})
t.Run("service/universal", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithServiceVersion("4.5.6"),
WithService("servenv"), WithUniversalVersion("1.2.3"))
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request", ServiceName("otherservenv")).(*span)
v, ok := sp.Meta[ext.Version]
assert.True(ok)
assert.Equal("1.2.3", v)
})
t.Run("universal/service", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithUniversalVersion("1.2.3"),
WithServiceVersion("4.5.6"), WithService("servenv"))
defer stop()

assert := assert.New(t)
Expand Down