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

[tracer] allowing SetMeta at tracer level #56

Merged
merged 3 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewSpan(name, service, resource string, spanID, traceID, parentID uint64, t
Name: name,
Service: service,
Resource: resource,
Meta: tracer.getAllMeta(),
SpanID: spanID,
TraceID: traceID,
ParentID: parentID,
Expand Down
39 changes: 39 additions & 0 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Tracer struct {
enabled bool // defines if the Tracer is enabled or not
enableMu sync.RWMutex

meta map[string]string
metaMu sync.RWMutex

services map[string]Service // name -> service
servicesModified bool
serviceChan chan Service
Expand Down Expand Up @@ -133,6 +136,42 @@ func (t *Tracer) SetServiceInfo(name, app, appType string) {
}
}

// SetMeta adds an arbitrary meta field at the tracer level.
// This will append those tags to each span created by the tracer.
func (t *Tracer) SetMeta(key, value string) {
if t == nil { // Defensive, span could be initialized with nil tracer
return
}

t.metaMu.Lock()
if t.meta == nil {
t.meta = make(map[string]string)
}
t.meta[key] = value
t.metaMu.Unlock()
}

// getAllMeta returns all the meta set by this tracer.
// In most cases, it is nil.
func (t *Tracer) getAllMeta() map[string]string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to implement this one? We are dealing without it in Python and Ruby, so I'd vote to keep the API simple if we can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it's here is locking, this way we use RLock within the tracer code, the span code does not have to worry about it. Should we not do this I think span code would have to deal internally with the private mutex. This is perfectly doable, but I tend to prefer having objects dealing with their mutex internally, and not require others to care about it.

Additionnally, Python code uses https://github.com/DataDog/dd-trace-py/blob/master/ddtrace/tracer.py#L164 which calls https://github.com/DataDog/dd-trace-py/blob/master/ddtrace/span.py#L145 so it also has this deep copy, only it's done in span.py instead of tracer.py.

I can move the code to span.go if you want to, just exposing the reason it's here. Note that getAllMeta is private, it's not part of the API anyway.

if t == nil { // Defensive, span could be initialized with nil tracer
return nil
}

var meta map[string]string

t.metaMu.RLock()
if t.meta != nil {
meta = make(map[string]string, len(t.meta))
for key, value := range t.meta {
meta[key] = value
}
}
t.metaMu.RUnlock()

return meta
}

// NewRootSpan creates a span with no parent. Its ids will be randomly
// assigned.
func (t *Tracer) NewRootSpan(name, service, resource string) *Span {
Expand Down
36 changes: 36 additions & 0 deletions tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,42 @@ func TestTracerServicesDisabled(t *testing.T) {
assert.Equal(0, len(transport.services))
}

func TestTracerMeta(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case to test: when we set a meta at the span-level which has the same key as one set as the tracer-level.

And to test something closer to user-land, we should do asserts on spans only (and don't need getAllMeta)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, will update the tests, thanks for feedback.

assert := assert.New(t)

var nilTracer *Tracer
nilTracer.SetMeta("key", "value")
assert.Nil(nilTracer.getAllMeta(), "nil tracer should return nil meta")

tracer, _ := getTestTracer()
assert.Nil(tracer.getAllMeta(), "by default, no meta")
tracer.SetMeta("env", "staging")

span := tracer.NewRootSpan("pylons.request", "pylons", "/")
assert.Equal("staging", span.GetMeta("env"))
assert.Equal("", span.GetMeta("component"))
span.Finish()
assert.Equal(map[string]string{"env": "staging"}, tracer.getAllMeta(), "there should be one meta")

tracer.SetMeta("component", "core")
span = tracer.NewRootSpan("pylons.request", "pylons", "/")
assert.Equal("staging", span.GetMeta("env"))
assert.Equal("core", span.GetMeta("component"))
span.Finish()
assert.Equal(map[string]string{"env": "staging", "component": "core"}, tracer.getAllMeta(), "there should be two entries")

tracer.SetMeta("env", "prod")
span = tracer.NewRootSpan("pylons.request", "pylons", "/")
assert.Equal("prod", span.GetMeta("env"))
assert.Equal("core", span.GetMeta("component"))
span.SetMeta("env", "sandbox")
assert.Equal("sandbox", span.GetMeta("env"))
assert.Equal("core", span.GetMeta("component"))
span.Finish()

assert.Equal(map[string]string{"env": "prod", "component": "core"}, tracer.getAllMeta(), "key1 should have been updated")
}

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