]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/samplers/AdaptiveSampler.h" | |
18 | #include "jaegertracing/samplers/GuaranteedThroughputProbabilisticSampler.h" | |
19 | #include "jaegertracing/thrift-gen/sampling_types.h" | |
20 | #include <cassert> | |
21 | #include <iterator> | |
22 | #include <memory> | |
23 | #include <utility> | |
24 | ||
25 | namespace jaegertracing { | |
26 | class TraceID; | |
27 | } // namespace jaegertracing | |
28 | ||
29 | namespace jaegertracing { | |
30 | namespace samplers { | |
31 | namespace { | |
32 | ||
33 | AdaptiveSampler::SamplerMap samplersFromStrategies( | |
34 | const sampling_manager::thrift::PerOperationSamplingStrategies& strategies) | |
35 | { | |
36 | AdaptiveSampler::SamplerMap samplers; | |
37 | for (auto&& strategy : strategies.perOperationStrategies) { | |
38 | samplers[strategy.operation] = | |
39 | std::make_shared<GuaranteedThroughputProbabilisticSampler>( | |
40 | strategies.defaultLowerBoundTracesPerSecond, | |
41 | strategy.probabilisticSampling.samplingRate); | |
42 | } | |
43 | return samplers; | |
44 | } | |
45 | ||
46 | } // anonymous namespace | |
47 | ||
48 | AdaptiveSampler::AdaptiveSampler( | |
49 | const sampling_manager::thrift::PerOperationSamplingStrategies& strategies, | |
50 | size_t maxOperations) | |
51 | : _samplers(samplersFromStrategies(strategies)) | |
52 | , _defaultSampler(strategies.defaultSamplingProbability) | |
53 | , _lowerBound(strategies.defaultLowerBoundTracesPerSecond) | |
54 | , _maxOperations(maxOperations) | |
55 | , _mutex() | |
56 | { | |
57 | } | |
58 | ||
59 | SamplingStatus AdaptiveSampler::isSampled(const TraceID& id, | |
60 | const std::string& operation) | |
61 | { | |
62 | std::lock_guard<std::mutex> lock(_mutex); | |
63 | auto samplerItr = _samplers.find(operation); | |
64 | if (samplerItr != std::end(_samplers)) { | |
65 | return samplerItr->second->isSampled(id, operation); | |
66 | } | |
67 | if (_samplers.size() >= _maxOperations) { | |
68 | return _defaultSampler.isSampled(id, operation); | |
69 | } | |
70 | ||
71 | auto newSampler = | |
72 | std::make_shared<GuaranteedThroughputProbabilisticSampler>( | |
73 | _lowerBound, _defaultSampler.samplingRate()); | |
74 | _samplers[operation] = newSampler; | |
75 | return newSampler->isSampled(id, operation); | |
76 | } | |
77 | ||
78 | void AdaptiveSampler::close() | |
79 | { | |
80 | std::lock_guard<std::mutex> lock(_mutex); | |
81 | for (auto&& pair : _samplers) { | |
82 | pair.second->close(); | |
83 | } | |
84 | } | |
85 | ||
86 | void AdaptiveSampler::update(const PerOperationSamplingStrategies& strategies) | |
87 | { | |
88 | const auto lowerBound = strategies.defaultLowerBoundTracesPerSecond; | |
89 | std::lock_guard<std::mutex> lock(_mutex); | |
90 | for (auto&& strategy : strategies.perOperationStrategies) { | |
91 | auto& sampler = _samplers[strategy.operation]; | |
92 | const auto samplingRate = strategy.probabilisticSampling.samplingRate; | |
93 | if (sampler) { | |
94 | sampler->update(lowerBound, samplingRate); | |
95 | } | |
96 | else { | |
97 | sampler = | |
98 | std::make_shared<GuaranteedThroughputProbabilisticSampler>( | |
99 | lowerBound, samplingRate); | |
100 | } | |
101 | assert(sampler); | |
102 | } | |
103 | } | |
104 | ||
105 | } // namespace samplers | |
106 | } // namespace jaegertracing |