Skip to content
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 {
Copy link

Copilot AI Mar 14, 2025

Choose a reason for hiding this comment

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

The type assertion on ctx may panic if the assertion fails. Consider using the comma-ok idiom (e.g., spanCtx, ok := ctx.(testSpanContext)) and failing the test with a clear message if the assertion does not succeed.

Suggested change
if ctx.(testSpanContext).FakeID != 42 {
spanCtx, ok := ctx.(testSpanContext)
if !ok {
t.Fatalf("Expected testSpanContext, got %T", ctx)
}
if spanCtx.FakeID != 42 {

Copilot uses AI. Check for mistakes.

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
)