fasthttp

package module
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

README

FastHttp middleware

Provide simple middleware for fasthttp with context prapagation option

GET

$ go get github.com/tel-io/instrumentation/middleware/fasthttp@latest

Usage

Just wrap function with mw and context extractor GetNativeContext with tel instance and trace span

import (
    mw "github.com/tel-io/instrumentation/middleware/fasthttp"
)
...
middleware := mw.ServerMiddleware()

// simple handler
if err := fasthttp.ListenAndServe(port, middleware(func(ctx *fasthttp.RequestCtx) {
    // EXTRACT OTEL CONTEXT WITH SPAN AND EVERYTHING.....
    ctn := mw.GetNativeContext(ctx)
})
Client
Example
package main

import (
	"context"

	mw "github.com/tel-io/instrumentation/middleware/fasthttp"
	"github.com/tel-io/tel/v2"
	"github.com/valyala/fasthttp"
	"go.opentelemetry.io/otel"
)

func main() {
	t, cc := tel.New(context.Background(), tel.GetConfigFromEnv())
	defer cc()

	req := fasthttp.AcquireRequest()
	res := fasthttp.AcquireResponse()

	ccx := t.Copy().Ctx()
	
	span, ctx := tel.FromCtx(ccx).StartSpan(ccx, "req")
	defer span.End()

	// fill request with carrier information about trace and baggage
	otel.GetTextMapPropagator().Inject(ctx, mw.NewCarrier(&req.Header))

	c := fasthttp.Client{}
	if err := c.Do(req, res); err != nil {
		span.RecordError(err)
	}
}

Propagator

Required composite map propagator: TraceContext and Baggage

	otel.SetTextMapPropagator(
		propagation.NewCompositeTextMapPropagator(
			propagation.TraceContext{}, propagation.Baggage{},
		))

NOTE: tel library already uses it

WARNING!

This experimental mw just show possibility. Under hood, it uses huge overhead.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetNativeContext

func GetNativeContext(ctx *fasthttp.RequestCtx) context.Context

GetNativeContext retrieve a standard library context from FastHTTP request context.

func WithNativeContext

func WithNativeContext(ctx *fasthttp.RequestCtx, nativeCtx context.Context)

WithNativeContext stores a standard library context into FastHTTP request context.

Types

type HeaderCarrier added in v1.2.4

type HeaderCarrier struct {
	*fasthttp.RequestHeader
}

HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface.

func NewCarrier added in v1.2.4

func NewCarrier(in *fasthttp.RequestHeader) *HeaderCarrier

NewCarrier implemented TextMapCarrier

func (HeaderCarrier) Get added in v1.2.4

func (hc HeaderCarrier) Get(key string) string

Get returns the value associated with the passed key.

func (HeaderCarrier) Keys added in v1.2.4

func (hc HeaderCarrier) Keys() []string

Keys lists the keys stored in this carrier.

func (HeaderCarrier) Set added in v1.2.4

func (hc HeaderCarrier) Set(key string, value string)

Set stores the key-value pair.

type Middleware

type Middleware func(next fasthttp.RequestHandler) fasthttp.RequestHandler

func ServerMiddleware

func ServerMiddleware(opts ...mw.Option) Middleware

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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