Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sampling strategies overwriting service entry when no sampling ty… #1244

Merged
merged 4 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"operation": "op4",
"type": "ratelimiting",
"param": 100
},
{
"operation": "op5",
"type": "probabilistic",
"param": 0.4
}
]
}
Expand Down
14 changes: 13 additions & 1 deletion plugin/sampling/strategystore/static/strategy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package static

import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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(&copy)
return &copy
}
55 changes: 55 additions & 0 deletions plugin/sampling/strategystore/static/strategy_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}