github.com/goadesign/goa/http/middleware


middleware

import "github.com/goadesign/goa/http/middleware"

Overview

Index

Package files

capture.go context.go ctxkeys.go debug.go log.go request.go requestid.go sampler.go trace.go

Constants

const (
    // RequestIDKey is the request context key used to store the request ID
    // created by the RequestID middleware.
    RequestIDKey ctxKey = iota + 1

    // TraceIDKey is the request context key used to store the current Trace
    // ID if any.
    TraceIDKey

    // TraceSpanIDKey is the request context key used to store the current
    // trace span ID if any.
    TraceSpanIDKey

    // TraceParentSpanIDKey is the request context key used to store the current
    // trace parent span ID if any.
    TraceParentSpanIDKey

    // RequestMethodKey is the request context key used to store r.Method created by
    // the PopulateRequestContext middleware.
    RequestMethodKey

    // RequestURIKey is the request context key used to store r.RequestURI created by
    // the PopulateRequestContext middleware.
    RequestURIKey

    // RequestPathKey is the request context key used to store r.URL.Path created by
    // the PopulateRequestContext middleware.
    RequestPathKey

    // RequestProtoKey is the request context key used to store r.Proto created by
    // the PopulateRequestContext middleware.
    RequestProtoKey

    // RequestHostKey is the request context key used to store r.Host created by
    // the PopulateRequestContext middleware.
    RequestHostKey

    // RequestRemoteAddrKey is the request context key used to store r.RemoteAddr
    // created by the PopulateRequestContext middleware.
    RequestRemoteAddrKey

    // RequestXForwardedForKey is the request context key used to store the
    // X-Forwarded-For header created by the PopulateRequestContext middleware.
    RequestXForwardedForKey

    // RequestXForwardedProtoKey is the request context key used to store the
    // X-Forwarded-Proto header created by the PopulateRequestContext middleware.
    RequestXForwardedProtoKey

    // RequestXRealIPKey is the request context key used to store the
    // X-Real-IP header created by the PopulateRequestContext middleware.
    RequestXRealIPKey

    // RequestAuthorizationKey is the request context key used to store the
    // Authorization header created by the PopulateRequestContext middleware.
    RequestAuthorizationKey

    // RequestRefererKey is the request context key used to store Referer header
    // created by the PopulateRequestContext middleware.
    RequestRefererKey

    // RequestUserAgentKey is the request context key used to store the User-Agent
    // header created by the PopulateRequestContext middleware.
    RequestUserAgentKey

    // RequestXRequestIDKey is the request context key used to store the X-Request-Id
    // header created by the PopulateRequestContext middleware.
    RequestXRequestIDKey

    // RequestAcceptKey is the request context key used to store the Accept header
    // created by the PopulateRequestContext middleware.
    RequestAcceptKey

    // RequestXCSRFTokenKey is the request context key used to store X-Csrf-Token header
    // created by the PopulateRequestContext middleware.
    RequestXCSRFTokenKey
)
const (
    // TraceIDHeader is the default name of the HTTP request header
    // containing the current TraceID if any.
    TraceIDHeader = "TraceID"

    // ParentSpanIDHeader is the default name of the HTTP request header
    // containing the parent span ID if any.
    ParentSpanIDHeader = "ParentSpanID"
)

func Debug

func Debug(mux goahttp.Muxer, w io.Writer) func(http.Handler) http.Handler

Debug returns a debug middleware which prints detailed information about incoming requests and outgoing responses including all headers, parameters and bodies.

func Log

func Log(l Logger) func(h http.Handler) http.Handler

Log returns a middleware that logs incoming HTTP requests and outgoing responses. The middleware uses the request ID set by the RequestID middleware or creates a short unique request ID if missing for each incoming request and logs it with the request and corresponding response details.

The middleware logs the incoming requests HTTP method and path as well as the originator of the request. The originator is computed by looking at the X-Forwarded-For HTTP header or - absent of that - the originating IP. The middleware also logs the response HTTP status code, body length (in bytes) and timing information.

func PopulateRequestContext

func PopulateRequestContext() func(http.Handler) http.Handler

PopulateRequestContext returns a middleware which populates a number of standard HTTP reqeust values into the request context. Those values may be extracted using the corresponding ContextKey type in this package.

func RequestContext

func RequestContext(ctx context.Context) func(http.Handler) http.Handler

RequestContext returns a middleware which initializes the request context.

func RequestContextKeyVals

func RequestContextKeyVals(keyvals ...interface{}) func(http.Handler) http.Handler

RequestContextKeyVals returns a middleware which adds the given key/value pairs to the request context.

func RequestID

func RequestID(options ...RequestIDOption) func(http.Handler) http.Handler

RequestID returns a middleware, which initializes the context with a unique value under the RequestIDKey key. Optionally uses the incoming “X-Request-Id” header, if present, with or without a length limit to use as request ID. the default behavior is to always generate a new ID.

examples of use:

service.Use(middleware.RequestID())

// enable options for using "X-Request-Id" header with length limit.
service.Use(middleware.RequestID(
  middleware.UseXRequestIDHeaderOption(true),
  middleware.XRequestHeaderLimitOption(128)))

func Trace

func Trace(opts ...Option) func(http.Handler) http.Handler

Trace returns a trace middleware that initializes the trace information in the request context.

samplingRate must be a value between 0 and 100. It represents the percentage of requests that should be traced. If the incoming request has a Trace ID header then the sampling rate is disregarded and the tracing is enabled.

spanIDFunc and traceIDFunc are the functions used to create Span and Trace IDs respectively. This is configurable so that the created IDs are compatible with the various backend tracing systems. The xray package provides implementations that produce AWS X-Ray compatible IDs.

func WithSpan

func WithSpan(ctx context.Context, traceID, spanID, parentID string) context.Context

WithSpan returns a context containing the given trace, span and parent span IDs.

func WithTrace

func WithTrace(ctx context.Context, traceID string) context.Context

WithTrace returns a context containing the given trace ID.

type Doer

type Doer interface {
    Do(*http.Request) (*http.Response, error)
}

Doer is the http client Do interface.

func WrapDoer

func WrapDoer(doer Doer) Doer

WrapDoer wraps a goa client Doer and sets the trace headers so that the downstream service may properly retrieve the parent span ID and trace ID.

type IDFunc

type IDFunc func() string

IDFunc is a function that produces span and trace IDs for consumption by tracing systems such as Zipkin or AWS X-Ray.

type Logger

type Logger interface {
    // Log creates a log entry using a sequence of alternating keys
    // and values.
    Log(keyvals ...interface{})
}

Logger is the logging interface used by the middleware to produce log entries.

func NewLogger

func NewLogger(l *log.Logger) Logger

NewLogger creates a Logger backed by a stdlib logger.

func WrapLogger

func WrapLogger(l Logger, traceID string) Logger

WrapLogger returns a logger which logs the trace ID with every message if there is one.

type Option

type Option func(*options) *options

Option is a constructor option that makes it possible to customize the middleware.

func MaxSamplingRate

func MaxSamplingRate(r int) Option

MaxSamplingRate sets a target sampling rate in requests per second. Setting a max sampling rate causes the middleware to adjust the sampling percent dynamically. Defaults to 2 req/s. SamplingPercent and MaxSamplingRate are mutually exclusive.

func SampleSize

func SampleSize(s int) Option

SampleSize sets the number of requests between two adjustments of the sampling rate when MaxSamplingRate is set. Defaults to 1,000.

func SamplingPercent

func SamplingPercent(p int) Option

SamplingPercent sets the tracing sampling rate as a percentage value. It panics if p is less than 0 or more than 100. SamplingPercent and MaxSamplingRate are mutually exclusive.

func SpanIDFunc

func SpanIDFunc(f IDFunc) Option

SpanIDFunc is a constructor option that overrides the function used to compute span IDs.

func TraceIDFunc

func TraceIDFunc(f IDFunc) Option

TraceIDFunc is a constructor option that overrides the function used to compute trace IDs.

type RequestIDOption

type RequestIDOption func(*requestIDOption) *requestIDOption

RequestIDOption uses a constructor pattern to customize middleware

func UseXRequestIDHeaderOption

func UseXRequestIDHeaderOption(f bool) RequestIDOption

UseXRequestIDHeaderOption enables/disables using “X-Request-Id” header.

func XRequestHeaderLimitOption

func XRequestHeaderLimitOption(limit int) RequestIDOption

XRequestHeaderLimitOption sets the option for using “X-Request-Id” header.

type ResponseCapture

type ResponseCapture struct {
    http.ResponseWriter
    StatusCode    int
    ContentLength int
}

ResponseCapture is a http.ResponseWriter which captures the response status code and content length.

func CaptureResponse

func CaptureResponse(w http.ResponseWriter) *ResponseCapture

CaptureResponse creates a ResponseCapture that wraps the given ResponseWriter.

func (*ResponseCapture) Hijack

func (w *ResponseCapture) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack supports the http.Hijacker interface.

func (*ResponseCapture) Write

func (w *ResponseCapture) Write(b []byte) (int, error)

Write computes the written len and stores it in ContentLength.

func (*ResponseCapture) WriteHeader

func (w *ResponseCapture) WriteHeader(code int)

WriteHeader records the value of the status code before writing it.

type Sampler

type Sampler interface {
    // Sample returns true if the caller should sample now.
    Sample() bool
}

Sampler is an interface for computing when a sample falls within a range.

func NewAdaptiveSampler

func NewAdaptiveSampler(maxSamplingRate, sampleSize int) Sampler

NewAdaptiveSampler computes the interval for sampling for tracing middleware. it can also be used by non-web go routines to trace internal API calls.

maxSamplingRate is the desired maximum sampling rate in requests per second.

sampleSize sets the number of requests between two adjustments of the sampling rate when MaxSamplingRate is set. the sample rate cannot be adjusted until the sample size is reached at least once.

func NewFixedSampler

func NewFixedSampler(samplingPercent int) Sampler

NewFixedSampler sets the tracing sampling rate as a percentage value.


Generated by godoc2md