Documentation
¶
Index ¶
- Constants
- func AddStrategy(name string, strategy StrategyFactory)
- func IsEnabled(config cfg.Config) (bool, error)
- type Decider
- type MessageWithSamplingEncoder
- type ProbabilisticSettings
- type Settings
- type Strategy
- func DecideByAlways(ctx context.Context, config cfg.Config) (Strategy, error)
- func DecideByHttpHeader(req *http.Request) Strategy
- func DecideByNever(ctx context.Context, config cfg.Config) (Strategy, error)
- func DecideByProbabilistic(ctx context.Context, config cfg.Config) (Strategy, error)
- func DecideByTracing(ctx context.Context, config cfg.Config) (Strategy, error)
- func NewProbabilisticStrategyWithInterfaces(clk clock.Clock, settings *ProbabilisticSettings, randFloat64 func() float64) Strategy
- type StrategyFactory
Constants ¶
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.
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 ¶
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.
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.
type ProbabilisticSettings ¶
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.
type Strategy ¶
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 ¶
DecideByAlways creates a strategy that always samples.
func DecideByHttpHeader ¶
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 ¶
DecideByNever creates a strategy that never samples.
func DecideByProbabilistic ¶
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 ¶
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.