Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Add span context to OnStartSpan #159

Merged
merged 20 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9806da7
Add span context to OnStartSpan
hkshaw1990 Jun 12, 2017
19e8a3d
Introduce contribObserver and contribSpanObserver
hkshaw1990 Jun 13, 2017
702850d
Wrap the old observer to contrib observer
hkshaw1990 Jun 20, 2017
8217148
Remove copyright
hkshaw1990 Jun 22, 2017
cd19a93
Redefine go-observer interface as ContribObserver
hkshaw1990 Jun 22, 2017
eaa3e43
Add bool according to the API changes in go-observer
hkshaw1990 Jun 22, 2017
f21c9cd
Add comments to contrib_observer.go
hkshaw1990 Jun 22, 2017
4cd454f
Rephrase comments
hkshaw1990 Jun 23, 2017
3f0685b
Rename contrib* to composite* for observer api
hkshaw1990 Jun 23, 2017
5d2dbd3
Remove bool from compositeObserver's OnStartSpan
hkshaw1990 Jun 23, 2017
02b63d7
Retire the old observer dispatcher and related callbacks
hkshaw1990 Jun 23, 2017
2a32e77
Rename the global interfaces for the new observer
hkshaw1990 Jul 4, 2017
22fb0a2
Use pointers for compositeObserver and compositeSpanObserver methods
hkshaw1990 Jul 6, 2017
a06ccab
Fix Formatting issues
hkshaw1990 Jul 6, 2017
cb923e2
Fix the tracer argument type
hkshaw1990 Jul 6, 2017
38c6fe4
Resolve a merge issue by moving the tags part in config/config.go
hkshaw1990 Jul 7, 2017
d2ddb67
Remove the old dispatcher and move the new dispatcher to observer.go
hkshaw1990 Jul 10, 2017
9ab053e
Delegate from Observer to ContribObserver for tracer options
hkshaw1990 Jul 11, 2017
59e4a99
Fix a type assertion causing build failure in observer_test.go
hkshaw1990 Jul 13, 2017
b06b8a1
Fix whitespace issue
hkshaw1990 Jul 13, 2017
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
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (c Configuration) New(
tracerOptions = append(tracerOptions, jaeger.TracerOptions.Tag(tag.Key, tag.Value))
}

for _, cobs := range opts.contribObservers {
tracerOptions = append(tracerOptions, jaeger.TracerOptions.ContribObserver(cobs))
}

tracer, closer := jaeger.NewTracer(
serviceName,
sampler,
Expand Down
9 changes: 9 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Options struct {
metrics metrics.Factory
logger jaeger.Logger
reporter jaeger.Reporter
contribObservers []jaeger.ContribObserver
observers []jaeger.Observer
zipkinSharedRPCSpan bool
tags []opentracing.Tag
Expand Down Expand Up @@ -71,6 +72,14 @@ func Observer(observer jaeger.Observer) Option {
}
}

// ContribObserver can be registered with the Tracer to recieve notifications
// about new spans.
func ContribObserver(observer jaeger.ContribObserver) Option {
return func(c *Options) {
c.contribObservers = append(c.contribObservers, observer)
}
}

// ZipkinSharedRPCSpan creates an option that enables sharing span ID between client
// and server spans a la zipkin. If false, client and server spans will be assigned
// different IDs.
Expand Down
85 changes: 85 additions & 0 deletions contrib_observer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package jaeger

import (
opentracing "github.com/opentracing/opentracing-go"
)

// ContribObserver can be registered with the Tracer to receive notifications
// about new Spans. Modelled after github.com/opentracing-contrib/go-observer.
type ContribObserver interface {
// Create and return a span observer. Called when a span starts.
// If the Observer is not interested in the given span, it must return (nil, false).
// E.g :
// func StartSpan(opName string, opts ...opentracing.StartSpanOption) {
// var sp opentracing.Span
// sso := opentracing.StartSpanOptions{}
// if spanObserver, ok := Observer.OnStartSpan(span, opName, sso); ok {
// // we have a valid SpanObserver
// }
// ...
// }
OnStartSpan(sp opentracing.Span, operationName string, options opentracing.StartSpanOptions) (ContribSpanObserver, bool)
}

// ContribSpanObserver is created by the Observer and receives notifications
// about other Span events. This interface is meant to match
// github.com/opentracing-contrib/go-observer, via duck typing, without
// directly importing the go-observer package.
type ContribSpanObserver interface {
OnSetOperationName(operationName string)
OnSetTag(key string, value interface{})
OnFinish(options opentracing.FinishOptions)
}

// wrapper observer for the old observers (see observer.go)
type oldObserver struct {
obs Observer
}

func (o *oldObserver) OnStartSpan(sp opentracing.Span, operationName string, options opentracing.StartSpanOptions) (ContribSpanObserver, bool) {
spanObserver := o.obs.OnStartSpan(operationName, options)
return spanObserver, spanObserver != nil
}

// compositeSpanObserver is a dispatcher to other span observers
type compositeSpanObserver struct {
observers []ContribSpanObserver
}

func (o *compositeSpanObserver) OnSetOperationName(operationName string) {
for _, obs := range o.observers {
obs.OnSetOperationName(operationName)
}
}

func (o *compositeSpanObserver) OnSetTag(key string, value interface{}) {
for _, obs := range o.observers {
obs.OnSetTag(key, value)
}
}

func (o *compositeSpanObserver) OnFinish(options opentracing.FinishOptions) {
for _, obs := range o.observers {
obs.OnFinish(options)
}
}
61 changes: 22 additions & 39 deletions observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,67 +22,50 @@ package jaeger

import opentracing "github.com/opentracing/opentracing-go"

// Observer can be registered with the Tracer to receive notifications about new Spans.
// Observer can be registered with the Tracer to receive notifications about
// new Spans.
//
// Deprecated: use jaeger.ContribObserver instead.
type Observer interface {
OnStartSpan(operationName string, options opentracing.StartSpanOptions) SpanObserver
}

// SpanObserver is created by the Observer and receives notifications about other Span events.
// SpanObserver is created by the Observer and receives notifications about
// other Span events.
//
// Deprecated: use jaeger.ContribSpanObserver instead.
type SpanObserver interface {
OnSetOperationName(operationName string)
OnSetTag(key string, value interface{})
OnFinish(options opentracing.FinishOptions)
}

// observer is a dispatcher to other observers
type observer struct {
observers []Observer
}

// spanObserver is a dispatcher to other span observers
type spanObserver struct {
observers []SpanObserver
// compositeObserver is a dispatcher to other observers
type compositeObserver struct {
observers []ContribObserver
}

// noopSpanObserver is used when there are no observers registered on the Tracer
// or none of them returns span observers from OnStartSpan.
var noopSpanObserver = spanObserver{}
// noopSpanObserver is used when there are no observers registered
// on the Tracer or none of them returns span observers from OnStartSpan.
var noopSpanObserver = &compositeSpanObserver{}

func (o *observer) append(observer Observer) {
o.observers = append(o.observers, observer)
func (o *compositeObserver) append(contribObserver ContribObserver) {
o.observers = append(o.observers, contribObserver)
}

func (o observer) OnStartSpan(operationName string, options opentracing.StartSpanOptions) SpanObserver {
var spanObservers []SpanObserver
func (o *compositeObserver) OnStartSpan(sp opentracing.Span, operationName string, options opentracing.StartSpanOptions) ContribSpanObserver {
var spanObservers []ContribSpanObserver
for _, obs := range o.observers {
spanObs := obs.OnStartSpan(operationName, options)
if spanObs != nil {
spanObs, ok := obs.OnStartSpan(sp, operationName, options)
if ok {
if spanObservers == nil {
spanObservers = make([]SpanObserver, 0, len(o.observers))
spanObservers = make([]ContribSpanObserver, 0, len(o.observers))
}
spanObservers = append(spanObservers, spanObs)
}
}
if len(spanObservers) == 0 {
return noopSpanObserver
}
return spanObserver{observers: spanObservers}
}

func (o spanObserver) OnSetOperationName(operationName string) {
for _, obs := range o.observers {
obs.OnSetOperationName(operationName)
}
}

func (o spanObserver) OnSetTag(key string, value interface{}) {
for _, obs := range o.observers {
obs.OnSetTag(key, value)
}
}

func (o spanObserver) OnFinish(options opentracing.FinishOptions) {
for _, obs := range o.observers {
obs.OnFinish(options)
}
return &compositeSpanObserver{observers: spanObservers}
}
2 changes: 1 addition & 1 deletion observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestObservers(t *testing.T) {
s := tracer.StartSpan("test", ext.RPCServerOption(nil))

forEachObs := func(f func(so *testSpanObserver)) {
observers := s.(*Span).observer.(spanObserver).observers
observers := s.(*Span).observer.(*compositeSpanObserver).observers
assert.Len(t, observers, 2)
for _, so := range observers {
f(so.(*testSpanObserver))
Expand Down
2 changes: 1 addition & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Span struct {
// references for this span
references []Reference

observer SpanObserver
observer ContribSpanObserver
}

// Tag is a simple key value wrapper.
Expand Down
4 changes: 2 additions & 2 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Tracer struct {
injectors map[interface{}]Injector
extractors map[interface{}]Extractor

observer observer
observer compositeObserver

tags []Tag
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (t *Tracer) startSpanWithOptions(

sp := t.newSpan()
sp.context = ctx
sp.observer = t.observer.OnStartSpan(operationName, options)
sp.observer = t.observer.OnStartSpan(sp, operationName, options)
return t.startSpanInternal(
sp,
operationName,
Expand Down
6 changes: 5 additions & 1 deletion tracer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOp
}
}

func (tracerOptions) Observer(observer Observer) TracerOption {
func (t *tracerOptions) Observer(observer Observer) TracerOption {
return t.ContribObserver(&oldObserver{obs: observer})
}

func (tracerOptions) ContribObserver(observer ContribObserver) TracerOption {
return func(tracer *Tracer) {
tracer.observer.append(observer)
}
Expand Down