OpenTelemetry trace middleware for Fiber that adds traces to requests.
func New(config ...Config) fiber.Handler
Import the middleware package that is part of the Fiber web framework
import (
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/label"
fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry"
)
After you initiate your Fiber app, you can use the following possibilities:
// Default middleware config
tracer := trace.NoopTracerProvider().Tracer("test")
app.Use(fiberOpentelemetry.New(fiberOpentelemetry.Config{
Tracer: trace,
}))
app.Get("/", func(ctx *fiber.Ctx) error {
c := fiber_opentelemetry.FromCtx(ctx)
ctx, span := tracer.Start(ctx, "trace-name")
defer span.End()
// attribute
span.SetAttributes(label.String("attribute-name", "123"))
// error
span.RecordError(ctx, err)
// event
span.AddEvent(ctx, "event-name")
})
// Config defines the config for middleware.
type Config struct {
Tracer trace.Tracer
TracerStartAttributes []trace.SpanStartOption
SpanName string
LocalKeyName string
}
var ConfigDefault = Config{
SpanName: "http/request",
LocalKeyName: LocalsCtxKey,
TracerStartAttributes: []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
trace.WithNewRoot(),
trace.WithRecord(),
},
}