Skip to content

Commit

Permalink
ddtrace/tracer: Memoize the PID (#438)
Browse files Browse the repository at this point in the history
The os.Getpid() call is visible in profile flamegraphs of busy servers.
Memoize it as the PID won't change in the lifetime of a Go process.
  • Loading branch information
CAFxX authored and gbbr committed May 16, 2019
1 parent eed4d38 commit f3fe6e5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type tracer struct {

// prioritySampling holds an instance of the priority sampler.
prioritySampling *prioritySampler
// pid of the process
pid string
}

const (
Expand Down Expand Up @@ -131,6 +133,7 @@ func newTracer(opts ...StartOption) *tracer {
errorBuffer: make(chan error, errorBufferSize),
stopped: make(chan struct{}),
prioritySampling: newPrioritySampler(),
pid: strconv.Itoa(os.Getpid()),
}

go t.worker()
Expand Down Expand Up @@ -269,7 +272,7 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
span.context = newSpanContext(span, context)
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, strconv.Itoa(os.Getpid()))
span.SetTag(ext.Pid, t.pid)
}
// add tags from options
for k, v := range opts.Tags {
Expand Down

0 comments on commit f3fe6e5

Please sign in to comment.