diff --git a/example/main.go b/example/main.go index 2e05b01..ef0338d 100644 --- a/example/main.go +++ b/example/main.go @@ -15,6 +15,7 @@ import ( "git.ma-al.com/maal-libraries/observer/pkg/level" "github.com/gofiber/fiber/v2" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" + "go.opentelemetry.io/otel/trace" ) func main() { @@ -42,6 +43,9 @@ func main() { main.Use(func(c *fiber.Ctx) error { span := tracing.SpanFromContext(c) span.AddEvent("pushed into a span an event from middleware") + + span = trace.SpanFromContext(c.UserContext()) + span.AddEvent("span also available from c.UserContext()") return c.Next() }) diff --git a/pkg/fiber_tracing/fiber_tracing.go b/pkg/fiber_tracing/fiber_tracing.go index 3e9cf78..6503207 100644 --- a/pkg/fiber_tracing/fiber_tracing.go +++ b/pkg/fiber_tracing/fiber_tracing.go @@ -44,9 +44,11 @@ func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) } // NOTE: You can use [trace.WithAttributes] as a parameter to opts argument +// Returns [c.UserContext] as [context.Context] func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace.Span) { - ctx := fiberOpentelemetry.FromCtx(c) - return fiberOpentelemetry.Tracer.Start(ctx, c.Method()+" "+c.Path(), opts...) + link := trace.LinkFromContext(fiberOpentelemetry.FromCtx(c)) + opts = append(opts, trace.WithLinks(link)) + return fiberOpentelemetry.Tracer.Start(c.UserContext(), c.Method()+" "+c.Path(), opts...) } // Retrieve span using [fiber.Ctx]