Skip to content

Commit

Permalink
ddtrace/tracer: add WithServiceNameMatch option (#1272)
Browse files Browse the repository at this point in the history
* ddtrace/tracer: add WithServiceNameMatch option
Removed limitation of having service name match the name defined when starting the tracer for version tracking.
  • Loading branch information
dianashevchenko committed May 3, 2022
1 parent 20d5e0c commit 0b70675
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
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 @@ -659,10 +663,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

0 comments on commit 0b70675

Please sign in to comment.