From d119563c7d45e7acca3a7db74de0363b81ad6ffe Mon Sep 17 00:00:00 2001 From: Natalia Goc Date: Mon, 20 May 2024 11:45:27 +0200 Subject: [PATCH] connect contexts managed by fiber middleware to join at c.UserContext() --- example/main.go | 4 ++++ pkg/fiber_tracing/fiber_tracing.go | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) 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]