Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthenticateUser = Func(func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, ctx, err := user.ExtractOrgIDFromHTTPRequest(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusUnauthorized)
			return
		}
		next.ServeHTTP(w, r.WithContext(ctx))
	})
})

    AuthenticateUser propagates the user ID from HTTP headers back to the request's context.

    View Source
    var BodySizeBuckets = []float64{1 * mb, 2.5 * mb, 5 * mb, 10 * mb, 25 * mb, 50 * mb, 100 * mb, 250 * mb}

      BodySizeBuckets defines buckets for request/response body sizes.

      View Source
      var Logging = Log{
      	Log: logging.Global(),
      }

        Logging middleware logs each HTTP request method, path, response code and duration for all HTTP requests.

        Functions

        func ClientUserHeaderInterceptor

        func ClientUserHeaderInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error

          ClientUserHeaderInterceptor propagates the user ID from the context to gRPC metadata, which eventually ends up as a HTTP2 header.

          func ExtractSampledTraceID

          func ExtractSampledTraceID(ctx context.Context) (string, bool)

            ExtractSampledTraceID works like ExtractTraceID but the returned bool is only true if the returned trace id is sampled.

            func ExtractTraceID

            func ExtractTraceID(ctx context.Context) (string, bool)

              ExtractTraceID extracts the trace id, if any from the context.

              func IsWSHandshakeRequest

              func IsWSHandshakeRequest(req *http.Request) bool

                IsWSHandshakeRequest returns true if the given request is a websocket handshake request.

                func MakeLabelValue

                func MakeLabelValue(path string) string

                  MakeLabelValue converts a Gorilla mux path to a string suitable for use in a Prometheus label value.

                  func NewStatsHandler

                  func NewStatsHandler(receivedPayloadSize, sentPayloadSize *prometheus.HistogramVec, inflightRequests *prometheus.GaugeVec) stats.Handler

                    NewStatsHandler creates handler that can be added to gRPC server options to track received and sent message sizes.

                    func ServerUserHeaderInterceptor

                    func ServerUserHeaderInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

                      ServerUserHeaderInterceptor propagates the user ID from the gRPC metadata back to our context.

                      func StreamClientUserHeaderInterceptor

                      func StreamClientUserHeaderInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error)

                        StreamClientUserHeaderInterceptor propagates the user ID from the context to gRPC metadata, which eventually ends up as a HTTP2 header. For streaming gRPC requests.

                        func StreamServerInstrumentInterceptor

                        func StreamServerInstrumentInterceptor(hist *prometheus.HistogramVec) grpc.StreamServerInterceptor

                          StreamServerInstrumentInterceptor instruments gRPC requests for errors and latency.

                          func StreamServerUserHeaderInterceptor

                          func StreamServerUserHeaderInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error

                            StreamServerUserHeaderInterceptor propagates the user ID from the gRPC metadata back to our context.

                            func UnaryServerInstrumentInterceptor

                            func UnaryServerInstrumentInterceptor(hist *prometheus.HistogramVec) grpc.UnaryServerInterceptor

                              UnaryServerInstrumentInterceptor instruments gRPC requests for errors and latency.

                              Types

                              type ErrorHandler

                              type ErrorHandler struct {
                              	Code    int
                              	Handler http.Handler
                              }

                                ErrorHandler lets you call an alternate http handler upon a certain response code. Note it will assume a 200 if the wrapped handler does not write anything

                                func (ErrorHandler) Wrap

                                func (e ErrorHandler) Wrap(next http.Handler) http.Handler

                                  Wrap implements Middleware

                                  type Func

                                  type Func func(http.Handler) http.Handler

                                    Func is to Interface as http.HandlerFunc is to http.Handler

                                    func (Func) Wrap

                                    func (m Func) Wrap(next http.Handler) http.Handler

                                      Wrap implements Interface

                                      type GRPCServerLog

                                      type GRPCServerLog struct {
                                      	Log logging.Interface
                                      	// WithRequest will log the entire request rather than just the error
                                      	WithRequest bool
                                      }

                                        GRPCServerLog logs grpc requests, errors, and latency.

                                        func (GRPCServerLog) StreamServerInterceptor

                                        func (s GRPCServerLog) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

                                          StreamServerInterceptor returns an interceptor that logs gRPC requests

                                          func (GRPCServerLog) UnaryServerInterceptor

                                          func (s GRPCServerLog) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

                                            UnaryServerInterceptor returns an interceptor that logs gRPC requests

                                            type HeaderAdder

                                            type HeaderAdder struct {
                                            	http.Header
                                            }

                                              HeaderAdder adds headers to responses

                                              func (HeaderAdder) Wrap

                                              func (h HeaderAdder) Wrap(next http.Handler) http.Handler

                                                Wrap implements Middleware

                                                type Instrument

                                                type Instrument struct {
                                                	RouteMatcher     RouteMatcher
                                                	Duration         *prometheus.HistogramVec
                                                	RequestBodySize  *prometheus.HistogramVec
                                                	ResponseBodySize *prometheus.HistogramVec
                                                	InflightRequests *prometheus.GaugeVec
                                                }

                                                  Instrument is a Middleware which records timings for every HTTP request

                                                  func (Instrument) Wrap

                                                  func (i Instrument) Wrap(next http.Handler) http.Handler

                                                    Wrap implements middleware.Interface

                                                    type Interface

                                                    type Interface interface {
                                                    	Wrap(http.Handler) http.Handler
                                                    }

                                                      Interface is the shared contract for all middlesware, and allows middlesware to wrap handlers.

                                                      var Identity Interface = Func(func(h http.Handler) http.Handler { return h })

                                                        Identity is an Interface which doesn't do anything.

                                                        func Merge

                                                        func Merge(middlesware ...Interface) Interface

                                                          Merge produces a middleware that applies multiple middlesware in turn; ie Merge(f,g,h).Wrap(handler) == f.Wrap(g.Wrap(h.Wrap(handler)))

                                                          func PathReplace

                                                          func PathReplace(replacement string) Interface

                                                            PathReplace replcase Request.RequestURI with the specified string.

                                                            func PathRewrite

                                                            func PathRewrite(regexp *regexp.Regexp, replacement string) Interface

                                                              PathRewrite supports regex matching and replace on Request URIs

                                                              type Log

                                                              type Log struct {
                                                              	Log               logging.Interface
                                                              	LogRequestHeaders bool // LogRequestHeaders true -> dump http headers at debug log level
                                                              	SourceIPs         *SourceIPExtractor
                                                              }

                                                                Log middleware logs http requests

                                                                func (Log) Wrap

                                                                func (l Log) Wrap(next http.Handler) http.Handler

                                                                  Wrap implements Middleware

                                                                  type RouteMatcher

                                                                  type RouteMatcher interface {
                                                                  	Match(*http.Request, *mux.RouteMatch) bool
                                                                  }

                                                                    RouteMatcher matches routes

                                                                    type SourceIPExtractor

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

                                                                      SourceIPExtractor extracts the source IPs from a HTTP request

                                                                      func NewSourceIPs

                                                                      func NewSourceIPs(header, regex string) (*SourceIPExtractor, error)

                                                                        NewSourceIPs creates a new SourceIPs

                                                                        func (SourceIPExtractor) Get

                                                                        func (sips SourceIPExtractor) Get(req *http.Request) string

                                                                          Get returns any source addresses we can find in the request, comma-separated

                                                                          type Tracer

                                                                          type Tracer struct {
                                                                          	RouteMatcher RouteMatcher
                                                                          	SourceIPs    *SourceIPExtractor
                                                                          }

                                                                            Tracer is a middleware which traces incoming requests.

                                                                            func (Tracer) Wrap

                                                                            func (t Tracer) Wrap(next http.Handler) http.Handler

                                                                              Wrap implements Interface

                                                                              Directories

                                                                              Path Synopsis