middleware

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxBodyBytes = 1 << 20 // 1 MiB

DefaultMaxBodyBytes is the default cap for captured request/response body size. Bodies larger than this are truncated with a marker suffix to avoid unbounded memory growth.

Variables

This section is empty.

Functions

func NewProfilingHTTPClient

func NewProfilingHTTPClient(profiler *profiling.Profiler) *http.Client

NewProfilingHTTPClient creates an HTTP client with profiling support

func TraceMiddleware

func TraceMiddleware(name string) func(http.Handler) http.Handler

TraceMiddleware wraps a handler with automatic tracing

func WithTracer

func WithTracer(ctx context.Context, tracer *RequestTracer) context.Context

WithTracer adds a tracer to the context

func Wrap

func Wrap(handler http.Handler, st store.Store, logRequestBody, logResponseBody bool, pathMatcher PathMatcher) http.Handler

Wrap wraps an http.Handler with the request visualization middleware

func WrapWithLimits

func WrapWithLimits(handler http.Handler, st store.Store, logRequestBody, logResponseBody bool, pathMatcher PathMatcher, maxBody int, sampleRate float64) http.Handler

WrapWithLimits is identical to Wrap but allows the caller to specify the maximum number of captured body bytes (per request and per response, <= 0 disables the cap) and the sampling rate (0..1; requests that lose the coin toss pass through uncaptured).

func WrapWithProfiling

func WrapWithProfiling(handler http.Handler, st store.Store, logRequestBody, logResponseBody bool, pathMatcher PathMatcher, profiler *profiling.Profiler) http.Handler

WrapWithProfiling wraps an http.Handler with request visualization and performance profiling.

func WrapWithProfilingAndLimits

func WrapWithProfilingAndLimits(handler http.Handler, st store.Store, logRequestBody, logResponseBody bool, pathMatcher PathMatcher, profiler *profiling.Profiler, maxBody int, sampleRate float64) http.Handler

WrapWithProfilingAndLimits is identical to WrapWithProfiling but exposes the captured-body size cap.

Types

type HTTPRoundTripper

type HTTPRoundTripper struct {
	Transport http.RoundTripper
	Profiler  *profiling.Profiler
}

HTTPRoundTripper is a profiling HTTP round tripper for outgoing requests

func (*HTTPRoundTripper) RoundTrip

func (rt *HTTPRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper with profiling

type LogCollector

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

LogCollector accumulates log lines emitted during one request.

func LogCollectorFromContext

func LogCollectorFromContext(ctx context.Context) *LogCollector

LogCollectorFromContext returns the request's collector, or nil when the context does not belong to a captured request.

func WithLogCollector

func WithLogCollector(ctx context.Context) (context.Context, *LogCollector)

WithLogCollector attaches a fresh collector to ctx and returns both.

func (*LogCollector) Append

func (c *LogCollector) Append(e store.LogEntry)

Append records one log line, dropping beyond the per-request cap.

func (*LogCollector) Snapshot

func (c *LogCollector) Snapshot() []store.LogEntry

Snapshot returns the collected lines. A trailing marker entry reports how many lines were dropped, if any.

type MiddlewareStackKey

type MiddlewareStackKey struct{}

MiddlewareStackKey is the context key user middleware can write to in order to surface custom middleware-stack information into the dashboard's "middleware trace" view. Expected value type: map[string]interface{} with a "stack" key whose value is []map[string]interface{}.

type PathMatcher

type PathMatcher interface {
	ShouldIgnorePath(path string) bool
}

PathMatcher defines an interface for checking if a path should be ignored

type ProfilingConfig

type ProfilingConfig struct {
	Enabled       bool
	ProfileType   profiling.ProfileType
	Threshold     time.Duration
	CaptureTraces bool
}

ProfilingConfig contains configuration for profiling middleware

type RequestTracer

type RequestTracer struct {
	RequestID string       `json:"request_id"`
	StartTime time.Time    `json:"start_time"`
	EndTime   time.Time    `json:"end_time"`
	Traces    []TraceEntry `json:"traces"`
	// contains filtered or unexported fields
}

RequestTracer tracks execution through middleware stack

func GetTracer

func GetTracer(ctx context.Context) *RequestTracer

GetTracer gets the tracer from context

func NewRequestTracer

func NewRequestTracer(requestID string) *RequestTracer

NewRequestTracer creates a new request tracer

func (*RequestTracer) Complete

func (rt *RequestTracer) Complete()

Complete marks the tracer as complete

func (*RequestTracer) EndTrace

func (rt *RequestTracer) EndTrace(err error)

EndTrace ends the current trace entry

func (*RequestTracer) GetStackTrace

func (rt *RequestTracer) GetStackTrace() string

GetStackTrace captures current goroutine stack

func (*RequestTracer) GetTraces

func (rt *RequestTracer) GetTraces() []TraceEntry

GetTraces returns all traces

func (*RequestTracer) RecordCustom

func (rt *RequestTracer) RecordCustom(name string, details interface{})

RecordCustom records a custom event

func (*RequestTracer) RecordHTTP

func (rt *RequestTracer) RecordHTTP(method, url string, duration time.Duration, status int, err error)

RecordHTTP records an HTTP call

func (*RequestTracer) RecordSQL

func (rt *RequestTracer) RecordSQL(query string, duration time.Duration, rows int, err error)

RecordSQL records a SQL query execution

func (*RequestTracer) StartTrace

func (rt *RequestTracer) StartTrace(name, traceType string, details interface{})

StartTrace starts a new trace entry

type RouteTraceKey

type RouteTraceKey struct{}

RouteTraceKey is the context key user middleware can write to in order to attach a JSON-encoded route descriptor to the request log. Expected value type: string (JSON-encoded object).

type TraceEntry

type TraceEntry struct {
	Name      string        `json:"name"`
	Type      string        `json:"type"` // "middleware", "handler", "sql", "http", "custom"
	StartTime time.Time     `json:"start_time"`
	EndTime   time.Time     `json:"end_time"`
	Duration  time.Duration `json:"duration"`
	Status    string        `json:"status"` // "running", "completed", "error"
	Error     string        `json:"error,omitempty"`
	Details   interface{}   `json:"details,omitempty"`
	Children  []TraceEntry  `json:"children,omitempty"`
}

TraceEntry represents a single trace entry in the middleware stack

type TracerContextKey

type TracerContextKey struct{}

TracerContextKey is the public context key used to attach a *RequestTracer to a request context. External packages (e.g. internal/profiling) read this key, so it must remain exported.

Jump to

Keyboard shortcuts

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