sampling

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package sampling provides a handler for sampling records at request level.

It discards records with lower than the minimum level if request is unsampled. For example, if the minimum level is slog.LevelError, it logs records with slog.LevelError and above regardless, but discards records with slog.LevelWarn and below unless the request is sampled.

It's ok to discard records with lower level if everything is fine. However, if a record with slog.LevelError logs, it's better to log records with slog.LevelWarn and below around it so developers could have a context for debugging even the request is not sampled. To achieve this, Handler.WithBuffer should be called at the beginning interceptor of the gRPC/HTTP request.

ctx, cancel := h.WithBuffer(ctx)
defer cancel()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithBuffer added in v0.3.0

func WithBuffer(ctx context.Context) (context.Context, func())

WithBuffer enables log buffering for the request associated with the given context. It usually should be called at the beginning interceptor of the gRPC/HTTP request.

Canceling this context releases buffer associated with it, so code should call cancel as soon as the operations running in this [Context] complete:

ctx, cancel := h.WithBuffer(ctx)
defer cancel()

Types

type Handler

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

Handler samples records according to the give sampler.

To create a new Handler, call New.

func New

func New(handler slog.Handler, sampler func(ctx context.Context) bool, opts ...Option) Handler

New creates a new Handler with the given Option(s).

func (Handler) Enabled

func (h Handler) Enabled(ctx context.Context, level slog.Level) bool

func (Handler) Handle

func (h Handler) Handle(ctx context.Context, record slog.Record) error

func (Handler) WithAttrs

func (h Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (Handler) WithGroup

func (h Handler) WithGroup(name string) slog.Handler

type Option

type Option func(*options)

Option configures the Handler with specific options.

func WithLevel

func WithLevel(level slog.Level) Option

WithLevel provides the minimum record level that will be logged without sampling. It discards unsampled records with lower level unless the buffer is activated by Handler.WithBuffer.

The default minimum record level is slog.LevelError.

Jump to

Keyboard shortcuts

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