Documentation
¶
Index ¶
- func Middleware(opts ...Option) gin.HandlerFunc
- func NewContext(ctx context.Context, evt *Event) context.Context
- type DevEmitterOption
- type Emitter
- type EmitterFunc
- type Event
- func (e *Event) Bool(key string, value bool) *Event
- func (e *Event) Dur(key string, value time.Duration) *Event
- func (e *Event) Emit()
- func (e *Event) Err(key string, err error) *Event
- func (e *Event) Failure(err error) *Event
- func (e *Event) Fields() []Field
- func (e *Event) FieldsMap() map[string]any
- func (e *Event) Float64(key string, value float64) *Event
- func (e *Event) HasError() bool
- func (e *Event) Int(key string, value int) *Event
- func (e *Event) Int64(key string, value int64) *Event
- func (e *Event) Interface(key string, value any) *Event
- func (e *Event) Set(key string, value any) *Event
- func (e *Event) StatusCode() int
- func (e *Event) Str(key, value string) *Event
- func (e *Event) Success() *Event
- func (e *Event) Time(key string, value time.Time) *Event
- func (e *Event) UUID(key string, value fmt.Stringer) *Event
- func (e *Event) UserID(id fmt.Stringer) *Event
- func (e *Event) UserName(name string) *Event
- func (e *Event) UserRole(role string) *Event
- type Field
- type Option
- type Sampler
- type SamplerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(opts ...Option) gin.HandlerFunc
Middleware returns a Gin middleware that creates a wide event per request, enriches it with request/response metadata, and emits it after the handler completes.
Types ¶
type DevEmitterOption ¶
type DevEmitterOption func(*devEmitterConfig)
DevEmitterOption configures a dev emitter.
func WithCategory ¶
func WithCategory(name string, pathPrefixes ...string) DevEmitterOption
WithCategory groups request paths under a named category using prefix matching. Events whose request.path starts with any of the given prefixes are assigned to this category. Categories can be displayed in the header and selectively muted via WithMuteCategories.
func WithColor ¶
func WithColor(enabled bool) DevEmitterOption
WithColor forces color output on or off, overriding TTY auto-detection.
func WithDurationThresholds ¶
func WithDurationThresholds(warn, err time.Duration) DevEmitterOption
WithDurationThresholds configures the color scale for duration values in the footer. Durations below warn are green, between warn and err are yellow, at or above err are red. Default thresholds are 500ms (warn) and 2s (error).
func WithMuteCategories ¶
func WithMuteCategories(names ...string) DevEmitterOption
WithMuteCategories silently drops events belonging to the named categories from dev output. Events in muted categories produce no output at all.
type Emitter ¶
type Emitter interface {
Emit(evt *Event)
}
Emitter defines the interface for outputting wide events.
func DevStdoutEmitter ¶
func DevStdoutEmitter(opts ...DevEmitterOption) Emitter
DevStdoutEmitter returns an Emitter that writes human-readable, colorized wide events to stdout using unicode tree-line formatting. Colors are auto-detected based on whether stdout is a terminal.
func DevWriterEmitter ¶
func DevWriterEmitter(w io.Writer, opts ...DevEmitterOption) Emitter
DevWriterEmitter returns a dev emitter that writes to the given writer. Colors are disabled by default; use WithColor(true) to force them on.
func JSONStdoutEmitter ¶
func JSONStdoutEmitter() Emitter
JSONStdoutEmitter returns an Emitter that writes one JSON line per event to stdout.
func JSONWriterEmitter ¶
JSONWriterEmitter returns an Emitter that writes one JSON line per event to the given writer.
func MultiEmitter ¶
MultiEmitter fans out to multiple emitters. All emitters receive every event.
type EmitterFunc ¶
type EmitterFunc func(evt *Event)
EmitterFunc is an adapter to allow the use of ordinary functions as Emitters.
func (EmitterFunc) Emit ¶
func (f EmitterFunc) Emit(evt *Event)
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a single wide event that accumulates structured fields throughout a unit of work and emits them as one rich log line.
func Begin ¶
Begin creates a new standalone event for background tasks or cron jobs. The caller is responsible for calling Emit() when the work is done.
func FromContext ¶
FromContext retrieves the event from a standard context. Returns nil if no event is stored.
func FromGin ¶
FromGin retrieves the event from a Gin context. It first checks the Gin key store, then falls back to the request context. Returns nil if no event is found.
func (*Event) Emit ¶
func (e *Event) Emit()
Emit finalizes the event and sends it to the emitter. Automatically sets the duration field. Safe to call multiple times; only the first call emits.
func (*Event) FieldsMap ¶
FieldsMap returns all fields as a nested map, expanding dot-separated keys. This is a convenience method for custom emitters that need structured data.
func (*Event) HasError ¶
HasError returns true if the event has an outcome of "failure" or a response status >= 500. Like the emitted output, it reads the latest value of each field.
func (*Event) Set ¶
Set adds a field with any value. The key can contain dots for nested JSON output.
func (*Event) StatusCode ¶
StatusCode returns the latest response status code, or 0 if not set.
type Option ¶
type Option func(*middlewareConfig)
Option configures the wide event middleware.
func WithEmitter ¶
WithEmitter sets the emitter for the middleware. Default: JSONStdoutEmitter.
func WithPostEnricher ¶
WithPostEnricher sets a function called after the handler to add fields.
func WithPreEnricher ¶
WithPreEnricher sets a function called before the handler to add fields.
func WithSampler ¶
WithSampler sets the tail sampler. Default: AlwaysSample.
func WithSkipPaths ¶
WithSkipPaths sets paths that should not generate wide events.
type Sampler ¶
Sampler decides whether a completed event should be emitted.
func AlwaysOnError ¶
func AlwaysOnError() Sampler
AlwaysOnError returns a sampler that emits if the event has an error.
func AlwaysOnStatus ¶
AlwaysOnStatus returns a sampler that emits if the response status matches any of the given codes.
func CompositeSampler ¶
CompositeSampler returns a sampler that emits if ANY of the given samplers say yes (OR logic).
func Probability ¶
Probability returns a sampler that emits with the given probability (0.0 to 1.0).
type SamplerFunc ¶
SamplerFunc is an adapter to allow the use of ordinary functions as Samplers.
func (SamplerFunc) ShouldSample ¶
func (f SamplerFunc) ShouldSample(evt *Event) bool