fiber_tracing

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 9 Imported by: 0

README

fiber_tracing is middleware for fiber framework

fiber_tracing Middleware trace requests on Fiber framework with OpenTracing API. You can use every tracer that implement OpenTracing interface

Install

go get -u github.com/gofiber/fiber
go get -u github.com/shareed2k/fiber_tracing

Config

Property Type Description Default
Tracer opentracing.Tracer initializes an opentracing tracer., possible values: jaeger, lightstep, instana, basictracer-go, ... "&opentracing.NoopTracer{}"
OperationName func(*fiber.Ctx) string Span operation name "HTTP " + ctx.Method() + " URL: " + ctx.Path()
ComponentName string Used for describing the tracing component name fiber/v2
ParentSpanKey string context key string used to get span scoped to the request #defaultTracingParentSpanKey
Filter func(*fiber.Ctx) bool Defines a function to skip middleware. nil
Modify func(*fiber.Ctx, opentracing.Span) Defines a function to edit span like add tags or logs... span.SetTag("http.remote_addr", ctx.IP()) ...

Example

package main

import (
	"github.com/gofiber/fiber"
	"github.com/shareed2k/fiber_tracing"
	"github.com/uber/jaeger-client-go/config"
)

func main() {
	app := fiber.New()

	defcfg := config.Configuration{
		ServiceName: "fiber-tracer",
		Sampler: &config.SamplerConfig{
			Type:  "const",
			Param: 1,
		},
		Reporter: &config.ReporterConfig{
			LogSpans:            true,
			BufferFlushInterval: 1 * time.Second,
		},
	}
	cfg, err := defcfg.FromEnv()
	if err != nil {
		panic("Could not parse Jaeger env vars: " + err.Error())
	}
	tracer, closer, err := cfg.NewTracer()
	if err != nil {
		panic("Could not initialize jaeger tracer: " + err.Error())
	}

	defer closer.Close()

	app.Use(fiber_tracing.NewWithConfig(fiber_tracing.Config{
		Tracer: tracer,
	}))

	// or
	/*
	app.Use(fiber_tracing.New(tracer))
	*/

	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Welcome!")
	})

	app.Listen(3000)
}

Example 2 with jaeger default tracer

package main

import (
	"github.com/gofiber/fiber"
	"github.com/shareed2k/fiber_tracing"
	"github.com/uber/jaeger-client-go/config"
)

func main() {
	app := fiber.New()

	closer := fiber_tracing.NewWithJaegerTracer(app)
	defer closer.Close()

	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Welcome!")
	})

	app.Listen(3000)
}

Documentation

Index

Constants

View Source
const (
	DefaultParentSpanKey = "#defaultTracingParentSpanKey"
	DefaultComponentName = "fiber/v2"
)

Variables

View Source
var (
	// DefaultTraceConfig is the default Trace middleware config.
	DefaultConfig = Config{
		Modify: func(ctx *fiber.Ctx, span opentracing.Span) {
			ext.HTTPMethod.Set(span, ctx.Method())
			ext.HTTPUrl.Set(span, ctx.OriginalURL())
			ext.Component.Set(span, DefaultComponentName)

			span.SetTag("http.remote_addr", ctx.IP())
			span.SetTag("http.path", ctx.Path())
			span.SetTag("http.host", ctx.Hostname())
		},
		OperationName: func(ctx *fiber.Ctx) string {
			return "HTTP " + ctx.Method() + " URL: " + ctx.Path()
		},
		ComponentName: DefaultComponentName,
		ParentSpanKey: DefaultParentSpanKey,
	}
)

Functions

func New

func New(tracer opentracing.Tracer) fiber.Handler

New returns a Trace middleware. Trace middleware traces http requests and reporting errors.

func NewWithConfig

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

NewWithConfig returns a Trace middleware with config.

func NewWithJaegerTracer

func NewWithJaegerTracer(f *fiber.App) io.Closer

NewWithJaegerTracer creates an Opentracing tracer and attaches it to Fiber middleware. Returns Closer do be added to caller function as `defer closer.Close()`

Types

type Config

type Config struct {
	// Tracer
	// Default: NoopTracer
	Tracer opentracing.Tracer

	// ParentSpanKey
	// Default: #defaultTracingParentSpanKey
	ParentSpanKey string

	// ComponentName used for describing the tracing component name
	ComponentName string

	// OperationName
	// Default: func(ctx *fiber.Ctx) string {
	//	 return "HTTP " + ctx.Method() + " URL: " + ctx.Path()
	// }
	OperationName func(*fiber.Ctx) string

	// Filter defines a function to skip middleware.
	// Optional. Default: nil
	Filter func(*fiber.Ctx) bool

	// Modify
	Modify func(*fiber.Ctx, opentracing.Span)
}

Config ...

Jump to

Keyboard shortcuts

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