Documentation ¶
Overview ¶
Package sampler contains all the logic of the agent-side trace sampling
Currently implementation is based on the scoring of the "signature" of each trace Based on the score, we get a sample rate to apply to the given trace
Current score implementation is super-simple, it is a counter with polynomial decay per signature. We increment it for each incoming trace then we periodically divide the score by two every X seconds. Right after the division, the score is an approximation of the number of received signatures over X seconds. It is different from the scoring in the Agent.
Since the sampling can happen at different levels (client, agent, server) or depending on different rules, we have to track the sample rate applied at previous steps. This way, sampling twice at 50% can result in an effective 25% sampling. The rate is stored as a metric in the trace root.
Package sampler contains all the logic of the agent-side trace sampling
Index ¶
- Constants
- func GetClientRate(s *pb.Span) float64
- func GetEventExtractionRate(s *pb.Span) float64
- func GetGlobalRate(s *pb.Span) float64
- func GetMaxEPSRate(s *pb.Span) float64
- func GetPreSampleRate(s *pb.Span) float64
- func IsAnalyzedSpan(s *pb.Span) bool
- func SampleByRate(traceID uint64, rate float64) bool
- func SetAnalyzedSpan(s *pb.Span)
- func SetClientRate(s *pb.Span, rate float64)
- func SetEventExtractionRate(s *pb.Span, rate float64)
- func SetMaxEPSRate(s *pb.Span, rate float64)
- func SetPreSampleRate(s *pb.Span, rate float64)
- type DynamicConfig
- type ErrorsSampler
- type NoPrioritySampler
- type PrioritySampler
- type RareSampler
- type RateByService
- type RemoteRates
- type Sampler
- type SamplingPriority
- type ScoreSampler
- type ServiceSignature
- type Signature
- type State
Constants ¶
const ( // KeySamplingRateGlobal is a metric key holding the global sampling rate. KeySamplingRateGlobal = "_sample_rate" // KeySamplingRateClient is a metric key holding the client-set sampling rate for APM events. KeySamplingRateClient = "_dd1.sr.rcusr" // KeySamplingRatePreSampler is a metric key holding the API rate limiter's rate for APM events. KeySamplingRatePreSampler = "_dd1.sr.rapre" // KeySamplingRateEventExtraction is the key of the metric storing the event extraction rate on an APM event. KeySamplingRateEventExtraction = "_dd1.sr.eausr" // KeySamplingRateMaxEPSSampler is the key of the metric storing the max eps sampler rate on an APM event. KeySamplingRateMaxEPSSampler = "_dd1.sr.eamax" // KeyErrorType is the key of the error type in the meta map KeyErrorType = "error.type" // KeyAnalyzedSpans is the metric key which specifies if a span is analyzed. KeyAnalyzedSpans = "_dd.analyzed" // KeyHTTPStatusCode is the key of the http status code in the meta map KeyHTTPStatusCode = "http.status_code" )
Variables ¶
This section is empty.
Functions ¶
func GetClientRate ¶
GetClientRate gets the rate at which the trace this span belongs to was sampled by the tracer. NOTE: This defaults to 1 if no rate is stored.
func GetEventExtractionRate ¶
GetEventExtractionRate gets the rate at which the trace from which we extracted this event was sampled at the tracer. This defaults to 1 if no rate is stored.
func GetGlobalRate ¶
GetGlobalRate gets the cumulative sample rate of the trace to which this span belongs to.
func GetMaxEPSRate ¶
GetMaxEPSRate gets the rate at which this event was sampled by the max eps event sampler.
func GetPreSampleRate ¶
GetPreSampleRate returns the rate at which the trace this span belongs to was sampled by the agent's presampler. NOTE: This defaults to 1 if no rate is stored.
func IsAnalyzedSpan ¶
IsAnalyzedSpan checks if a span is analyzed
func SampleByRate ¶
SampleByRate returns whether to keep a trace, based on its ID and a sampling rate. This assumes that trace IDs are nearly uniformly distributed.
func SetClientRate ¶
SetClientRate sets the rate at which the trace this span belongs to was sampled by the tracer.
func SetEventExtractionRate ¶
SetEventExtractionRate sets the rate at which the trace from which we extracted this event was sampled at the tracer.
func SetMaxEPSRate ¶
SetMaxEPSRate sets the rate at which this event was sampled by the max eps event sampler.
func SetPreSampleRate ¶
SetPreSampleRate sets the rate at which the trace this span belongs to was sampled by the agent's presampler.
Types ¶
type DynamicConfig ¶
type DynamicConfig struct { // RateByService contains the rate for each service/env tuple, // used in priority sampling by client libs. RateByService RateByService }
DynamicConfig contains configuration items which may change dynamically over time.
func NewDynamicConfig ¶
func NewDynamicConfig() *DynamicConfig
NewDynamicConfig creates a new dynamic config object which maps service signatures to their corresponding sampling rates. Each service will have a default assigned matching the service rate of the specified env.
type ErrorsSampler ¶
type ErrorsSampler struct{ ScoreSampler }
ErrorsSampler is dedicated to catching traces containing spans with errors.
func NewErrorsSampler ¶
func NewErrorsSampler(conf *config.AgentConfig) *ErrorsSampler
NewErrorsSampler returns an initialized Sampler dedicate to errors. It behaves just like the the normal ScoreEngine except for its GetType method (useful for reporting).
type NoPrioritySampler ¶
type NoPrioritySampler struct{ ScoreSampler }
NoPrioritySampler is dedicated to catching traces with no priority set.
func NewNoPrioritySampler ¶
func NewNoPrioritySampler(conf *config.AgentConfig) *NoPrioritySampler
NewNoPrioritySampler returns an initialized Sampler dedicated to traces with no priority set.
type PrioritySampler ¶
type PrioritySampler struct {
// contains filtered or unexported fields
}
PrioritySampler computes priority rates per tracerEnv, service to apply in a feedback loop with trace-agent clients. Computed rates are sent in http responses to trace-agent. The rates are continuously adjusted in function of the received traffic to match a targetTPS (target traces per second). In order of priority, the sampler will match a targetTPS set remotely (remoteRates) and then the local targetTPS.
func NewPrioritySampler ¶
func NewPrioritySampler(conf *config.AgentConfig, dynConf *DynamicConfig) *PrioritySampler
NewPrioritySampler returns an initialized Sampler
func (*PrioritySampler) Sample ¶
func (s *PrioritySampler) Sample(now time.Time, trace *pb.TraceChunk, root *pb.Span, tracerEnv string, clientDroppedP0sWeight float64) bool
Sample counts an incoming trace and returns the trace sampling decision and the applied sampling rate
func (*PrioritySampler) Start ¶
func (s *PrioritySampler) Start()
Start runs and block on the Sampler main loop
type RareSampler ¶
type RareSampler struct {
// contains filtered or unexported fields
}
RareSampler samples traces that are not caught by the Priority sampler. It ensures that we sample traces for each combination of (env, service, name, resource, error type, http status) seen on a top level or measured span for which we did not see any span with a priority > 0 (sampled by Priority). The resulting sampled traces will likely be incomplete and will be flagged with a exceptioKey metric set at 1.
func NewRareSampler ¶
func NewRareSampler() *RareSampler
NewRareSampler returns a NewRareSampler that ensures that we sample combinations of env, service, name, resource, http-status, error type for each top level or measured spans
func (*RareSampler) Sample ¶
func (e *RareSampler) Sample(now time.Time, t *pb.TraceChunk, env string) bool
Sample a trace and returns true if trace was sampled (should be kept)
type RateByService ¶
type RateByService struct {
// contains filtered or unexported fields
}
RateByService stores the sampling rate per service. It is thread-safe, so one can read/write on it concurrently, using getters and setters.
func (*RateByService) GetNewState ¶
func (rbs *RateByService) GetNewState(version string) State
GetNewState returns the current state if the given version is different from the local version.
func (*RateByService) SetAll ¶
func (rbs *RateByService) SetAll(rates map[ServiceSignature]rm)
SetAll the sampling rate for all services. If a service/env is not in the map, then the entry is removed.
type RemoteRates ¶
type RemoteRates struct {
// contains filtered or unexported fields
}
RemoteRates computes rates per (env, service) to apply in trace-agent clients. The rates are adjusted to match a targetTPS per (env, service) received from remote configurations. RemoteRates listens for new remote configurations with a grpc subscriber. On reception, new tps targets replace the previous ones.
func (*RemoteRates) Start ¶
func (r *RemoteRates) Start()
Start runs and adjust rates per signature following remote TPS targets
type Sampler ¶
type Sampler struct {
// contains filtered or unexported fields
}
Sampler is the main component of the sampling logic Seen traces are counted per signature in a circular buffer of numBuckets. The sampler distributes uniformly on all signature a targetTPS. The bucket with the maximum counts over the period of the buffer is used to compute the sampling rates.
type SamplingPriority ¶
type SamplingPriority int8
SamplingPriority is the type encoding a priority sampling decision.
const ( // PriorityNone is the value for SamplingPriority when no priority sampling decision could be found. PriorityNone SamplingPriority = math.MinInt8 // PriorityUserDrop is the value set by a user to explicitly drop a trace. PriorityUserDrop SamplingPriority = -1 // PriorityAutoDrop is the value set by a tracer to suggest dropping a trace. PriorityAutoDrop SamplingPriority = 0 // PriorityAutoKeep is the value set by a tracer to suggest keeping a trace. PriorityAutoKeep SamplingPriority = 1 // PriorityUserKeep is the value set by a user to explicitly keep a trace. PriorityUserKeep SamplingPriority = 2 )
func GetSamplingPriority ¶
func GetSamplingPriority(t *pb.TraceChunk) (SamplingPriority, bool)
GetSamplingPriority returns the value of the sampling priority metric set on this span and a boolean indicating if such a metric was actually found or not.
type ScoreSampler ¶
type ScoreSampler struct { *Sampler // contains filtered or unexported fields }
ScoreSampler samples pieces of traces by computing a signature based on spans (service, name, rsc, http.status, error.type) scoring it and applying a rate. The rates are applied on the TraceID to maximize the number of chunks with errors caught for the same traceID. For a set traceID: P(chunk1 kept and chunk2 kept) = min(P(chunk1 kept), P(chunk2 kept))
type ServiceSignature ¶
type ServiceSignature struct{ Name, Env string }
ServiceSignature represents a unique way to identify a service.
func (ServiceSignature) Hash ¶
func (s ServiceSignature) Hash() Signature
Hash generates the signature of a trace with minimal information such as service and env, this is typically used by distributed sampling based on priority, and used as a key to store the desired rate for a given service,env tuple.
func (ServiceSignature) String ¶
func (s ServiceSignature) String() string
type Signature ¶
type Signature uint64
Signature is a hash representation of trace or a service, used to identify simlar signatures.
type State ¶
type State struct { Rates map[string]float64 Mechanisms map[string]apmsampling.SamplingMechanism Version string }
State specifies the current state of DynamicConfig