-
Notifications
You must be signed in to change notification settings - Fork 486
/
strategy_store.go
218 lines (190 loc) · 6.79 KB
/
strategy_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package strategy_store
import (
"context"
"encoding/json"
"fmt"
"go.uber.org/atomic"
"go.uber.org/zap"
ss "github.com/jaegertracing/jaeger/cmd/collector/app/sampling/strategystore"
"github.com/jaegertracing/jaeger/thrift-gen/sampling"
)
type strategyStore struct {
storedStrategies atomic.Value // holds *storedStrategies
logger *zap.Logger
}
type storedStrategies struct {
defaultStrategy *sampling.SamplingStrategyResponse
serviceStrategies map[string]*sampling.SamplingStrategyResponse
}
type strategyLoader func() ([]byte, error)
// NewStrategyStore creates a strategy store that holds static sampling strategies.
func NewStrategyStore(strats string, logger *zap.Logger) (ss.StrategyStore, error) {
h := &strategyStore{
logger: logger,
}
h.storedStrategies.Store(defaultStrategies())
loadFn := func() ([]byte, error) {
return []byte(strats), nil
}
strategies, err := loadStrategies(loadFn)
if err != nil {
return nil, err
}
h.parseStrategies(strategies)
return h, nil
}
// GetSamplingStrategy implements StrategyStore#GetSamplingStrategy.
func (h *strategyStore) GetSamplingStrategy(_ context.Context, serviceName string) (*sampling.SamplingStrategyResponse, error) {
ss := h.storedStrategies.Load().(*storedStrategies)
serviceStrategies := ss.serviceStrategies
if strategy, ok := serviceStrategies[serviceName]; ok {
return strategy, nil
}
return ss.defaultStrategy, nil
}
// Close stops updating the strategies
func (h *strategyStore) Close() {
}
// TODO good candidate for a global util function
func loadStrategies(loadFn strategyLoader) (*strategies, error) {
strategyBytes, err := loadFn()
if err != nil {
return nil, err
}
var strategies *strategies
if err := json.Unmarshal(strategyBytes, &strategies); err != nil {
return nil, fmt.Errorf("failed to unmarshal strategies: %w", err)
}
return strategies, nil
}
func (h *strategyStore) parseStrategies(strategies *strategies) {
if strategies == nil {
h.logger.Info("No sampling strategies provided or URL is unavailable, using defaults")
return
}
newStore := defaultStrategies()
if strategies.DefaultStrategy != nil {
newStore.defaultStrategy = h.parseServiceStrategies(strategies.DefaultStrategy)
}
merge := true
if newStore.defaultStrategy.OperationSampling == nil ||
newStore.defaultStrategy.OperationSampling.PerOperationStrategies == nil {
merge = false
}
for _, s := range strategies.ServiceStrategies {
newStore.serviceStrategies[s.Service] = h.parseServiceStrategies(s)
// Merge with the default operation strategies, because only merging with
// the default strategy has no effect on service strategies (the default strategy
// is not merged with and only used as a fallback).
opS := newStore.serviceStrategies[s.Service].OperationSampling
if opS == nil {
if newStore.defaultStrategy.OperationSampling == nil ||
newStore.serviceStrategies[s.Service].ProbabilisticSampling == 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,
newStore.defaultStrategy.OperationSampling.PerOperationStrategies)
}
}
h.storedStrategies.Store(newStore)
}
// mergePerOperationStrategies merges two operation strategies a and b, where a takes precedence over b.
func mergePerOperationSamplingStrategies(
a, b []*sampling.OperationSamplingStrategy,
) []*sampling.OperationSamplingStrategy {
m := make(map[string]bool)
for _, aOp := range a {
m[aOp.Operation] = true
}
for _, bOp := range b {
if m[bOp.Operation] {
continue
}
a = append(a, bOp)
}
return a
}
func (h *strategyStore) parseServiceStrategies(strategy *serviceStrategy) *sampling.SamplingStrategyResponse {
resp := h.parseStrategy(&strategy.strategy)
if len(strategy.OperationStrategies) == 0 {
return resp
}
opS := &sampling.PerOperationSamplingStrategies{
DefaultSamplingProbability: defaultSamplingProbability,
}
if resp.StrategyType == sampling.SamplingStrategyType_PROBABILISTIC {
opS.DefaultSamplingProbability = resp.ProbabilisticSampling.SamplingRate
}
for _, operationStrategy := range strategy.OperationStrategies {
s, ok := h.parseOperationStrategy(operationStrategy, opS)
if !ok {
continue
}
opS.PerOperationStrategies = append(opS.PerOperationStrategies,
&sampling.OperationSamplingStrategy{
Operation: operationStrategy.Operation,
ProbabilisticSampling: s.ProbabilisticSampling,
})
}
resp.OperationSampling = opS
return resp
}
func (h *strategyStore) parseOperationStrategy(
strategy *operationStrategy,
parent *sampling.PerOperationSamplingStrategies,
) (s *sampling.SamplingStrategyResponse, ok bool) {
s = h.parseStrategy(&strategy.strategy)
if s.StrategyType == sampling.SamplingStrategyType_RATE_LIMITING {
// TODO OperationSamplingStrategy only supports probabilistic sampling
h.logger.Warn(
fmt.Sprintf(
"Operation strategies only supports probabilistic sampling at the moment,"+
"'%s' defaulting to probabilistic sampling with probability %f",
strategy.Operation, parent.DefaultSamplingProbability),
zap.Any("strategy", strategy))
return nil, false
}
return s, true
}
func (h *strategyStore) parseStrategy(strategy *strategy) *sampling.SamplingStrategyResponse {
switch strategy.Type {
case samplerTypeProbabilistic:
return &sampling.SamplingStrategyResponse{
StrategyType: sampling.SamplingStrategyType_PROBABILISTIC,
ProbabilisticSampling: &sampling.ProbabilisticSamplingStrategy{
SamplingRate: strategy.Param,
},
}
case samplerTypeRateLimiting:
return &sampling.SamplingStrategyResponse{
StrategyType: sampling.SamplingStrategyType_RATE_LIMITING,
RateLimitingSampling: &sampling.RateLimitingSamplingStrategy{
MaxTracesPerSecond: int16(strategy.Param),
},
}
default:
h.logger.Warn("Failed to parse sampling strategy", zap.Any("strategy", strategy))
return defaultStrategyResponse()
}
}