skipper: Index | Examples | Files | Directories

package filters

import ""

Package filters contains definitions for skipper filtering and a default, built-in set of filters.

Filters are used to augment both the inbound request's attributes before forwarding it to the route endpoint, and the outbound response's attributes before returning it to the original client.

Filter Specification and Filter Instances

Filter implementations are based on filter specifications that provide a filter name and a 'factory' method to create filter instances. The filter name is used to identify a filter in a route definition. The filter specifications can be used by multiple routes, while the filter instances belong to a single route. Filter instances are created while the route definitions are parsed and initialized, based on the specifications stored in the filter registry. Different filter instances can be created with different parameters.

Filtering and FilterContext

Once a route is identified during request processing, a context object is created that is unique to the request, holding the current request, the response (once it is available), and some further information and state related to the current flow.

Each filter in a route is called twice, once for the request in the order of their position in the route definition, and once for the response in reverse order.

Handling Requests with Filters

Filters can handle the requests themselves, meaning that they can set the response status, headers and send any particular response body. In this case, it is the filter's responsibility to mark the request as served to avoid generating the default response.

If a filter replaces the response body, it is the filter's responsibility to close the original body.


package main

import (

type customSpec struct{ name string }
type customFilter struct{ prefix string }

func (s *customSpec) Name() string {

// a specification can be used to create filter instances with different config
func (s *customSpec) CreateFilter(config []interface{}) (filters.Filter, error) {
    if len(config) == 0 {
        return nil, filters.ErrInvalidFilterParameters

    prefix, ok := config[0].(string)
    if !ok {
        return nil, filters.ErrInvalidFilterParameters

    return &customFilter{prefix}, nil

// a simple filter logging the request URLs
func (f *customFilter) Request(ctx filters.FilterContext) {
    log.Println(f.prefix, ctx.Request().URL)

func (f *customFilter) Response(_ filters.FilterContext) {}

func main() {
    // create registry
    registry := builtin.MakeRegistry()

    // create and register the filter specification
    spec := &customSpec{name: "customFilter"}

    // create simple data client, with route entries referencing 'customFilter',
    // and clipping part of the request path:
    dataClient, err := testdataclient.NewDoc(`

		ui: Path("/ui/*page") ->
			customFilter("ui request") ->
			modPath("^/[^/]*", "") ->

		api: Path("/api/*resource") ->
			customFilter("api request") ->
			modPath("^/[^/]*", "") ->

    if err != nil {

    // create routing object:
    rt := routing.New(routing.Options{
        FilterRegistry: registry,
        DataClients:    []routing.DataClient{dataClient}})
    defer rt.Close()

    // create http.Handler:
    p := proxy.New(rt, proxy.OptionsNone)
    defer p.Close()



Package Files

doc.go filters.go


const (
    // DynamicBackendHostKey is the key used in the state bag to pass host to the proxy.
    DynamicBackendHostKey = "backend:dynamic:host"

    // DynamicBackendSchemeKey is the key used in the state bag to pass scheme to the proxy.
    DynamicBackendSchemeKey = "backend:dynamic:scheme"

    // DynamicBackendURLKey is the key used in the state bag to pass url to the proxy.
    DynamicBackendURLKey = "backend:dynamic:url"

    // BackendIsProxyKey is the key used in the state bag to notify proxy that the backend is also a proxy.
    BackendIsProxyKey = "backend:isproxy"

    // BackendTimeout is the key used in the state bag to configure backend timeout in proxy
    BackendTimeout = "backend:timeout"


var ErrInvalidFilterParameters = errors.New("invalid filter parameters")

ErrInvalidFilterParameters is used in case of invalid filter parameters.

type Filter Uses

type Filter interface {
    // The Request method is called while processing the incoming request.

    // The Response method is called while processing the response to be
    // returned.

Filters are created by the Spec components, optionally using filter specific settings. When implementing filters, it needs to be taken into consideration, that filter instances are route specific and not request specific, so any state stored with a filter is shared between all requests for the same route and can cause concurrency issues.

type FilterContext Uses

type FilterContext interface {
    // The response writer object belonging to the incoming request. Used by
    // filters that handle the requests themselves.
    ResponseWriter() http.ResponseWriter

    // The incoming request object. It is forwarded to the route endpoint
    // with its properties changed by the filters.
    Request() *http.Request

    // The response object. It is returned to the client with its
    // properties changed by the filters.
    Response() *http.Response

    // The copy (deep) of the original incoming request or nil if the
    // implementation does not provide it.
    // The object received from this method contains an empty body, and all
    // payload related properties have zero value.
    OriginalRequest() *http.Request

    // The copy (deep) of the original incoming response or nil if the
    // implementation does not provide it.
    // The object received from this method contains an empty body, and all
    // payload related properties have zero value.
    OriginalResponse() *http.Response

    // This method is deprecated. A FilterContext implementation should flag this state
    // internally
    Served() bool

    // This method is deprecated. You should call Serve providing the desired response

    // Serve a request with the provided response. It can be used by filters that handle the requests
    // themselves. FilterContext implementations should flag this state and prevent the filter chain
    // from continuing

    // Provides the wildcard parameter values from the request path by their
    // name as the key.
    PathParam(string) string

    // Provides a read-write state bag, unique to a request and shared by all
    // the filters in the route.
    StateBag() map[string]interface{}

    // Gives filters access to the backend url specified in the route or an empty
    // value in case it's a shunt, loopback. In case of dynamic backend is empty.
    BackendUrl() string

    // Returns the host that will be set for the outgoing proxy request as the
    // 'Host' header.
    OutgoingHost() string

    // Allows explicitly setting the Host header to be sent to the backend, overriding the
    // strategy used by the implementation, which can be either the Host header from the
    // incoming request or the host fragment of the backend url.
    // Filters that need to modify the outgoing 'Host' header, need to use
    // this method instead of setting the Request().Headers["Host"] value.
    // (The requestHeader filter automatically detects if the header name
    // is 'Host' and calls this method.)

    // Allow filters to collect metrics other than the default metrics (Filter Request, Filter Response methods)
    Metrics() Metrics

    // Allow filters to add Tags, Baggage to the trace or set the ComponentName.
    Tracer() opentracing.Tracer

    // Allow filters to create their own spans
    ParentSpan() opentracing.Span

    // Returns a clone of the FilterContext including a brand new request object.
    // The stream body of the new request is shared with the original.
    // Whenever the request body of the original request is read, the body of the
    // new request body is written.
    // The StateBag and filterMetrics object are not preserved in the new context.
    // Therefore, you can't access state bag values set in the previous context.
    Split() (FilterContext, error)

    // Performs a new route lookup and executes the matched route if any

Context object providing state and information that is unique to a request.

type Metrics Uses

type Metrics interface {
    // MeasureSince adds values to a timer with a custom key.
    MeasureSince(key string, start time.Time)

    // IncCounter increments a custom counter identified by its key.
    IncCounter(key string)

    // IncCounterBy increments a custom counter identified by its key by a certain value.
    IncCounterBy(key string, value int64)

    // IncFloatCounterBy increments a custom counter identified by its key by a certain
    // float (decimal) value. IMPORTANT: Not all Metrics implementation support float
    // counters. In that case, a call to IncFloatCounterBy is dropped.
    IncFloatCounterBy(key string, value float64)

Metrics provides possibility to use custom metrics from filter implementations. The custom metrics will be exposed by the common metrics endpoint exposed by the proxy, where they can be accessed by the custom key prefixed by the filter name and the string 'custom'. E.g: <filtername>.custom.<customkey>.

type Registry Uses

type Registry map[string]Spec

Registry used to lookup Spec objects while initializing routes.

func (Registry) Register Uses

func (r Registry) Register(s Spec)

Registers a filter specification.

type Spec Uses

type Spec interface {
    // Name gives the name of the Spec. It is used to identify filters in a route definition.
    Name() string

    // CreateFilter creates a Filter instance. Called with the parameters in the route
    // definition while initializing a route.
    CreateFilter(config []interface{}) (Filter, error)

Spec objects are specifications for filters. When initializing the routes, the Filter instances are created using the Spec objects found in the registry.


accesslogPackage accesslog provides request filters that give the ability to override AccessLogDisabled setting.
apiusagemonitoringPackage apiusagemonitoring provides filters gathering metrics around API calls
authPackage auth provides authentication related filters.
builtinPackage builtin provides a small, generic set of filters.
circuitPackage circuit provides filters to control the circuit breaker settings on the route level.
cookiePackage cookie implements filters to append to requests or responses.
corsPackage cors implements the origin header for CORS.
diagPackage diag provides a set of network throttling filters for diagnostic purpose.
filtertestPackage filtertest implements mock versions of the Filter, Spec and FilterContext interfaces used during tests.
flowidPackage flowid implements a filter used for identifying incoming requests through their complete lifecycle for logging and monitoring or else.
logPackage log provides a request logging filter, usable also for audit logging.
ratelimitPackage ratelimit provides filters to control the rate limitter settings on the route level.
schedulerPackage scheduler implements filter logic that changes the http request scheduling behavior of the proxy.
sedPackage sed provides stream editor filters for request and response payload.
servePackage serve provides a wrapper of net/http.Handler to be used as a filter.
teePackage tee provides a unix-like tee feature for routing.
tracingPackage tracing provides filters to instrument distributed tracing.

Package filters imports 4 packages (graph) and is imported by 125 packages. Updated 2021-01-12. Refresh now. Tools for package owners.