fiberotel

package module
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2021 License: MIT Imports: 8 Imported by: 0

README

Fiber OpenTelemetry

This repo is forked from https://github.com/psmarcin/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/penguin-statistics/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(),
    },
}

Documentation

Index

Constants

View Source
const LocalsCtxKey = "otel-ctx"

Variables

View Source
var ConfigDefault = Config{
	SpanName:     "HTTP {{ .Method }}",
	LocalKeyName: LocalsCtxKey,
	TracerStartAttributes: []trace.SpanStartOption{
		trace.WithSpanKind(trace.SpanKindServer),
		trace.WithNewRoot(),
	},
}

ConfigDefault is the default config

View Source
var Tracer = otel.Tracer("fiberotel")

Functions

func FromCtx

func FromCtx(ctx *fiber.Ctx) context.Context

func New

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

New creates a new middleware handler

func SpanFromCtx

func SpanFromCtx(ctx *fiber.Ctx) trace.Span

func StartTracerFromCtx added in v0.8.2

func StartTracerFromCtx(ctx *fiber.Ctx, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

Types

type Config

type Config struct {
	Tracer                trace.Tracer
	TracerStartAttributes []trace.SpanStartOption
	// SpanName is a template for span naming.
	// The scope is fiber context.
	SpanName     string
	LocalKeyName string
}

Config defines the config for middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL