Skip to content

Commit

Permalink
sampling: require a default policy
Browse files Browse the repository at this point in the history
  • Loading branch information
axw committed Feb 15, 2021
1 parent 0c7bd22 commit e33e96c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions x-pack/apm-server/sampling/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type LocalSamplingConfig struct {
// Policies holds local tail-sampling policies. Policies are matched in the
// order provided. Policies should therefore be ordered from most to least
// specific.
//
// Policies must include at least one policy that matches all traces, to ensure
// that dropping non-matching traces is intentional.
Policies []Policy

// IngestRateDecayFactor holds the ingest rate decay factor, used for calculating
Expand Down Expand Up @@ -173,10 +176,17 @@ func (config LocalSamplingConfig) validate() error {
if len(config.Policies) == 0 {
return errors.New("Policies unspecified")
}
var anyDefaultPolicy bool
for i, policy := range config.Policies {
if err := policy.validate(); err != nil {
return errors.Wrapf(err, "Policy %d invalid", i)
}
if policy.PolicyCriteria == (PolicyCriteria{}) {
anyDefaultPolicy = true
}
}
if !anyDefaultPolicy {
return errors.New("Policies does not contain a default (empty criteria) policy")
}
if config.IngestRateDecayFactor <= 0 || config.IngestRateDecayFactor > 1 {
return errors.New("IngestRateDecayFactor unspecified or out of range (0,1]")
Expand Down
6 changes: 5 additions & 1 deletion x-pack/apm-server/sampling/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func TestNewProcessorConfigInvalid(t *testing.T) {
config.MaxDynamicServices = 1

assertInvalidConfigError("invalid local sampling config: Policies unspecified")
config.Policies = []sampling.Policy{{}}
config.Policies = []sampling.Policy{{
PolicyCriteria: sampling.PolicyCriteria{ServiceName: "foo"},
}}
assertInvalidConfigError("invalid local sampling config: Policies does not contain a default (empty criteria) policy")
config.Policies[0].PolicyCriteria = sampling.PolicyCriteria{}
for _, invalid := range []float64{-1, 1.0, 2.0} {
config.Policies[0].SampleRate = invalid
assertInvalidConfigError("invalid local sampling config: Policy 0 invalid: SampleRate unspecified or out of range [0,1)")
Expand Down
3 changes: 3 additions & 0 deletions x-pack/apm-server/sampling/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ func TestProcessLocalTailSamplingPolicyOrder(t *testing.T) {
}, {
PolicyCriteria: sampling.PolicyCriteria{ServiceName: "service_name"},
SampleRate: 0.1,
}, {
PolicyCriteria: sampling.PolicyCriteria{},
SampleRate: 0,
}}
config.FlushInterval = 10 * time.Millisecond
published := make(chan string)
Expand Down

0 comments on commit e33e96c

Please sign in to comment.