filters

package
v0.9.106 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2017 License: Apache-2.0, MIT Imports: 4 Imported by: 0

Documentation

Overview

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.

Example
package main

import (
	"github.com/zalando/skipper/filters"
	"github.com/zalando/skipper/filters/builtin"
	"github.com/zalando/skipper/proxy"
	"github.com/zalando/skipper/routing"
	"github.com/zalando/skipper/routing/testdataclient"
	"log"
)

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

func (s *customSpec) Name() string {
	return s.name
}

// 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"}
	registry.Register(spec)

	// 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("^/[^/]*", "") ->
			"https://ui.example.org";

		api: Path("/api/*resource") ->
			customFilter("api request") ->
			modPath("^/[^/]*", "") ->
			"https://api.example.org"`)

	if err != nil {
		log.Fatal(err)
	}

	// 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()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

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

Error used in case of invalid filter parameters.

Functions

This section is empty.

Types

type Filter

type Filter interface {

	// The Request method is called while processing the incoming request.
	Request(FilterContext)

	// The Response method is called while processing the response to be
	// returned.
	Response(FilterContext)
}

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
	MarkServed()

	// 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
	Serve(*http.Response)

	// 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 or loopback
	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.)
	SetOutgoingHost(string)

	// 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
}

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

type Metrics added in v0.9.86

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)
}

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 {

	// The name of the Spec is used to identify filters in a route definition.
	Name() string

	// 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.

Directories

Path Synopsis
Package auth implements the basic auth for headers based on "https://github.com/abbot/go-http-auth".
Package auth implements the basic auth for headers based on "https://github.com/abbot/go-http-auth".
Package builtin provides a small, generic set of filters.
Package builtin provides a small, generic set of filters.
Package circuit provides filters to control the circuit breaker settings on the route level.
Package circuit provides filters to control the circuit breaker settings on the route level.
Package cookie implements filters to append to requests or responses.
Package cookie implements filters to append to requests or responses.
Package diag provides a set of network throttling filters for diagnostic purpose.
Package diag provides a set of network throttling filters for diagnostic purpose.
Package filtertest implements mock versions of the Filter, Spec and FilterContext interfaces used during tests.
Package filtertest implements mock versions of the Filter, Spec and FilterContext interfaces used during tests.
Package flowid implements a filter used for identifying incoming requests through their complete lifecycle for logging and monitoring or else.
Package flowid implements a filter used for identifying incoming requests through their complete lifecycle for logging and monitoring or else.
Package ratelimit provides filters to control the rate limitter settings on the route level.
Package ratelimit provides filters to control the rate limitter settings on the route level.
Package serve provides a wrapper of net/http.Handler to be used as a filter.
Package serve provides a wrapper of net/http.Handler to be used as a filter.
Package tee provides a unix-like tee feature for routing.
Package tee provides a unix-like tee feature for routing.

Jump to

Keyboard shortcuts

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