]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/samplers/RemoteSamplingJSON.h
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / samplers / RemoteSamplingJSON.h
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_SAMPLERS_REMOTESAMPLINGJSON_H
18 #define JAEGERTRACING_SAMPLERS_REMOTESAMPLINGJSON_H
19
20 #include "jaegertracing/Compilers.h"
21
22 #include "jaegertracing/thrift-gen/sampling_types.h"
23 #include <cstdint>
24 #include <iterator>
25 #include <map>
26 #include <nlohmann/json.hpp>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30 #include <vector>
31
32 namespace jaegertracing {
33 namespace sampling_manager {
34 namespace thrift {
35
36 const std::map<int, const char*>& samplingStrategyType_VALUES_TO_NAMES();
37
38 #define JSON_FROM_FIELD(var, field) \
39 { \
40 json[#field] = var.field; \
41 }
42
43 #define FIELD_FROM_JSON(var, field) \
44 { \
45 var.__set_##field(json.at(#field)); \
46 }
47
48 inline void to_json(nlohmann::json& json,
49 const SamplingStrategyType::type& type)
50 {
51 json = samplingStrategyType_VALUES_TO_NAMES().at(static_cast<int>(type));
52 }
53
54 inline void from_json(const nlohmann::json& json,
55 SamplingStrategyType::type& type)
56 {
57 const auto str = json.get<std::string>();
58 if (str == "PROBABILISTIC") {
59 type = SamplingStrategyType::PROBABILISTIC;
60 return;
61 }
62 if (str == "RATE_LIMITING") {
63 type = SamplingStrategyType::RATE_LIMITING;
64 return;
65 }
66 std::ostringstream oss;
67 oss << "Invalid sampling strategy type " << str;
68 throw std::invalid_argument(oss.str());
69 }
70
71 inline void to_json(nlohmann::json& json,
72 const ProbabilisticSamplingStrategy& strategy)
73 {
74 JSON_FROM_FIELD(strategy, samplingRate);
75 }
76
77 inline void from_json(const nlohmann::json& json,
78 ProbabilisticSamplingStrategy& strategy)
79 {
80 FIELD_FROM_JSON(strategy, samplingRate);
81 }
82
83 inline void to_json(nlohmann::json& json,
84 const RateLimitingSamplingStrategy& strategy)
85 {
86 JSON_FROM_FIELD(strategy, maxTracesPerSecond);
87 }
88
89 inline void from_json(const nlohmann::json& json,
90 RateLimitingSamplingStrategy& strategy)
91 {
92 FIELD_FROM_JSON(strategy, maxTracesPerSecond);
93 }
94
95 inline void to_json(nlohmann::json& json,
96 const OperationSamplingStrategy& strategy)
97 {
98 JSON_FROM_FIELD(strategy, operation);
99 JSON_FROM_FIELD(strategy, probabilisticSampling);
100 }
101
102 inline void from_json(const nlohmann::json& json,
103 OperationSamplingStrategy& strategy)
104 {
105 FIELD_FROM_JSON(strategy, operation);
106 FIELD_FROM_JSON(strategy, probabilisticSampling);
107 }
108
109 inline void to_json(nlohmann::json& json,
110 const PerOperationSamplingStrategies& strategies)
111 {
112 JSON_FROM_FIELD(strategies, defaultSamplingProbability);
113 JSON_FROM_FIELD(strategies, defaultLowerBoundTracesPerSecond);
114 JSON_FROM_FIELD(strategies, perOperationStrategies);
115 if (strategies.__isset.defaultUpperBoundTracesPerSecond) {
116 JSON_FROM_FIELD(strategies, defaultUpperBoundTracesPerSecond);
117 }
118 }
119
120 inline void from_json(const nlohmann::json& json,
121 PerOperationSamplingStrategies& strategies)
122 {
123 FIELD_FROM_JSON(strategies, defaultSamplingProbability);
124 FIELD_FROM_JSON(strategies, defaultLowerBoundTracesPerSecond);
125 FIELD_FROM_JSON(strategies, perOperationStrategies);
126 auto itr = json.find("defaultUpperBoundTracesPerSecond");
127 if (itr != std::end(json)) {
128 strategies.__set_defaultUpperBoundTracesPerSecond(itr->get<double>());
129 }
130 }
131
132 inline void to_json(nlohmann::json& json,
133 const SamplingStrategyResponse& response)
134 {
135 JSON_FROM_FIELD(response, strategyType);
136 if (response.__isset.probabilisticSampling) {
137 JSON_FROM_FIELD(response, probabilisticSampling);
138 }
139 if (response.__isset.rateLimitingSampling) {
140 JSON_FROM_FIELD(response, rateLimitingSampling);
141 }
142 if (response.__isset.operationSampling) {
143 JSON_FROM_FIELD(response, operationSampling);
144 }
145 }
146
147 inline void from_json(const nlohmann::json& json,
148 SamplingStrategyResponse& response)
149 {
150 FIELD_FROM_JSON(response, strategyType);
151 auto itr = json.find("probabilisticSampling");
152 if (itr != std::end(json)) {
153 response.__set_probabilisticSampling(
154 itr->get<ProbabilisticSamplingStrategy>());
155 }
156 itr = json.find("rateLimitingSampling");
157 if (itr != std::end(json)) {
158 response.__set_rateLimitingSampling(
159 itr->get<RateLimitingSamplingStrategy>());
160 }
161 itr = json.find("operationSampling");
162 if (itr != std::end(json)) {
163 response.__set_operationSampling(
164 itr->get<PerOperationSamplingStrategies>());
165 }
166 }
167
168 #undef FIELD_FROM_JSON
169 #undef JSON_FROM_FIELD
170
171 } // namespace thrift
172 } // namespace sampling_manager
173 } // namespace jaegertracing
174
175 #endif // JAEGERTRACING_SAMPLERS_REMOTESAMPLINGJSON_H