allow retrieving spans from fiber.Ctx

This commit is contained in:
Natalia Goc 2024-05-20 11:04:36 +02:00
parent 77ab12c3ac
commit 372f4367ed
2 changed files with 12 additions and 0 deletions

View File

@ -39,6 +39,12 @@ func main() {
})) }))
defer tracing.ShutdownTracer() defer tracing.ShutdownTracer()
main.Use(func(c *fiber.Ctx) error {
span := tracing.SpanFromContext(c)
span.AddEvent("pushed into a span an event from middleware")
return c.Next()
})
main.Get("/", Handler) main.Get("/", Handler)
// handle interrupts (shutdown) // handle interrupts (shutdown)

View File

@ -49,6 +49,12 @@ func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace
return fiberOpentelemetry.Tracer.Start(ctx, c.Method()+" "+c.Path(), opts...) return fiberOpentelemetry.Tracer.Start(ctx, c.Method()+" "+c.Path(), opts...)
} }
// Retrieve span using [fiber.Ctx]
func SpanFromContext(c *fiber.Ctx) trace.Span {
ctx := fiberOpentelemetry.FromCtx(c)
return trace.SpanFromContext(ctx)
}
func NewMiddleware(config Config) func(*fiber.Ctx) error { func NewMiddleware(config Config) func(*fiber.Ctx) error {
var tracerProviders []trc.TracerProviderOption var tracerProviders []trc.TracerProviderOption