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 service version to tracer. #607

Merged
merged 13 commits into from
Mar 18, 2020
1 change: 1 addition & 0 deletions contrib/google.golang.org/grpc/stats_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestClientStatsHandler(t *testing.T) {
"span.type": ext.AppTypeRPC,
"grpc.code": codes.OK.String(),
"service.name": serviceName,
"service": serviceName,
"resource.name": "/grpc.Fixture/Ping",
tagMethodName: "/grpc.Fixture/Ping",
ext.TargetHost: "127.0.0.1",
Expand Down
1 change: 1 addition & 0 deletions contrib/google.golang.org/grpc/stats_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestServerStatsHandler(t *testing.T) {
"span.type": ext.AppTypeRPC,
"grpc.code": codes.OK.String(),
"service.name": serviceName,
"service": serviceName,
"resource.name": "/grpc.Fixture/Ping",
tagMethodName: "/grpc.Fixture/Ping",
}, span.Tags())
Expand Down
6 changes: 6 additions & 0 deletions ddtrace/ext/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const (
// ServiceName defines the Service name for this Span.
ServiceName = "service.name"

// Service defines the Service tag for this Span.
Service = "service"

// Version is a tag that specifies the current application version.
Version = "version"

// ResourceName defines the Resource name for the Span.
ResourceName = "resource.name"

Expand Down
16 changes: 15 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type config struct {
// serviceName specifies the name of this application.
serviceName string

// service specifies the service tag set with DD_SERVICE that will be sent with all traces.
service string

// version specifies the version of this application
version string

// sampler specifies the sampler that will be used for sampling traces.
sampler Sampler

Expand Down Expand Up @@ -107,6 +113,10 @@ func defaults(c *config) {
}
if v := os.Getenv("DD_SERVICE"); v != "" {
c.serviceName = v
c.service = v
if ver := os.Getenv("DD_VERSION"); ver != "" {
gbbr marked this conversation as resolved.
Show resolved Hide resolved
c.version = ver
}
} else {
c.serviceName = filepath.Base(os.Args[0])
}
Expand Down Expand Up @@ -190,6 +200,7 @@ func WithServiceName(name string) StartOption {
func WithService(name string) StartOption {
return func(c *config) {
c.serviceName = name
WithGlobalTag(ext.Service, name)(c)
}
}

Expand Down Expand Up @@ -300,7 +311,10 @@ func Tag(k string, v interface{}) StartSpanOption {

// ServiceName sets the given service name on the started span. For example "http.server".
func ServiceName(name string) StartSpanOption {
return Tag(ext.ServiceName, name)
return func(cfg *ddtrace.StartSpanConfig) {
Tag(ext.ServiceName, name)(cfg)
Tag(ext.Service, name)(cfg)
}
}

// ResourceName sets the given resource name on the started span. A resource could
Expand Down
6 changes: 6 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
span.setMeta("language", "go")
}
}
if t.config.service != "" {
span.SetTag(ext.Service, t.config.service)
}
// add tags from options
for k, v := range opts.Tags {
span.SetTag(k, v)
Expand All @@ -321,6 +324,9 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
for k, v := range t.config.globalTags {
span.SetTag(k, v)
}
if t.config.version != "" && span.Meta[ext.Service] == t.config.service {
span.SetTag(ext.Version, t.config.version)
}
if context == nil {
// this is a brand new trace, sample it
t.sample(span)
Expand Down
102 changes: 102 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,108 @@ func TestTracerReportsHostname(t *testing.T) {
})
}

func TestEnvTags(t *testing.T) {
t.Run("service", func(t *testing.T) {
os.Setenv("DD_SERVICE", "servenv")
defer os.Unsetenv("DD_SERVICE")

tracer, _, _, stop := startTestTracer(t)
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request").(*span)
assert.Equal("servenv", sp.Meta[ext.Service])
})
t.Run("version", func(t *testing.T) {
os.Setenv("DD_SERVICE", "servenv")
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")
defer os.Unsetenv("DD_SERVICE")

tracer, _, _, stop := startTestTracer(t)
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request").(*span)
assert.Equal("1.2.3", sp.Meta[ext.Version])
})
t.Run("noversion", func(t *testing.T) {
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")

tracer, _, _, stop := startTestTracer(t)
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request").(*span)
_, ok := sp.Meta[ext.Version]
assert.False(ok)
})
t.Run("noversion2", func(t *testing.T) {
knusbaum marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")

tracer, _, _, stop := startTestTracer(t, WithGlobalTag(ext.Service, "otherservenv"))
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request", ServiceName("otherservenv")).(*span)
_, ok := sp.Meta[ext.Version]
assert.False(ok)
})
t.Run("serviceOverride", func(t *testing.T) {
os.Setenv("DD_SERVICE", "servenv")
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")
knusbaum marked this conversation as resolved.
Show resolved Hide resolved
defer os.Unsetenv("DD_SERVICE")

tracer, _, _, stop := startTestTracer(t, WithGlobalTag(ext.Service, "otherservenv"))
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request").(*span)
assert.Equal("otherservenv", sp.Meta[ext.Service])
_, ok := sp.Meta[ext.Version]
assert.False(ok)
})
t.Run("serviceOverride2", func(t *testing.T) {
os.Setenv("DD_SERVICE", "servenv")
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")
defer os.Unsetenv("DD_SERVICE")

tracer, _, _, stop := startTestTracer(t, WithService("otherservenv"))
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request").(*span)
assert.Equal("otherservenv", sp.Meta[ext.Service])
_, ok := sp.Meta[ext.Version]
assert.False(ok)
})
knusbaum marked this conversation as resolved.
Show resolved Hide resolved
t.Run("serviceOverride3", func(t *testing.T) {
os.Setenv("DD_SERVICE", "servenv")
os.Setenv("DD_VERSION", "1.2.3")
defer os.Unsetenv("DD_VERSION")
defer os.Unsetenv("DD_SERVICE")

tracer, _, _, stop := startTestTracer(t)
defer stop()

assert := assert.New(t)
sp := tracer.StartSpan("http.request", ServiceName("otherservenv")).(*span)
sp2 := tracer.StartSpan("http.request", Tag(ext.Service, "otherservenv")).(*span)

assert.Equal("otherservenv", sp.Meta[ext.Service])
_, ok := sp.Meta[ext.Version]
assert.False(ok)

assert.Equal("otherservenv", sp2.Meta[ext.Service])
_, ok = sp2.Meta[ext.Version]
assert.False(ok)
})
}

// BenchmarkConcurrentTracing tests the performance of spawning a lot of
// goroutines where each one creates a trace with a parent and a child.
func BenchmarkConcurrentTracing(b *testing.B) {
Expand Down