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: add customize tracer in extract func and add gomodule #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions amqptracer/tracer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package amqptracer

import (
"errors"
opentracing "github.com/opentracing/opentracing-go"
"github.com/streadway/amqp"
)
Expand Down Expand Up @@ -55,3 +56,30 @@ func Extract(hdrs amqp.Table) (opentracing.SpanContext, error) {
c := amqpHeadersCarrier(hdrs)
return opentracing.GlobalTracer().Extract(opentracing.TextMap, c)
}

// Extract extracts the span context out of the AMQP header.
//
// Example:
//
// func ConsumeMessage(ctx context.Context, msg *amqp.Delivery) error {
// // Extract the span context out of the AMQP header.
// spCtx, _ := amqptracer.ExtractWithTracer(msg.Headers, tracer)
// sp := opentracing.StartSpan(
// "ConsumeMessage",
// opentracing.FollowsFrom(spCtx),
// )
// defer sp.Finish()
//
// // Update the context with the span for the subsequent reference.
// ctx = opentracing.ContextWithSpan(ctx, sp)
//
// // Actual message processing.
// return ProcessMessage(ctx, msg)
// }
func ExtractWithTracer(hdrs amqp.Table, tracer opentracing.Tracer) (opentracing.SpanContext, error) {
if tracer == nil {
return nil, errors.New("tracer is nil")
}
c := amqpHeadersCarrier(hdrs)
return tracer.Extract(opentracing.TextMap, c)
}
16 changes: 16 additions & 0 deletions amqptracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,19 @@ func TestExtract(t *testing.T) {
t.Errorf("Failed to read testprefix-fakeid correctly")
}
}

func TestExtractWithTracer(t *testing.T) {
h := map[string]interface{}{}
h["NotOT"] = "blah"
h["opname"] = "AlsoNotOT"
h["testprefix-fakeid"] = "42"

// Extract the tracing span out from the AMQP header
ctx, err := ExtractWithTracer(h, testTracer{})
if err != nil {
t.Fatal(err)
}
if ctx.(testSpanContext).FakeID != 42 {
t.Errorf("Failed to read testprefix-fakeid correctly")
}
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/opentracing-contrib/go-amqp

go 1.13

require (
github.com/opentracing/opentracing-go v1.1.0
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71
github.com/stretchr/testify v1.5.1 // indirect
)