forked from jaegertracing/jaeger
/
strategy_store.go
153 lines (140 loc) · 4.98 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
// 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 static
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"go.uber.org/zap"
ss "github.com/jaegertracing/jaeger/cmd/collector/app/sampling/strategystore"
"github.com/jaegertracing/jaeger/thrift-gen/sampling"
)
type strategyStore struct {
logger *zap.Logger
defaultStrategy *sampling.SamplingStrategyResponse
serviceStrategies map[string]*sampling.SamplingStrategyResponse
}
// NewStrategyStore creates a strategy store that holds static sampling strategies.
func NewStrategyStore(options Options, logger *zap.Logger) (ss.StrategyStore, error) {
h := &strategyStore{
logger: logger,
serviceStrategies: make(map[string]*sampling.SamplingStrategyResponse),
}
strategies, err := loadStrategies(options.StrategiesFile)
if err != nil {
return nil, err
}
h.parseStrategies(strategies)
return h, nil
}
// GetSamplingStrategy implements StrategyStore#GetSamplingStrategy.
func (h *strategyStore) GetSamplingStrategy(serviceName string) (*sampling.SamplingStrategyResponse, error) {
if strategy, ok := h.serviceStrategies[serviceName]; ok {
return strategy, nil
}
return h.defaultStrategy, nil
}
// TODO good candidate for a global util function
func loadStrategies(strategiesFile string) (*strategies, error) {
if strategiesFile == "" {
return nil, nil
}
bytes, err := ioutil.ReadFile(strategiesFile) /* nolint #nosec , this comes from an admin, not user */
if err != nil {
return nil, errors.Wrap(err, "Failed to open strategies file")
}
var strategies strategies
if err := json.Unmarshal(bytes, &strategies); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal strategies")
}
return &strategies, nil
}
func (h *strategyStore) parseStrategies(strategies *strategies) {
h.defaultStrategy = &defaultStrategy
if strategies == nil {
h.logger.Info("No sampling strategies provided, using defaults")
return
}
if strategies.DefaultStrategy != nil {
h.defaultStrategy = h.parseStrategy(strategies.DefaultStrategy)
}
for _, s := range strategies.ServiceStrategies {
h.serviceStrategies[s.Service] = h.parseServiceStrategies(s)
}
}
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 := h.parseStrategy(&operationStrategy.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",
operationStrategy.Operation, opS.DefaultSamplingProbability),
zap.Any("strategy", operationStrategy))
continue
}
opS.PerOperationStrategies = append(opS.PerOperationStrategies,
&sampling.OperationSamplingStrategy{
Operation: operationStrategy.Operation,
ProbabilisticSampling: s.ProbabilisticSampling,
})
}
resp.OperationSampling = opS
return resp
}
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 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 ©
}