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

feat: Tracer in Transport #45

Closed
Closed
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
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/opentracing-contrib/go-stdlib

go 1.12

require (
github.com/opentracing/opentracing-go v1.1.0
github.com/stretchr/testify v1.3.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
23 changes: 16 additions & 7 deletions nethttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Transport struct {
// The actual RoundTripper to use for the request. A nil
// RoundTripper defaults to http.DefaultTransport.
http.RoundTripper
Tracer opentracing.Tracer
}

type clientOptions struct {
Expand Down Expand Up @@ -97,11 +98,9 @@ func ClientSpanObserver(f func(span opentracing.Span, r *http.Request)) ClientOp
// return nil
// }
func TraceRequest(tr opentracing.Tracer, req *http.Request, options ...ClientOption) (*http.Request, *Tracer) {
opts := &clientOptions{
spanObserver: func(_ opentracing.Span, _ *http.Request) {},
}
opts := clientOptions{}
for _, opt := range options {
opt(opts)
opt(&opts)
}
ht := &Tracer{tr: tr, opts: opts}
ctx := req.Context()
Expand Down Expand Up @@ -140,7 +139,14 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if rt == nil {
rt = http.DefaultTransport
}
tracer := TracerFromRequest(req)

var tracer *Tracer
if t.Tracer != nil {
tracer = &Tracer{tr: t.Tracer}
} else {
tracer = TracerFromRequest(req)
}

if tracer == nil {
return rt.RoundTrip(req)
}
Expand All @@ -149,7 +155,10 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {

ext.HTTPMethod.Set(tracer.sp, req.Method)
ext.HTTPUrl.Set(tracer.sp, req.URL.String())
tracer.opts.spanObserver(tracer.sp, req)

if tracer.opts.spanObserver != nil {
tracer.opts.spanObserver(tracer.sp, req)
}

carrier := opentracing.HTTPHeadersCarrier(req.Header)
tracer.sp.Tracer().Inject(tracer.sp.Context(), opentracing.HTTPHeaders, carrier)
Expand All @@ -176,7 +185,7 @@ type Tracer struct {
tr opentracing.Tracer
root opentracing.Span
sp opentracing.Span
opts *clientOptions
opts clientOptions
}

func (h *Tracer) start(req *http.Request) opentracing.Span {
Expand Down