Skip to content
This repository has been archived by the owner on Jun 5, 2022. It is now read-only.

OpenTelemetry trace middleware for Fiber that adds traces to requests.

License

Notifications You must be signed in to change notification settings

psmarcin/fiber-opentelemetry

Repository files navigation

Fiber OpenTelemetry

❗️Important

This repository is now archived because there is new official library available for everyone. Please migrate to https://github.com/gofiber/contrib/tree/main/otelfiber

There is no need to keep two libraries to solve one problem.

Old description

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(),
    },
}

About

OpenTelemetry trace middleware for Fiber that adds traces to requests.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages