http

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: 18 Imported by: 6

README

HTTP module

How to use

$ go get github.com/tel-io/instrumentation/middleware/http@latest
Client
import (
    "github.com/tel-io/tel/v2"
	mw "github.com/tel-io/instrumentation/middleware/http"
)

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

	// create wrapped client
    client:= mw.NewClient(nil)

	ctx, span := t.StartSpan(context.Background(), path, trace.WithAttributes(semconv.PeerServiceKey.String("ExampleService")))
    defer span.End()

	// create context based request
    req, _ := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
	//
    res, err := client.Do(req)
}
Server
import (
    mw "github.com/tel-io/instrumentation/middleware/http"
)

func (s *Server) Start(ctx context.Context) (err error) {
	m := mw.ServerMiddlewareAll()

	mx := http.NewServeMux()
	mx.Handle("/hello", m(http.HandlerFunc(s.helloHttp)))
	mx.Handle("/crash", m(http.HandlerFunc(s.crashHttp)))
	mx.Handle("/error", m(http.HandlerFunc(s.errorHttp)))

	srv := &http.Server{}
	srv.Handler = mx
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultSpanNameFormatter = func(_ string, r *http.Request) string {
		var b strings.Builder

		b.WriteString(r.Method)
		b.WriteString(":")
		b.WriteString(decreasePathCardinality(r.URL.Path))

		return b.String()
	}

	DefaultFilter = func(r *http.Request) bool {
		if k, ok := r.Header["Upgrade"]; ok {
			for _, v := range k {
				if v == "websocket" {
					return false
				}
			}
		}

		return !(r.Method == http.MethodGet && strings.HasPrefix(r.URL.RequestURI(), "/health"))
	}
)

Functions

func DefaultURI

func DefaultURI(r *http.Request) string

func NewClient

func NewClient(ca []byte, opts ...Option) *http.Client

NewClient with CA injection

func UpdateClient

func UpdateClient(c *http.Client, opts ...Option) *http.Client

UpdateClient inject tracer

Types

type Checker added in v1.2.0

type Checker struct {
	URL     string
	Timeout time.Duration
}

func NewChecker added in v1.2.0

func NewChecker(url string) Checker

func NewDomainWithTimeout added in v1.2.0

func NewDomainWithTimeout(url string, timeout time.Duration) Checker

func (Checker) Check added in v1.2.0

type Middleware

type Middleware func(next http.Handler) http.Handler

func ServerMiddleware

func ServerMiddleware(opts ...Option) Middleware

ServerMiddleware perform: * telemetry log injection * measure execution time * recovery

func ServerMiddlewareAll

func ServerMiddlewareAll(opts ...Option) Middleware

ServerMiddlewareAll represent all essential metrics Execution order:

  • opentracing injection via nethttp.Middleware
  • recovery + measure execution time + debug log via own ServerMiddleware
  • metrics via metrics.NewHTTPMiddlewareWithOption

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface used for setting optional config properties.

func WithDumpPayloadOnError added in v1.2.2

func WithDumpPayloadOnError(enable bool) Option

WithDumpPayloadOnError write dump request and response on faults

Default: true

func WithDumpRequest added in v1.2.0

func WithDumpRequest(enable bool) Option

WithDumpRequest dump request as plain text to log and trace i guess we can go further and perform option with encoding requests

func WithDumpResponse added in v1.2.0

func WithDumpResponse(enable bool) Option

WithDumpResponse dump response as plain text to log and trace

func WithFilter

func WithFilter(f ...otelhttp.Filter) Option

WithFilter append filter to default

func WithHeaders added in v1.2.0

func WithHeaders(enable bool) Option

WithHeaders explicitly set possibility to write http headers

func WithOperation

func WithOperation(name string) Option

func WithOtelOpts

func WithOtelOpts(opts ...otelhttp.Option) Option

func WithPathExtractor

func WithPathExtractor(in PathExtractor) Option

func WithTel

func WithTel(t *tel.Telemetry) Option

WithTel also add options to pass own metric and trace provider

type PathExtractor

type PathExtractor func(r *http.Request) string

type TracedServeMux

type TracedServeMux struct {
	// contains filtered or unexported fields
}

TracedServeMux is a wrapper around http.ServeMux that instruments handlers for tracing.

func NewServeMux

func NewServeMux(opts ...Option) *TracedServeMux

NewServeMux creates a new TracedServeMux.

func (*TracedServeMux) Handle

func (tm *TracedServeMux) Handle(pattern string, handler http.Handler)

Handle implements http.ServeMux#Handle

func (*TracedServeMux) ServeHTTP

func (tm *TracedServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.ServeMux#ServeHTTP

Jump to

Keyboard shortcuts

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