Skip to content

Commit

Permalink
Merge pull request #372 from maxekman/fix-optional-tracing-context
Browse files Browse the repository at this point in the history
Fix / Register tracing context manually
  • Loading branch information
maxekman authored Dec 14, 2021
2 parents 1b41069 + a7ea4e5 commit f89f3f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/todomvc/backend/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewHandler(

// Simple HTTP request logging middleware as final handler.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL)
log.Printf("%s %s\n", r.Method, r.URL.EscapedPath())
h.ServeHTTP(w, r)
})

Expand Down
2 changes: 2 additions & 0 deletions examples/todomvc/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func main() {
log.Fatal("could not create tracer: ", err)
}

tracing.RegisterContext()

// Create the outbox that will project and publish events.
var outbox eh.Outbox

Expand Down
12 changes: 11 additions & 1 deletion tracing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ const (
tracingSpanKeyStr = "eh_tracing_span"
)

func init() {
// RegisterContext registers the tracing span to be marshaled/unmarshaled on the
// context. This enables propagation of the tracing spans for backends that
// supports it (like Jaeger).
//
// For usage with Elastic APM which doesn't support submitting of child spans
// for the same parent span multiple times outside of a single transaction don't
// register the context. This will provide a new context upon handling in the
// event bus or outbox, which currently is the best Elastic APM can support.
//
// See: https://github.com/elastic/apm/issues/122
func RegisterContext() {
eh.RegisterContextMarshaler(func(ctx context.Context, vals map[string]interface{}) {
if span := opentracing.SpanFromContext(ctx); span != nil {
tracer := opentracing.GlobalTracer()
Expand Down
4 changes: 4 additions & 0 deletions tracing/eventbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"github.com/looplab/eventhorizon/eventbus/local"
)

func init() {
RegisterContext()
}

// NOTE: Not named "Integration" to enable running with the unit tests.
func TestEventBusAddHandler(t *testing.T) {
if testing.Short() {
Expand Down

0 comments on commit f89f3f5

Please sign in to comment.