Skip to content

Latest commit

 

History

History
81 lines (62 loc) · 1.71 KB

README.md

File metadata and controls

81 lines (62 loc) · 1.71 KB

Fiber OpenTelemetry

OpenTelemetry trace middleware for Fiber that adds traces to requests.

Table of Contents

Signatures

func New(config ...Config) fiber.Handler

Examples

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

// Config defines the config for middleware.
type Config struct {
    Tracer                trace.Tracer
    TracerStartAttributes []trace.SpanStartOption
    SpanName              string
    LocalKeyName          string
}

Default Config

var ConfigDefault = Config{
    SpanName:     "http/request",
    LocalKeyName: LocalsCtxKey,
    TracerStartAttributes: []trace.SpanStartOption{
        trace.WithSpanKind(trace.SpanKindServer),
        trace.WithNewRoot(),
        trace.WithRecord(),
    },
}