Documentation ¶
Overview ¶
Package middlewares provides middleware function for decorating HTTP handlers.
Index ¶
- func Apply(h http.Handler, middlewares ...Middleware) http.Handler
- func Client() *statsd.Client
- func ContextWithSiteAndUserInfo(ctx context.Context, userID string, orgID string, subdomain string, admin bool) context.Context
- func ContextWithUser(ctx context.Context, userID string, orgID string, admin bool) context.Context
- func DataDogEventLogger(l *zap.Logger, sc *statsd.Client, level zapcore.Level) *zap.Logger
- func GetLogLevel(level string) zapcore.Level
- func InitClient(host string, port string, prefix string, globalTags []string) error
- func IsAdminFromContext(ctx context.Context) bool
- func NewStandardZapConfig() zap.Config
- func NewStandardZapLevelConfig(level zapcore.Level) zap.Config
- func OrgIDFromContext(ctx context.Context) (string, error)
- func SubdomainFromContext(ctx context.Context) (string, error)
- func UserIDFromContext(ctx context.Context) (string, error)
- type DataDogWriter
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply(h http.Handler, middlewares ...Middleware) http.Handler
Apply wraps a list of middlewares around a handler and returns it
func Client ¶ added in v1.0.6
Client returns a pointer to the dogstatsd Client, useful for logging metrics
func ContextWithSiteAndUserInfo ¶ added in v1.0.16
func ContextWithSiteAndUserInfo(ctx context.Context, userID string, orgID string, subdomain string, admin bool) context.Context
ContextWithSiteAndUserInfo places a user ID value, site Id, site subdomain, and admin bool into a context using the same context user key
func ContextWithUser ¶ added in v1.0.14
ContextWithUser places a user ID value, org Id value, and admin bool into a context using the same context user key
func DataDogEventLogger ¶ added in v1.0.11
DataDogEventLogger will take an already-constructed zap.Logger and a datadog statsd.Client and return a Logger that will also "tee" its output to DataDog Events.
func GetLogLevel ¶ added in v1.0.18
GetLogLevel get a zapcore level from an string
levels can be debug, info, warning, error, dpanic, panic, fatal
func InitClient ¶ added in v1.0.6
InitClient accepts config parameters and builds the dogstatsd Client, accessible by the getter Client() An error is returned only if a problem is encountered setting up the Client.
func IsAdminFromContext ¶ added in v1.0.14
IsAdminFromContext returns a boolean indicating whether the user is an admin or not
func NewStandardZapConfig ¶ added in v1.0.11
NewStandardZapConfig returns a sensible [config](https://godoc.org/go.uber.org/zap#Config) for a Zap logger.
func NewStandardZapLevelConfig ¶ added in v1.0.11
NewStandardZapLevelConfig returns a sensible [config](https://godoc.org/go.uber.org/zap#Config) for a Zap logger. @param level - required, a level at or above which the logger will record messages
func OrgIDFromContext ¶ added in v1.0.14
OrgIDFromContext retrieves an organization ID value stored in a context
func SubdomainFromContext ¶ added in v1.0.16
SubdomainFromContext retrieves an organization's subdomain value stored in a context
Types ¶
type DataDogWriter ¶ added in v1.0.11
type DataDogWriter struct {
// contains filtered or unexported fields
}
DataDogWriter implements io.Writer. It should be made into a [WriteSyncer](https://godoc.org/go.uber.org/zap/zapcore#WriteSyncer) for sending Zap logs to DataDog as [Events](https://godoc.org/github.com/DataDog/datadog-go/statsd#Event), using datadog-go/statsd [Client.Event()](https://godoc.org/github.com/DataDog/datadog-go/statsd#Client.Event).
func (DataDogWriter) Write ¶ added in v1.0.11
func (d DataDogWriter) Write(p []byte) (n int, err error)
Write will take a JSON-formatted log message from zap.L().[Level]() as a byte slice, format it, and send it as an Event to d.client.Event(). @param p []byte - the byte array of the buffered log message.
type Middleware ¶
Middleware is a type for decorating requests.
func AccessControlAllowOrigin ¶
func AccessControlAllowOrigin(origin string) Middleware
AccessControlAllowOrigin is a middleware for adding an access control header to requests
func AddHeaders ¶
func AddHeaders(headers map[string]string) Middleware
AddHeaders is a middleware for adding arbitrary headers
func HandleGrpc ¶ added in v1.0.5
func HandleGrpc(server *grpc.Server) Middleware
HandleGrpc passes gRPC requests to the given *grpc.Server and passes http requests on to the http handler
func InstrumentRoute ¶
func InstrumentRoute() Middleware
InstrumentRoute is a middleware for adding metrics to a route. The following metrics are added:
# Counter http_request_count{"verb", "path"} # Counter http_request_status_%s{"verb", "path"} // where %s is each specific HTTP status code # Histogram http_request_duration{"verb", "path"}
func Logging ¶
func Logging(closures ...func(*http.Request) []zapcore.Field) Middleware
Logging is a mux middleware for adding a request log. Logs contains the following fields: level, timestamp, response_time, message, path, method, status, query, remote_addr, user_agent, and body_bytes.
Logging accepts an optional list of closures that accept the incoming request and return a slice of zapcore.Field. Each closure is evaluated and its response fields are appended to the logged message after the request is handled