slogsampling

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: MIT Imports: 5 Imported by: 6

README

Slog sampling policy

tag Go Version GoDoc Build Status Go report Coverage Contributors License

A middleware that samples incoming records which caps the CPU and I/O load of logging while attempting to preserve a representative subset of your logs.

See also:

🚀 Install

go get github.com/samber/slog-sampling

Compatibility: go >= 1.20.3

This library is v0 and follows SemVer strictly. On slog final release (go 1.21), this library will go v1.

No breaking changes will be made to exported APIs before v1.0.0.

💡 Usage

GoDoc: https://pkg.go.dev/github.com/samber/slog-sampling

The sampling middleware can be used standalone or with slog-multi helper.

Handler options
type Option struct {
	// This will log the first `First` log entries with the same level and message
	// in a `Tick` interval as-is. Following that, it will allow through
	// every `Thereafter`th log entry with the same level and message in that interval.
	Tick       time.Duration
	First      uint64
	Thereafter uint64

    // Optional hooks
	OnAccepted func(context.Context, slog.Record)
	OnDropped  func(context.Context, slog.Record)
}

If Thereafter is zero, the middleware will drop all log entries after the first First records in that interval.

Using slog-multi
import (
	slogmulti "github.com/samber/slog-multi"
	slogsampling "github.com/samber/slog-sampling"
	"golang.org/x/exp/slog"
)

// Will print the first 10 entries having the same level+message, then every 10th messages until next interval.
option := slogsampling.Option{
    Tick:       5 * time.Second,
    First:      10,
    Thereafter: 10,

    OnAccepted: func(context.Context, slog.Record) {
        // ...
    },
    OnDropped: func(context.Context, slog.Record) {
        // ...
    },
}

logger := slog.New(
    slogmulti.
        Pipe(option.NewSamplingMiddleware()).
        Handler(slog.NewJSONHandler(os.Stdout)),
)
Standalone
import (
	slogmulti "github.com/samber/slog-multi"
	slogsampling "github.com/samber/slog-sampling"
	"golang.org/x/exp/slog"
)

// Will print the first 10 entries having the same level+message, then every 10th messages until next interval.
option := slogsampling.Option{
    Tick:       5 * time.Second,
    First:      10,
    Thereafter: 10,

    OnAccepted: func(context.Context, slog.Record) {
        // ...
    },
    OnDropped: func(context.Context, slog.Record) {
        // ...
    },
}

logger := slog.New(
    option.NewSamplingMiddleware()(
        slog.NewJSONHandler(os.Stdout),
    ),
)

🤝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2023 Samuel Berthe.

This project is MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	// This will log the first `First` log entries with the same level and message
	// in a `Tick` interval as-is. Following that, it will allow through
	// every `Thereafter`th log entry with the same level and message in that interval.
	Tick       time.Duration
	First      uint64
	Thereafter uint64

	// Optional hooks
	OnAccepted func(context.Context, slog.Record)
	OnDropped  func(context.Context, slog.Record)
}

func (Option) NewSamplingMiddleware

func (o Option) NewSamplingMiddleware() slogmulti.Middleware

NewSamplingMiddleware returns a slog-multi middleware.

Jump to

Keyboard shortcuts

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