smpl

package
v0.58.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const HeaderSamplingKey = "X-Goso-Sampled"

Variables

This section is empty.

Functions

func AddStrategy

func AddStrategy(name string, strategy StrategyFactory)

AddStrategy adds a new named strategy factory to the available strategies.

func IsEnabled

func IsEnabled(config cfg.Config) (bool, error)

Types

type Decider

type Decider interface {
	// Decide evaluates the configured strategies to determine if the current context should be sampled.
	// It accepts additional strategies that take precedence over the configured ones.
	// It returns the context (potentially enriched with the sampling decision), the decision itself (true if sampled), and any error occurred.
	Decide(ctx context.Context, additionalStrategies ...Strategy) (context.Context, bool, error)
}

Decider determines if a request should be sampled.

func NewDecider

func NewDecider(ctx context.Context, config cfg.Config) (Decider, error)

NewDecider creates a new Decider based on the provided configuration. It parses the settings from the "sampling" configuration key.

func NewDeciderWithInterfaces

func NewDeciderWithInterfaces(strategies []Strategy, settings *Settings, writer metric.Writer) Decider

NewDeciderWithInterfaces creates a new Decider with the given strategies, settings and metric writer. This is useful for testing or when manual construction is required.

func ProvideDecider

func ProvideDecider(ctx context.Context, config cfg.Config) (Decider, error)

ProvideDecider provides a singleton Decider instance from the application context.

type MessageWithSamplingEncoder

type MessageWithSamplingEncoder struct{}

MessageWithSamplingEncoder encodes/decodes the sampling decision into message attributes. It uses the attribute "sampled" to propagate the decision across async boundaries.

func NewMessageWithSamplingEncoder

func NewMessageWithSamplingEncoder() *MessageWithSamplingEncoder

NewMessageWithSamplingEncoder creates a new MessageWithSamplingEncoder.

func (MessageWithSamplingEncoder) Decode

func (m MessageWithSamplingEncoder) Decode(ctx context.Context, _ any, attributes map[string]string) (context.Context, map[string]string, error)

Decode reads the "sampled" attribute and updates the context with the propagated decision.

func (MessageWithSamplingEncoder) Encode

func (m MessageWithSamplingEncoder) Encode(ctx context.Context, _ any, attributes map[string]string) (context.Context, map[string]string, error)

Encode writes the current sampling decision from the context into the "sampled" attribute.

type ProbabilisticSettings

type ProbabilisticSettings struct {
	Interval            time.Duration `cfg:"interval" default:"1s"`
	FixedSampleCount    int           `cfg:"fixed_sample_count" default:"1"`
	ExtraRatePercentage int           `cfg:"extra_rate_percentage" default:"5"`
}

type Settings

type Settings struct {
	// Enabled toggles the sampling logic. If false, sampling is assumed to be true but not stored in the context.
	Enabled bool `cfg:"enabled" default:"false"`
	// Strategies is a list of strategy names to apply in order.
	Strategies []string `cfg:"strategies"`
}

Settings configures the sampling behavior.

func UnmarshalSettings

func UnmarshalSettings(config cfg.Config) (*Settings, error)

type Strategy

type Strategy func(ctx context.Context) (isApplied bool, isSampled bool, err error)

Strategy is a function that determines if sampling should occur based on the context. It returns isApplied (true if the strategy made a decision), isSampled (the decision), and an error.

func DecideByAlways

func DecideByAlways(ctx context.Context, config cfg.Config) (Strategy, error)

DecideByAlways creates a strategy that always samples.

func DecideByHttpHeader

func DecideByHttpHeader(req *http.Request) Strategy

DecideByHttpHeader creates a strategy based on the 'X-Goso-Sampled' HTTP header. It returns applied=true if the header is present, and parses the value as a boolean.

func DecideByNever

func DecideByNever(ctx context.Context, config cfg.Config) (Strategy, error)

DecideByNever creates a strategy that never samples.

func DecideByProbabilistic

func DecideByProbabilistic(ctx context.Context, config cfg.Config) (Strategy, error)

DecideByProbabilistic creates a new probabilistic sampling strategy. It guarantees a configurable number of sampled=true decisions per interval (when invoked), plus an additional configurable percentage probability for subsequent calls in that interval.

func DecideByTracing

func DecideByTracing(ctx context.Context, config cfg.Config) (Strategy, error)

DecideByTracing creates a strategy that delegates to the tracing context. If a trace exists in the context, its sampling decision is used.

func NewProbabilisticStrategyWithInterfaces

func NewProbabilisticStrategyWithInterfaces(clk clock.Clock, settings *ProbabilisticSettings, randFloat64 func() float64) Strategy

NewProbabilisticStrategyWithInterfaces creates a probabilistic strategy with injected clock, RNG, and settings for testing.

type StrategyFactory

type StrategyFactory func(ctx context.Context, config cfg.Config) (Strategy, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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