]>
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 | #ifndef JAEGERTRACING_TESTUTILS_SAMPLINGMANAGER_H | |
18 | #define JAEGERTRACING_TESTUTILS_SAMPLINGMANAGER_H | |
19 | ||
20 | #include "jaegertracing/Compilers.h" | |
21 | ||
22 | #include "jaegertracing/thrift-gen/SamplingManager.h" | |
23 | #include "jaegertracing/thrift-gen/sampling_types.h" | |
24 | #include <iterator> | |
25 | #include <mutex> | |
26 | #include <string> | |
27 | #include <unordered_map> | |
28 | #include <utility> | |
29 | ||
30 | namespace jaegertracing { | |
31 | namespace testutils { | |
32 | ||
33 | class SamplingManager : public sampling_manager::thrift::SamplingManagerIf { | |
34 | public: | |
35 | using Response = sampling_manager::thrift::SamplingStrategyResponse; | |
36 | ||
37 | void getSamplingStrategy(Response& response, | |
38 | const std::string& service) override | |
39 | { | |
40 | using ProbabilisticSamplingStrategy = | |
41 | sampling_manager::thrift::ProbabilisticSamplingStrategy; | |
42 | using SamplingStrategyType = | |
43 | sampling_manager::thrift::SamplingStrategyType; | |
44 | ||
45 | std::lock_guard<std::mutex> lock(_mutex); | |
46 | auto responseItr = _sampling.find(service); | |
47 | if (responseItr != std::end(_sampling)) { | |
48 | response = responseItr->second; | |
49 | return; | |
50 | } | |
51 | ||
52 | constexpr auto kSamplingRate = 0.01; | |
53 | ProbabilisticSamplingStrategy probabilisticSampling; | |
54 | probabilisticSampling.__set_samplingRate(kSamplingRate); | |
55 | response.__set_strategyType(SamplingStrategyType::PROBABILISTIC); | |
56 | response.__set_probabilisticSampling(probabilisticSampling); | |
57 | } | |
58 | ||
59 | void addSamplingStrategy(const std::string& serviceName, | |
60 | const Response& response) | |
61 | { | |
62 | std::lock_guard<std::mutex> lock(_mutex); | |
63 | _sampling[serviceName] = response; | |
64 | } | |
65 | ||
66 | private: | |
67 | using StrategyMap = std::unordered_map<std::string, Response>; | |
68 | ||
69 | StrategyMap _sampling; | |
70 | std::mutex _mutex; | |
71 | }; | |
72 | ||
73 | } // namespace testutils | |
74 | } // namespace jaegertracing | |
75 | ||
76 | #endif // JAEGERTRACING_TESTUTILS_SAMPLINGMANAGER_H |