Skip to content

Commit

Permalink
ddtracer/tracer: set hostname meta tag on each span (#839)
Browse files Browse the repository at this point in the history
Added DD_TRACE_SOURCE_HOSTNAME variable read to allow setting the hostname manually by the user. Hostname is added to each span as a meta tag of `_dd.hostname`
  • Loading branch information
dianashevchenko committed Feb 12, 2021
1 parent 45db01a commit e869dbd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
10 changes: 10 additions & 0 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func newConfig(opts ...StartOption) *config {
log.Warn("unable to look up hostname: %v", err)
}
}
if v := os.Getenv("DD_TRACE_SOURCE_HOSTNAME"); v != "" {
c.hostname = v
}
if v := os.Getenv("DD_ENV"); v != "" {
c.env = v
}
Expand Down Expand Up @@ -433,6 +436,13 @@ func WithServiceVersion(version string) StartOption {
}
}

// WithHostname allows specifying the hostname with which to mark outgoing traces.
func WithHostname(name string) StartOption {
return func(c *config) {
c.hostname = name
}
}

// StartSpanOption is a configuration option for StartSpan. It is aliased in order
// to help godoc group all the functions returning it together. It is considered
// more correct to refer to it as the type as the origin, ddtrace.StartSpanOption.
Expand Down
25 changes: 25 additions & 0 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,28 @@ func TestGlobalTag(t *testing.T) {
WithGlobalTag("k", "v")(&c)
assert.Contains(t, statsTags(&c), "k:v")
}

func TestWithHostname(t *testing.T) {
t.Run("WithHostname", func(t *testing.T) {
assert := assert.New(t)
c := newConfig(WithHostname("hostname"))
assert.Equal("hostname", c.hostname)
})

t.Run("env", func(t *testing.T) {
assert := assert.New(t)
os.Setenv("DD_TRACE_SOURCE_HOSTNAME", "hostname-env")
defer os.Unsetenv("DD_TRACE_SOURCE_HOSTNAME")
c := newConfig()
assert.Equal("hostname-env", c.hostname)
})

t.Run("env-override", func(t *testing.T) {
assert := assert.New(t)

os.Setenv("DD_TRACE_SOURCE_HOSTNAME", "hostname-env")
defer os.Unsetenv("DD_TRACE_SOURCE_HOSTNAME")
c := newConfig(WithHostname("hostname-middleware"))
assert.Equal("hostname-middleware", c.hostname)
})
}
6 changes: 3 additions & 3 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
taskEnd: startExecutionTracerTask(operationName),
noDebugStack: t.config.noDebugStack,
}
if t.hostname != "" {
span.setMeta(keyHostname, t.hostname)
}
if context != nil {
// this is a child span
span.TraceID = context.traceID
Expand All @@ -289,9 +292,6 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
if context == nil || context.span == nil {
// this is either a root span or it has a remote parent, we should add the PID.
span.setMeta(ext.Pid, t.pid)
if t.hostname != "" {
span.setMeta(keyHostname, t.hostname)
}
if _, ok := opts.Tags[ext.ServiceName]; !ok && t.config.runtimeMetrics {
// this is a root span in the global service; runtime metrics should
// be linked to it:
Expand Down
67 changes: 65 additions & 2 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,9 @@ func TestTracerFlush(t *testing.T) {
}

func TestTracerReportsHostname(t *testing.T) {
t.Run("enabled", func(t *testing.T) {
const hostname = "hostname-test"

t.Run("DD_TRACE_REPORT_HOSTNAME/set", func(t *testing.T) {
os.Setenv("DD_TRACE_REPORT_HOSTNAME", "true")
defer os.Unsetenv("DD_TRACE_REPORT_HOSTNAME")

Expand All @@ -1038,11 +1040,72 @@ func TestTracerReportsHostname(t *testing.T) {
assert.True(ok)
assert.Equal(name, tracer.hostname)

name, ok = child.Meta[keyHostname]
assert.True(ok)
assert.Equal(name, tracer.hostname)
})

t.Run("DD_TRACE_REPORT_HOSTNAME/unset", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t)
defer stop()

root := tracer.StartSpan("root").(*span)
child := tracer.StartSpan("child", ChildOf(root.Context())).(*span)
child.Finish()
root.Finish()

assert := assert.New(t)

_, ok := root.Meta[keyHostname]
assert.False(ok)
_, ok = child.Meta[keyHostname]
assert.False(ok)
})

t.Run("disabled", func(t *testing.T) {
t.Run("WithHostname", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t, WithHostname(hostname))
defer stop()

root := tracer.StartSpan("root").(*span)
child := tracer.StartSpan("child", ChildOf(root.Context())).(*span)
child.Finish()
root.Finish()

assert := assert.New(t)

got, ok := root.Meta[keyHostname]
assert.True(ok)
assert.Equal(got, hostname)

got, ok = child.Meta[keyHostname]
assert.True(ok)
assert.Equal(got, hostname)
})

t.Run("DD_TRACE_SOURCE_HOSTNAME/set", func(t *testing.T) {
os.Setenv("DD_TRACE_SOURCE_HOSTNAME", "hostname-test")
defer os.Unsetenv("DD_TRACE_SOURCE_HOSTNAME")

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

root := tracer.StartSpan("root").(*span)
child := tracer.StartSpan("child", ChildOf(root.Context())).(*span)
child.Finish()
root.Finish()

assert := assert.New(t)

got, ok := root.Meta[keyHostname]
assert.True(ok)
assert.Equal(got, hostname)

got, ok = child.Meta[keyHostname]
assert.True(ok)
assert.Equal(got, hostname)
})

t.Run("DD_TRACE_SOURCE_HOSTNAME/unset", func(t *testing.T) {
tracer, _, _, stop := startTestTracer(t)
defer stop()

Expand Down

0 comments on commit e869dbd

Please sign in to comment.