Skip to content

Commit

Permalink
Set default sampling for service which has no per-operation strategies
Browse files Browse the repository at this point in the history
Signed-off-by: defool <[email protected]>
  • Loading branch information
defool committed May 8, 2020
1 parent 16d7e09 commit e457add
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"service_strategies": [
{
"service": "ServiceA",
"type": "probabilistic",
"param": 1.0
}
],
"default_strategy": {
"type": "probabilistic",
"param": 0.2,
"operation_strategies": [
{
"operation": "/health",
"type": "probabilistic",
"param": 0.0
}
]
}
}
10 changes: 7 additions & 3 deletions plugin/sampling/strategystore/static/strategy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,15 @@ func (h *strategyStore) parseStrategies(strategies *strategies) {
// is not merged with and only used as a fallback).
opS := newStore.serviceStrategies[s.Service].OperationSampling
if opS == nil {
// Service has no per-operation strategies, so just reference the default settings.
newStore.serviceStrategies[s.Service].OperationSampling = newStore.defaultStrategy.OperationSampling
if newStore.defaultStrategy.OperationSampling == nil {
continue
}
// Service has no per-operation strategies, so just reference the default settings and change default samplingRate.
newOpS := *newStore.defaultStrategy.OperationSampling
newOpS.DefaultSamplingProbability = newStore.serviceStrategies[s.Service].ProbabilisticSampling.SamplingRate
newStore.serviceStrategies[s.Service].OperationSampling = &newOpS
continue
}

if merge {
opS.PerOperationStrategies = mergePerOperationSamplingStrategies(
opS.PerOperationStrategies,
Expand Down
13 changes: 13 additions & 0 deletions plugin/sampling/strategystore/static/strategy_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,16 @@ func TestAutoUpdateStrategyErrors(t *testing.T) {
assert.Equal(t, "blah", store.reloadSamplingStrategyFile(tempFile.Name(), "blah"))
assert.Len(t, logs.FilterMessage("failed to update sampling strategies from file").All(), 1)
}

func TestServiceNoPerOperationStrategies(t *testing.T) {
store, err := NewStrategyStore(Options{StrategiesFile: "fixtures/service_no_per_operation.json"}, zap.NewNop())
require.NoError(t, err)

s, err := store.GetSamplingStrategy("ServiceA")
require.NoError(t, err)
assert.Equal(t, s.OperationSampling.DefaultSamplingProbability, 1.0)

s, err = store.GetSamplingStrategy("ServiceB")
require.NoError(t, err)
assert.Equal(t, s.OperationSampling.DefaultSamplingProbability, 0.2)
}

0 comments on commit e457add

Please sign in to comment.