]> git.proxmox.com Git - ceph.git/blob - ceph/src/blkin/blkin-lib/ztracer.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / blkin / blkin-lib / ztracer.hpp
1 /*
2 * Copyright 2014 Marios Kogias <marioskogias@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef ZTRACER_H
31
32 #define ZTRACER_H
33
34 #include <string>
35 extern "C" {
36 #include <zipkin_c.h>
37 }
38
39 namespace ZTracer {
40 using std::string;
41
42 const char* const CLIENT_SEND = "cs";
43 const char* const CLIENT_RECV = "cr";
44 const char* const SERVER_SEND = "ss";
45 const char* const SERVER_RECV = "sr";
46 const char* const WIRE_SEND = "ws";
47 const char* const WIRE_RECV = "wr";
48 const char* const CLIENT_SEND_FRAGMENT = "csf";
49 const char* const CLIENT_RECV_FRAGMENT = "crf";
50 const char* const SERVER_SEND_FRAGMENT = "ssf";
51 const char* const SERVER_RECV_FRAGMENT = "srf";
52
53 static inline int ztrace_init() { return blkin_init(); }
54
55 class Endpoint : private blkin_endpoint {
56 private:
57 string _ip; // storage for blkin_endpoint.ip, see copy_ip()
58 string _name; // storage for blkin_endpoint.name, see copy_name()
59
60 friend class Trace;
61 public:
62 Endpoint(const char *name)
63 {
64 blkin_init_endpoint(this, "0.0.0.0", 0, name);
65 }
66 Endpoint(const char *ip, int port, const char *name)
67 {
68 blkin_init_endpoint(this, ip, port, name);
69 }
70
71 // copy constructor and operator need to account for ip/name storage
72 Endpoint(const Endpoint &rhs) : _ip(rhs._ip), _name(rhs._name)
73 {
74 blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
75 rhs.port,
76 _name.size() ? _name.c_str(): rhs.name);
77 }
78 const Endpoint& operator=(const Endpoint &rhs)
79 {
80 _ip.assign(rhs._ip);
81 _name.assign(rhs._name);
82 blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
83 rhs.port,
84 _name.size() ? _name.c_str() : rhs.name);
85 return *this;
86 }
87
88 // Endpoint assumes that ip and name will be string literals, and
89 // avoids making a copy and freeing on destruction. if you need to
90 // initialize Endpoint with a temporary string, copy_ip/copy_name()
91 // will store it in a std::string and assign from that
92 void copy_ip(const string &newip)
93 {
94 _ip.assign(newip);
95 ip = _ip.c_str();
96 }
97 void copy_name(const string &newname)
98 {
99 _name.assign(newname);
100 name = _name.c_str();
101 }
102
103 void copy_address_from(const Endpoint *endpoint)
104 {
105 _ip.assign(endpoint->ip);
106 ip = _ip.c_str();
107 port = endpoint->port;
108 }
109 void share_address_from(const Endpoint *endpoint)
110 {
111 ip = endpoint->ip;
112 port = endpoint->port;
113 }
114 void set_port(int p) { port = p; }
115 };
116
117 class Trace : private blkin_trace {
118 private:
119 string _name; // storage for blkin_trace.name, see copy_name()
120
121 public:
122 // default constructor zero-initializes blkin_trace valid()
123 // will return false on a default-constructed Trace until
124 // init()
125 Trace()
126 {
127 // zero-initialize so valid() returns false
128 name = NULL;
129 info.trace_id = 0;
130 info.span_id = 0;
131 info.parent_span_id = 0;
132 endpoint = NULL;
133 }
134
135 // construct a Trace with an optional parent
136 Trace(const char *name, const Endpoint *ep, const Trace *parent = NULL)
137 {
138 if (parent && parent->valid()) {
139 blkin_init_child(this, parent, ep ? : parent->endpoint,
140 name);
141 } else {
142 blkin_init_new_trace(this, name, ep);
143 }
144 }
145
146 // construct a Trace from blkin_trace_info
147 Trace(const char *name, const Endpoint *ep,
148 const blkin_trace_info *i, bool child=false)
149 {
150 if (child)
151 blkin_init_child_info(this, i, ep, name);
152 else {
153 blkin_init_new_trace(this, name, ep);
154 set_info(i);
155 }
156 }
157
158 // copy constructor and operator need to account for name storage
159 Trace(const Trace &rhs) : _name(rhs._name)
160 {
161 name = _name.size() ? _name.c_str() : rhs.name;
162 info = rhs.info;
163 endpoint = rhs.endpoint;
164 }
165 const Trace& operator=(const Trace &rhs)
166 {
167 _name.assign(rhs._name);
168 name = _name.size() ? _name.c_str() : rhs.name;
169 info = rhs.info;
170 endpoint = rhs.endpoint;
171 return *this;
172 }
173
174 // return true if the Trace has been initialized
175 bool valid() const { return info.trace_id != 0; }
176 operator bool() const { return valid(); }
177
178 // (re)initialize a Trace with an optional parent
179 int init(const char *name, const Endpoint *ep,
180 const Trace *parent = NULL)
181 {
182 if (parent && parent->valid())
183 return blkin_init_child(this, parent,
184 ep ? : parent->endpoint, name);
185
186 return blkin_init_new_trace(this, name, ep);
187 }
188
189 // (re)initialize a Trace from blkin_trace_info
190 int init(const char *name, const Endpoint *ep,
191 const blkin_trace_info *i, bool child=false)
192 {
193 if (child)
194 return blkin_init_child_info(this, i, ep, _name.c_str());
195
196 return blkin_set_trace_properties(this, i, _name.c_str(), ep);
197 }
198
199 // Trace assumes that name will be a string literal, and avoids
200 // making a copy and freeing on destruction. if you need to
201 // initialize Trace with a temporary string, copy_name() will store
202 // it in a std::string and assign from that
203 void copy_name(const string &newname)
204 {
205 _name.assign(newname);
206 name = _name.c_str();
207 }
208
209 const blkin_trace_info* get_info() const { return &info; }
210 void set_info(const blkin_trace_info *i) { info = *i; }
211
212 // record key-value annotations
213 void keyval(const char *key, const char *val) const
214 {
215 if (valid())
216 BLKIN_KEYVAL_STRING(this, endpoint, key, val);
217 }
218 void keyval(const char *key, int64_t val) const
219 {
220 if (valid())
221 BLKIN_KEYVAL_INTEGER(this, endpoint, key, val);
222 }
223 void keyval(const char *key, const char *val, const Endpoint *ep) const
224 {
225 if (valid())
226 BLKIN_KEYVAL_STRING(this, ep, key, val);
227 }
228 void keyval(const char *key, int64_t val, const Endpoint *ep) const
229 {
230 if (valid())
231 BLKIN_KEYVAL_INTEGER(this, ep, key, val);
232 }
233
234 // record timestamp annotations
235 void event(const char *event) const
236 {
237 if (valid())
238 BLKIN_TIMESTAMP(this, endpoint, event);
239 }
240 void event(const char *event, const Endpoint *ep) const
241 {
242 if (valid())
243 BLKIN_TIMESTAMP(this, ep, event);
244 }
245 };
246
247 }
248 #endif /* end of include guard: ZTRACER_H */