Skip to content

Commit

Permalink
ddtrace/tracer: add hostname tag to root spans (#437)
Browse files Browse the repository at this point in the history
This functionality is controlled by the DD_TRACE_REPORT_HOSTNAME
environment variable. When set to "true", the root spans will
automatically have a tag added containing the hostname.
  • Loading branch information
cgilmour authored and gbbr committed May 21, 2019
1 parent 76b2a63 commit c19e9e5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
15 changes: 14 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracer

import (
"log"
"net/http"
"os"
"path/filepath"
Expand All @@ -22,7 +23,7 @@ type config struct {
// sampler specifies the sampler that will be used for sampling traces.
sampler Sampler

// agentAddr specifies the hostname and of the agent where the traces
// agentAddr specifies the hostname and port of the agent where the traces
// are sent to.
agentAddr string

Expand All @@ -38,6 +39,10 @@ type config struct {

// httpRoundTripper defines the http.RoundTripper used by the agent transport.
httpRoundTripper http.RoundTripper

// hostname is automatically assigned when the DD_TRACE_REPORT_HOSTNAME is set to true,
// and is added as a special tag to the root span of traces.
hostname string
}

// StartOption represents a function that can be provided as a parameter to Start.
Expand All @@ -48,6 +53,14 @@ func defaults(c *config) {
c.serviceName = filepath.Base(os.Args[0])
c.sampler = NewAllSampler()
c.agentAddr = defaultAddress

if os.Getenv("DD_TRACE_REPORT_HOSTNAME") == "true" {
var err error
c.hostname, err = os.Hostname()
if err != nil {
log.Printf("%sunable to look up hostname: %v\n", errorPrefix, err)
}
}
}

// WithPrioritySampling is deprecated, and priority sampling is enabled by default.
Expand Down
1 change: 1 addition & 0 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,5 @@ const (
keySamplingPriority = "_sampling_priority_v1"
keySamplingPriorityRate = "_sampling_priority_rate_v1"
keyOrigin = "_dd.origin"
keyHostname = "_dd.hostname"
)
3 changes: 3 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ 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.SetTag(ext.Pid, t.pid)
if t.hostname != "" {
span.SetTag(keyHostname, t.hostname)
}
}
// add tags from options
for k, v := range opts.Tags {
Expand Down
41 changes: 41 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,47 @@ func TestTracerFlush(t *testing.T) {
})
}

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

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

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

assert := assert.New(t)

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

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

t.Run("disabled", func(t *testing.T) {
tracer, _, stop := startTestTracer()
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)
})
}

// 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

0 comments on commit c19e9e5

Please sign in to comment.