]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/samplers/RateLimitingSampler.h
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / samplers / RateLimitingSampler.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_RATELIMITINGSAMPLER_H
18 #define JAEGERTRACING_SAMPLERS_RATELIMITINGSAMPLER_H
19
20 #include "jaegertracing/Compilers.h"
21
22 #include "jaegertracing/Constants.h"
23 #include "jaegertracing/Tag.h"
24 #include "jaegertracing/samplers/Sampler.h"
25 #include "jaegertracing/samplers/SamplingStatus.h"
26 #include "jaegertracing/utils/RateLimiter.h"
27 #include <algorithm>
28 #include <string>
29 #include <vector>
30
31 namespace jaegertracing {
32 class TraceID;
33 } // namespace jaegertracing
34
35 namespace jaegertracing {
36 namespace samplers {
37
38 class RateLimitingSampler : public Sampler {
39 public:
40 explicit RateLimitingSampler(double maxTracesPerSecond)
41 : _maxTracesPerSecond(maxTracesPerSecond)
42 , _rateLimiter(_maxTracesPerSecond, std::max(_maxTracesPerSecond, 1.0))
43 , _tags({ { kSamplerTypeTagKey, kSamplerTypeRateLimiting },
44 { kSamplerParamTagKey, maxTracesPerSecond } })
45 {
46 }
47
48 SamplingStatus isSampled(const TraceID& id,
49 const std::string& operation) override
50 {
51 return SamplingStatus(_rateLimiter.checkCredit(1), _tags);
52 }
53
54 void close() override {}
55
56 Type type() const override { return Type::kRateLimitingSampler; }
57
58 private:
59 double _maxTracesPerSecond;
60 utils::RateLimiter<> _rateLimiter;
61 std::vector<Tag> _tags;
62 };
63
64 } // namespace samplers
65 } // namespace jaegertracing
66
67 #endif // JAEGERTRACING_SAMPLERS_RATELIMITINGSAMPLER_H