2 * Copyright (c) 2017 Uber Technologies, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef JAEGERTRACING_SAMPLERS_REMOTESAMPLINGJSON_H
18 #define JAEGERTRACING_SAMPLERS_REMOTESAMPLINGJSON_H
20 #include "jaegertracing/Compilers.h"
22 #include "jaegertracing/thrift-gen/sampling_types.h"
26 #include <nlohmann/json.hpp>
32 namespace jaegertracing
{
33 namespace sampling_manager
{
36 const std::map
<int, const char*>& samplingStrategyType_VALUES_TO_NAMES();
38 #define JSON_FROM_FIELD(var, field) \
40 json[#field] = var.field; \
43 #define FIELD_FROM_JSON(var, field) \
45 var.__set_##field(json.at(#field)); \
48 inline void to_json(nlohmann::json
& json
,
49 const SamplingStrategyType::type
& type
)
51 json
= samplingStrategyType_VALUES_TO_NAMES().at(static_cast<int>(type
));
54 inline void from_json(const nlohmann::json
& json
,
55 SamplingStrategyType::type
& type
)
57 const auto str
= json
.get
<std::string
>();
58 if (str
== "PROBABILISTIC") {
59 type
= SamplingStrategyType::PROBABILISTIC
;
62 if (str
== "RATE_LIMITING") {
63 type
= SamplingStrategyType::RATE_LIMITING
;
66 std::ostringstream oss
;
67 oss
<< "Invalid sampling strategy type " << str
;
68 throw std::invalid_argument(oss
.str());
71 inline void to_json(nlohmann::json
& json
,
72 const ProbabilisticSamplingStrategy
& strategy
)
74 JSON_FROM_FIELD(strategy
, samplingRate
);
77 inline void from_json(const nlohmann::json
& json
,
78 ProbabilisticSamplingStrategy
& strategy
)
80 FIELD_FROM_JSON(strategy
, samplingRate
);
83 inline void to_json(nlohmann::json
& json
,
84 const RateLimitingSamplingStrategy
& strategy
)
86 JSON_FROM_FIELD(strategy
, maxTracesPerSecond
);
89 inline void from_json(const nlohmann::json
& json
,
90 RateLimitingSamplingStrategy
& strategy
)
92 FIELD_FROM_JSON(strategy
, maxTracesPerSecond
);
95 inline void to_json(nlohmann::json
& json
,
96 const OperationSamplingStrategy
& strategy
)
98 JSON_FROM_FIELD(strategy
, operation
);
99 JSON_FROM_FIELD(strategy
, probabilisticSampling
);
102 inline void from_json(const nlohmann::json
& json
,
103 OperationSamplingStrategy
& strategy
)
105 FIELD_FROM_JSON(strategy
, operation
);
106 FIELD_FROM_JSON(strategy
, probabilisticSampling
);
109 inline void to_json(nlohmann::json
& json
,
110 const PerOperationSamplingStrategies
& strategies
)
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
);
120 inline void from_json(const nlohmann::json
& json
,
121 PerOperationSamplingStrategies
& strategies
)
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>());
132 inline void to_json(nlohmann::json
& json
,
133 const SamplingStrategyResponse
& response
)
135 JSON_FROM_FIELD(response
, strategyType
);
136 if (response
.__isset
.probabilisticSampling
) {
137 JSON_FROM_FIELD(response
, probabilisticSampling
);
139 if (response
.__isset
.rateLimitingSampling
) {
140 JSON_FROM_FIELD(response
, rateLimitingSampling
);
142 if (response
.__isset
.operationSampling
) {
143 JSON_FROM_FIELD(response
, operationSampling
);
147 inline void from_json(const nlohmann::json
& json
,
148 SamplingStrategyResponse
& response
)
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
>());
156 itr
= json
.find("rateLimitingSampling");
157 if (itr
!= std::end(json
)) {
158 response
.__set_rateLimitingSampling(
159 itr
->get
<RateLimitingSamplingStrategy
>());
161 itr
= json
.find("operationSampling");
162 if (itr
!= std::end(json
)) {
163 response
.__set_operationSampling(
164 itr
->get
<PerOperationSamplingStrategies
>());
168 #undef FIELD_FROM_JSON
169 #undef JSON_FROM_FIELD
171 } // namespace thrift
172 } // namespace sampling_manager
173 } // namespace jaegertracing
175 #endif // JAEGERTRACING_SAMPLERS_REMOTESAMPLINGJSON_H