sampler

package
v0.0.0-...-3257545 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

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.

Index

Constants

View Source
const (

	// DefaultDecayPeriod defines the default decayPeriod
	DefaultDecayPeriod time.Duration = 5 * time.Second
	// DefaultDecayFactor with this factor, any past trace counts for less than 50% after 6*decayPeriod and >1% after 39*decayPeriod
	// We can keep it hardcoded, but having `decayPeriod` configurable should be enough?
	DefaultDecayFactor float64 = 1.125 // 9/8

)
View Source
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"

	// KeySamplingPriority is the key of the sampling priority value in the metrics map of the root span
	KeySamplingPriority = "_sampling_priority_v1"

	// KeyErrorType is the key of the error type in the meta map
	KeyErrorType = "error.type"

	// 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

func GetClientRate(s *pb.Span) float64

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

func GetEventExtractionRate(s *pb.Span) float64

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

func GetGlobalRate(s *pb.Span) float64

GetGlobalRate gets the cumulative sample rate of the trace to which this span belongs to.

func GetMaxEPSRate

func GetMaxEPSRate(s *pb.Span) float64

GetMaxEPSRate gets the rate at which this event was sampled by the max eps event sampler.

func GetMetric

func GetMetric(s *pb.Span, k string) (float64, bool)

GetMetric gets a value in the span Metrics map

func GetPreSampleRate

func GetPreSampleRate(s *pb.Span) float64

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 SampleByRate

func SampleByRate(traceID uint64, rate float64) bool

SampleByRate tells if a trace (from its ID) with a given rate should be sampled Use Knuth multiplicative hashing to leverage imbalanced traceID generators

func SetClientRate

func SetClientRate(s *pb.Span, rate float64)

SetClientRate sets the rate at which the trace this span belongs to was sampled by the tracer.

func SetEventExtractionRate

func SetEventExtractionRate(s *pb.Span, rate float64)

SetEventExtractionRate sets the rate at which the trace from which we extracted this event was sampled at the tracer.

func SetMaxEPSRate

func SetMaxEPSRate(s *pb.Span, rate float64)

SetMaxEPSRate sets the rate at which this event was sampled by the max eps event sampler.

func SetMetric

func SetMetric(s *pb.Span, key string, val float64)

SetMetric sets a value in the span Metrics map.

func SetPreSampleRate

func SetPreSampleRate(s *pb.Span, rate float64)

SetPreSampleRate sets the rate at which the trace this span belongs to was sampled by the agent's presampler.

func SetSamplingPriority

func SetSamplingPriority(s *pb.Span, priority SamplingPriority)

SetSamplingPriority sets the sampling priority value on this span, overwriting any previously set value.

Types

type EngineType

type EngineType int

EngineType represents the type of a sampler engine.

const (
	// NormalScoreEngineType is the type of the ScoreEngine sampling non-error traces.
	NormalScoreEngineType EngineType = iota
	// ErrorsScoreEngineType is the type of the ScoreEngine sampling error traces.
	ErrorsScoreEngineType
	// PriorityEngineType is type of the priority sampler engine type.
	PriorityEngineType
)

type MemoryBackend

type MemoryBackend struct {

	// DecayPeriod is the time period between each score decay.
	// A lower value is more reactive, but forgets quicker.
	DecayPeriod time.Duration
	// contains filtered or unexported fields
}

MemoryBackend storing any state required to run the sampling algorithms.

Current implementation is only based on counters with polynomial decay. Its bias with steady counts is 1 * decayFactor. The stored scores represent approximation of the real count values (with a countScaleFactor factor).

func NewMemoryBackend

func NewMemoryBackend(decayPeriod time.Duration, decayFactor float64) *MemoryBackend

NewMemoryBackend returns an initialized Backend.

func (*MemoryBackend) AddTotalScore

func (b *MemoryBackend) AddTotalScore(n float64)

AddTotalScore adds to the total score.

func (*MemoryBackend) CountSample

func (b *MemoryBackend) CountSample()

CountSample counts a trace sampled by the sampler.

func (*MemoryBackend) CountSignature

func (b *MemoryBackend) CountSignature(signature Signature)

CountSignature counts an incoming signature.

func (*MemoryBackend) CountWeightedSig

func (b *MemoryBackend) CountWeightedSig(signature Signature, n float64)

CountWeightedSig counts a trace sampled by the sampler.

func (*MemoryBackend) DecayScore

func (b *MemoryBackend) DecayScore()

DecayScore applies the decay to the rolling counters.

func (*MemoryBackend) GetCardinality

func (b *MemoryBackend) GetCardinality() int64

GetCardinality returns the number of different signatures seen recently.

func (*MemoryBackend) GetSampledScore

func (b *MemoryBackend) GetSampledScore() float64

GetSampledScore returns the global score of all sampled traces.

func (*MemoryBackend) GetSignatureScore

func (b *MemoryBackend) GetSignatureScore(signature Signature) float64

GetSignatureScore returns the score of a signature. It is normalized to represent a number of signatures per second.

func (*MemoryBackend) GetSignatureScores

func (b *MemoryBackend) GetSignatureScores() map[Signature]float64

GetSignatureScores returns the scores for all signatures. It is normalized to represent a number of signatures per second.

func (*MemoryBackend) GetTotalScore

func (b *MemoryBackend) GetTotalScore() float64

GetTotalScore returns the global score of all sampled traces.

func (*MemoryBackend) GetUpperSampledScore

func (b *MemoryBackend) GetUpperSampledScore() float64

GetUpperSampledScore returns a certain upper bound of the global count of all sampled traces.

type Sampler

type Sampler struct {
	// Storage of the state of the sampler
	Backend *MemoryBackend

	// Extra sampling rate to combine to the existing sampling
	ExtraRate float64
	// Maximum limit to the total number of traces per second to sample
	TargetTPS float64
	// RateThresholdTo1 is the value above which all computed sampling rates will be set to 1
	RateThresholdTo1 float64

	// Sample any signature with a score lower than scoreSamplingOffset
	// It is basically the number of similar traces per second after which we start sampling
	SignatureScoreOffset *atomic.Float64
	// Logarithm slope for the scoring function
	SignatureScoreSlope *atomic.Float64
	// SignatureScoreFactor = math.Pow(SignatureScoreSlope, math.Log10(scoreSamplingOffset))
	SignatureScoreFactor *atomic.Float64
	// contains filtered or unexported fields
}

Sampler is the main component of the sampling logic

func NewSampler

func NewSampler(extraRate float64, targetTPS float64, tags []string) *Sampler

NewSampler returns an initialized Sampler

func (*Sampler) AdjustScoring

func (s *Sampler) AdjustScoring()

AdjustScoring modifies sampler coefficients to fit better the `targetTPS` condition

func (*Sampler) GetAllCountScores

func (s *Sampler) GetAllCountScores() map[Signature]float64

GetAllCountScores scores all signatures based on their recent throughput The score value can be seeing as the sample rate if the count were the only factor Since other factors can intervene (such as extra global sampling), its value can be larger than 1

func (*Sampler) GetAllSignatureSampleRates

func (s *Sampler) GetAllSignatureSampleRates() map[Signature]float64

GetAllSignatureSampleRates gives the sample rate to apply to all signatures. For now, only based on count score.

func (*Sampler) GetCountScore

func (s *Sampler) GetCountScore(signature Signature) float64

GetCountScore scores any signature based on its recent throughput The score value can be seeing as the sample rate if the count were the only factor Since other factors can intervene (such as extra global sampling), its value can be larger than 1

func (*Sampler) GetDefaultCountScore

func (s *Sampler) GetDefaultCountScore() float64

GetDefaultCountScore returns a default score when not knowing the signature for real. Since other factors can intervene (such as extra global sampling), its value can be larger than 1

func (*Sampler) GetDefaultSampleRate

func (s *Sampler) GetDefaultSampleRate() float64

GetDefaultSampleRate gives the sample rate to apply to an unknown signature. For now, only based on count score.

func (*Sampler) GetSampleRate

func (s *Sampler) GetSampleRate(trace pb.Trace, root *pb.Span, signature Signature) float64

GetSampleRate returns the sample rate to apply to a trace.

func (*Sampler) GetSignatureSampleRate

func (s *Sampler) GetSignatureSampleRate(signature Signature) float64

GetSignatureSampleRate gives the sample rate to apply to any signature. For now, only based on count score.

func (*Sampler) GetTargetTPSSampleRate

func (s *Sampler) GetTargetTPSSampleRate() float64

GetTargetTPSSampleRate returns an extra sample rate to apply if we are above targetTPS.

func (*Sampler) SetRateThresholdTo1

func (s *Sampler) SetRateThresholdTo1(r float64)

SetRateThresholdTo1 sets the RateThresholdTo1

func (*Sampler) SetSignatureCoefficients

func (s *Sampler) SetSignatureCoefficients(offset float64, slope float64)

SetSignatureCoefficients updates the internal scoring coefficients used by the signature scoring

func (*Sampler) Start

func (s *Sampler) Start()

Start runs and the Sampler main loop

func (*Sampler) Stop

func (s *Sampler) Stop()

Stop stops the main Run loop

func (*Sampler) UpdateExtraRate

func (s *Sampler) UpdateExtraRate(extraRate float64)

UpdateExtraRate updates the extra sample rate

func (*Sampler) UpdateTargetTPS

func (s *Sampler) UpdateTargetTPS(targetTPS float64)

UpdateTargetTPS updates the max TPS limit

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(s *pb.Span) (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 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.

func ComputeSignatureWithRootAndEnv

func ComputeSignatureWithRootAndEnv(trace pb.Trace, root *pb.Span, env string) Signature

ComputeSignatureWithRootAndEnv generates the signature of a trace knowing its root Signature based on the hash of (env, service, name, resource, is_error) for the root, plus the set of (env, service, name, is_error) of each span.

type SpanHash

type SpanHash uint32

SpanHash is the type of the hashes used during the computation of a signature Use FNV for hashing since it is super-cheap and we have no cryptographic needs

func ComputeSpanHash

func ComputeSpanHash(span *pb.Span, env string, withResource bool) SpanHash

ComputeSpanHash computes a SpanHash for the given Span

Jump to

Keyboard shortcuts

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