Back to

Package filters

Latest Go to latest
Published: 4 days ago | Licenses: Apache-2.0 , MIT | Module:


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.



// Copyright 2015 Zalando SE
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package filters_test

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 Example() {
	// 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


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"


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

Error used in case of invalid filter parameters.

type Filter

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

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

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

type Registry map[string]Spec

Registry used to lookup Spec objects while initializing routes.

func (Registry) Register

func (r Registry) Register(s Spec)

Registers a filter specification.

type Spec

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.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier