middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package middleware builds handler middlewares: logic that runs before and after each invocation of a Lambda handler, the Go side of Powertools' middleware factory.

A handler is any function aws-lambda-go's lambda.Start accepts in the (context, event) -> (result, error) shape; a middleware takes a handler and returns one that wraps it. Every handler-level Powertools utility (the logger's context injection, the tracer's handler capture, metrics flushing, idempotency) is such a middleware, so they stack with each other and with your own:

lambda.Start(middleware.Chain(handle,
	middleware.New("audit", audit),
	logResponse,
))

A middleware that does not care about the event type is a generic function, such as logResponse above, and Chain instantiates it for the handler at hand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func[E, R any] = func(ctx context.Context, event E, next Handler[E, R]) (R, error)

Func is the body of a middleware made with New: it receives the event and the next handler, and decides whether, when and how to call it.

type Handler

type Handler[E, R any] = func(ctx context.Context, event E) (R, error)

Handler is a Lambda handler that takes an event E and returns a result R.

func Chain

func Chain[E, R any](h Handler[E, R], middlewares ...Middleware[E, R]) Handler[E, R]

Chain applies middlewares to h. The first middleware is the outermost: it runs first on the way in and last on the way out.

type Middleware

type Middleware[E, R any] = func(next Handler[E, R]) Handler[E, R]

Middleware wraps a Handler in another one.

func After

func After[E, R any](fn func(ctx context.Context, event E, result R, err error) (R, error)) Middleware[E, R]

After makes a middleware that runs fn after the handler, with what the handler returned; fn returns what the invocation returns.

func Before

func Before[E, R any](fn func(ctx context.Context, event E) error) Middleware[E, R]

Before makes a middleware that runs fn before the handler. An error from fn stops the invocation before the handler runs.

func New

func New[E, R any](name string, fn Func[E, R], opts ...Option) Middleware[E, R]

New makes a Middleware out of fn. The name identifies it in traces and in the debug log.

type Option

type Option func(*config)

Option configures a middleware made with New.

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider sends the spans of WithTracing to tp instead of the global tracer provider.

func WithTracing

func WithTracing(enabled bool) Option

WithTracing records every run of the middleware as a span named "## <name>", so that its overhead shows in the trace. It defaults to the POWERTOOLS_TRACE_MIDDLEWARES environment variable. The spans go to the global OpenTelemetry tracer provider, which the tracing package sets up.

Jump to

Keyboard shortcuts

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