diff --git a/plugin/sampling/strategystore/static/fixtures/missing-service-types.json b/plugin/sampling/strategystore/static/fixtures/missing-service-types.json new file mode 100644 index 00000000000..0d3d5f2a3c0 --- /dev/null +++ b/plugin/sampling/strategystore/static/fixtures/missing-service-types.json @@ -0,0 +1,33 @@ +{ + "default_strategy": { + "type": "probabilistic", + "param": 0.5 + }, + "service_strategies": [ + { + "service": "foo", + "operation_strategies": [ + { + "operation": "op1", + "type": "probabilistic", + "param": 0.2 + } + ] + }, + { + "service": "bar", + "operation_strategies": [ + { + "operation": "op3", + "type": "probabilistic", + "param": 0.3 + }, + { + "operation": "op5", + "type": "probabilistic", + "param": 0.4 + } + ] + } + ] +} diff --git a/plugin/sampling/strategystore/static/fixtures/operation_strategies.json b/plugin/sampling/strategystore/static/fixtures/operation_strategies.json index f0c7525f357..d0c00fb3247 100644 --- a/plugin/sampling/strategystore/static/fixtures/operation_strategies.json +++ b/plugin/sampling/strategystore/static/fixtures/operation_strategies.json @@ -35,6 +35,11 @@ "operation": "op4", "type": "ratelimiting", "param": 100 + }, + { + "operation": "op5", + "type": "probabilistic", + "param": 0.4 } ] } diff --git a/plugin/sampling/strategystore/static/strategy_store.go b/plugin/sampling/strategystore/static/strategy_store.go index cede2d1d85c..fce2c080d10 100644 --- a/plugin/sampling/strategystore/static/strategy_store.go +++ b/plugin/sampling/strategystore/static/strategy_store.go @@ -15,6 +15,8 @@ package static import ( + "bytes" + "encoding/gob" "encoding/json" "fmt" "io/ioutil" @@ -136,6 +138,16 @@ func (h *strategyStore) parseStrategy(strategy *strategy) *sampling.SamplingStra } default: h.logger.Warn("Failed to parse sampling strategy", zap.Any("strategy", strategy)) - return &defaultStrategy + return deepCopy(&defaultStrategy) } } + +func deepCopy(s *sampling.SamplingStrategyResponse) *sampling.SamplingStrategyResponse { + var buf bytes.Buffer + enc := gob.NewEncoder(&buf) + dec := gob.NewDecoder(&buf) + enc.Encode(*s) + var copy sampling.SamplingStrategyResponse + dec.Decode(©) + return © +} diff --git a/plugin/sampling/strategystore/static/strategy_store_test.go b/plugin/sampling/strategystore/static/strategy_store_test.go index 16e05575b44..fd587e1aba5 100644 --- a/plugin/sampling/strategystore/static/strategy_store_test.go +++ b/plugin/sampling/strategystore/static/strategy_store_test.go @@ -91,9 +91,52 @@ func TestPerOperationSamplingStrategies(t *testing.T) { require.NotNil(t, s.OperationSampling) os = s.OperationSampling assert.EqualValues(t, os.DefaultSamplingProbability, 0.001) + require.Len(t, os.PerOperationStrategies, 2) + assert.Equal(t, "op3", os.PerOperationStrategies[0].Operation) + assert.EqualValues(t, 0.3, os.PerOperationStrategies[0].ProbabilisticSampling.SamplingRate) + assert.Equal(t, "op5", os.PerOperationStrategies[1].Operation) + assert.EqualValues(t, 0.4, os.PerOperationStrategies[1].ProbabilisticSampling.SamplingRate) + + s, err = store.GetSamplingStrategy("default") + require.NoError(t, err) + assert.EqualValues(t, makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, 0.5), *s) +} + +func TestMissingServiceSamplingStrategyTypes(t *testing.T) { + logger, buf := testutils.NewLogger() + store, err := NewStrategyStore(Options{StrategiesFile: "fixtures/missing-service-types.json"}, logger) + assert.Contains(t, buf.String(), "Failed to parse sampling strategy") + require.NoError(t, err) + + expected := makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, defaultSamplingProbability) + + s, err := store.GetSamplingStrategy("foo") + require.NoError(t, err) + assert.Equal(t, sampling.SamplingStrategyType_PROBABILISTIC, s.StrategyType) + assert.Equal(t, *expected.ProbabilisticSampling, *s.ProbabilisticSampling) + + require.NotNil(t, s.OperationSampling) + os := s.OperationSampling + assert.EqualValues(t, os.DefaultSamplingProbability, defaultSamplingProbability) require.Len(t, os.PerOperationStrategies, 1) + assert.Equal(t, "op1", os.PerOperationStrategies[0].Operation) + assert.EqualValues(t, 0.2, os.PerOperationStrategies[0].ProbabilisticSampling.SamplingRate) + + expected = makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, defaultSamplingProbability) + + s, err = store.GetSamplingStrategy("bar") + require.NoError(t, err) + assert.Equal(t, sampling.SamplingStrategyType_PROBABILISTIC, s.StrategyType) + assert.Equal(t, *expected.ProbabilisticSampling, *s.ProbabilisticSampling) + + require.NotNil(t, s.OperationSampling) + os = s.OperationSampling + assert.EqualValues(t, os.DefaultSamplingProbability, 0.001) + require.Len(t, os.PerOperationStrategies, 2) assert.Equal(t, "op3", os.PerOperationStrategies[0].Operation) assert.EqualValues(t, 0.3, os.PerOperationStrategies[0].ProbabilisticSampling.SamplingRate) + assert.Equal(t, "op5", os.PerOperationStrategies[1].Operation) + assert.EqualValues(t, 0.4, os.PerOperationStrategies[1].ProbabilisticSampling.SamplingRate) s, err = store.GetSamplingStrategy("default") require.NoError(t, err) @@ -150,3 +193,15 @@ func makeResponse(samplerType sampling.SamplingStrategyType, param float64) (res } return resp } + +func TestDeepCopy(t *testing.T) { + s := &sampling.SamplingStrategyResponse{ + StrategyType: sampling.SamplingStrategyType_PROBABILISTIC, + ProbabilisticSampling: &sampling.ProbabilisticSamplingStrategy{ + SamplingRate: 0.5, + }, + } + copy := deepCopy(s) + assert.False(t, copy == s) + assert.EqualValues(t, copy, s) +}