connect contexts managed by fiber middleware to join at c.UserContext()

This commit is contained in:
Natalia Goc 2024-05-20 11:45:27 +02:00
parent 372f4367ed
commit d119563c7d
2 changed files with 8 additions and 2 deletions

View File

@ -15,6 +15,7 @@ import (
"git.ma-al.com/maal-libraries/observer/pkg/level" "git.ma-al.com/maal-libraries/observer/pkg/level"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/trace"
) )
func main() { func main() {
@ -42,6 +43,9 @@ func main() {
main.Use(func(c *fiber.Ctx) error { main.Use(func(c *fiber.Ctx) error {
span := tracing.SpanFromContext(c) span := tracing.SpanFromContext(c)
span.AddEvent("pushed into a span an event from middleware") 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() return c.Next()
}) })

View File

@ -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 // 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) { func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
ctx := fiberOpentelemetry.FromCtx(c) link := trace.LinkFromContext(fiberOpentelemetry.FromCtx(c))
return fiberOpentelemetry.Tracer.Start(ctx, c.Method()+" "+c.Path(), opts...) opts = append(opts, trace.WithLinks(link))
return fiberOpentelemetry.Tracer.Start(c.UserContext(), c.Method()+" "+c.Path(), opts...)
} }
// Retrieve span using [fiber.Ctx] // Retrieve span using [fiber.Ctx]