]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/testutils/MockAgent.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / testutils / MockAgent.h
CommitLineData
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_MOCKAGENT_H
18#define JAEGERTRACING_TESTUTILS_MOCKAGENT_H
19
20#include "jaegertracing/Compilers.h"
21
22#include "jaegertracing/baggage/RemoteRestrictionManager.h"
23#include "jaegertracing/net/IPAddress.h"
24#include "jaegertracing/testutils/SamplingManager.h"
25#include "jaegertracing/testutils/TUDPTransport.h"
26#include "jaegertracing/thrift-gen/Agent.h"
27#include "jaegertracing/thrift-gen/jaeger_types.h"
28#include "jaegertracing/utils/UDPTransporter.h"
29#include <atomic>
30#include <future>
31#include <memory>
32#include <mutex>
33#include <stdexcept>
34#include <string>
35#include <thread>
36#include <type_traits>
37#include <utility>
38#include <vector>
39
40namespace jaegertracing {
41namespace baggage {
42class Restriction;
43} // namespace baggage
44} // namespace jaegertracing
45namespace twitter {
46namespace zipkin {
47namespace thrift {
48class Span;
49} // namespace thrift
50} // namespace zipkin
51} // namespace twitter
52
53namespace jaegertracing {
54namespace testutils {
55
56class MockAgent : public agent::thrift::AgentIf,
57 public std::enable_shared_from_this<MockAgent> {
58 public:
59 using KeyRestrictionMap =
60 baggage::RemoteRestrictionManager::KeyRestrictionMap;
61
62 static std::shared_ptr<MockAgent> make()
63 {
64 // Avoid `make_shared` when `weak_ptr` might be used.
65 std::shared_ptr<MockAgent> newInstance(new MockAgent());
66 return newInstance;
67 }
68
69 ~MockAgent();
70
71 void start();
72
73 void close();
74
75 void
76 emitZipkinBatch(const std::vector<twitter::zipkin::thrift::Span>&) override
77 {
78 throw std::logic_error("emitZipkinBatch not implemented");
79 }
80
81 void emitBatch(const thrift::Batch& batch) override;
82
83 bool isServingUDP() const { return _servingUDP; }
84
85 bool isServingHTTP() const { return _servingHTTP; }
86
87 template <typename... Args>
88 void addSamplingStrategy(Args&&... args)
89 {
90 _samplingMgr.addSamplingStrategy(std::forward<Args>(args)...);
91 }
92
93 void addBaggageRestriction(const std::string& key,
94 const baggage::Restriction& restriction)
95 {
96 _restrictions.insert(std::make_pair(key, restriction));
97 }
98
99 std::vector<thrift::Batch> batches() const
100 {
101 std::lock_guard<std::mutex> lock(_mutex);
102 return _batches;
103 }
104
105 net::IPAddress spanServerAddress() const { return _transport.addr(); }
106
107 std::unique_ptr<utils::Transport> spanServerClient()
108 {
109 return std::unique_ptr<utils::Transport>(
110 new utils::UDPTransporter(spanServerAddress(), 0));
111 }
112
113 net::IPAddress samplingServerAddress() const { return _httpAddress; }
114
115 void resetBatches()
116 {
117 std::lock_guard<std::mutex> lock(_mutex);
118 _batches.clear();
119 }
120
121 private:
122 MockAgent();
123
124 void serveUDP(std::promise<void>& started);
125
126 void serveHTTP(std::promise<void>& started);
127
128 TUDPTransport _transport;
129 std::vector<thrift::Batch> _batches;
130 std::atomic<bool> _servingUDP;
131 std::atomic<bool> _servingHTTP;
132 SamplingManager _samplingMgr;
133 KeyRestrictionMap _restrictions;
134 mutable std::mutex _mutex;
135 std::thread _udpThread;
136 std::thread _httpThread;
137 net::IPAddress _httpAddress;
138};
139
140} // namespace testutils
141} // namespace jaegertracing
142
143#endif // JAEGERTRACING_TESTUTILS_MOCKAGENT_H