2 * Copyright 2014 Marios Kogias <marioskogias@gmail.com>
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
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.
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.
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";
53 static inline int ztrace_init() { return blkin_init(); }
55 class Endpoint : private blkin_endpoint {
57 string _ip; // storage for blkin_endpoint.ip, see copy_ip()
58 string _name; // storage for blkin_endpoint.name, see copy_name()
62 Endpoint(const char *name)
64 blkin_init_endpoint(this, "0.0.0.0", 0, name);
66 Endpoint(const char *ip, int port, const char *name)
68 blkin_init_endpoint(this, ip, port, name);
71 // copy constructor and operator need to account for ip/name storage
72 Endpoint(const Endpoint &rhs) : _ip(rhs._ip), _name(rhs._name)
74 blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
76 _name.size() ? _name.c_str(): rhs.name);
78 const Endpoint& operator=(const Endpoint &rhs)
81 _name.assign(rhs._name);
82 blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
84 _name.size() ? _name.c_str() : rhs.name);
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)
97 void copy_name(const string &newname)
99 _name.assign(newname);
100 name = _name.c_str();
103 void copy_address_from(const Endpoint *endpoint)
105 _ip.assign(endpoint->ip);
107 port = endpoint->port;
109 void share_address_from(const Endpoint *endpoint)
112 port = endpoint->port;
114 void set_port(int p) { port = p; }
117 class Trace : private blkin_trace {
119 string _name; // storage for blkin_trace.name, see copy_name()
122 // default constructor zero-initializes blkin_trace valid()
123 // will return false on a default-constructed Trace until
127 // zero-initialize so valid() returns false
131 info.parent_span_id = 0;
135 // construct a Trace with an optional parent
136 Trace(const char *name, const Endpoint *ep, const Trace *parent = NULL)
138 if (parent && parent->valid()) {
139 blkin_init_child(this, parent, ep ? : parent->endpoint,
142 blkin_init_new_trace(this, name, ep);
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)
151 blkin_init_child_info(this, i, ep, name);
153 blkin_init_new_trace(this, name, ep);
158 // copy constructor and operator need to account for name storage
159 Trace(const Trace &rhs) : _name(rhs._name)
161 name = _name.size() ? _name.c_str() : rhs.name;
163 endpoint = rhs.endpoint;
165 const Trace& operator=(const Trace &rhs)
167 _name.assign(rhs._name);
168 name = _name.size() ? _name.c_str() : rhs.name;
170 endpoint = rhs.endpoint;
174 // return true if the Trace has been initialized
175 bool valid() const { return info.trace_id != 0; }
176 operator bool() const { return valid(); }
178 // (re)initialize a Trace with an optional parent
179 int init(const char *name, const Endpoint *ep,
180 const Trace *parent = NULL)
182 if (parent && parent->valid())
183 return blkin_init_child(this, parent,
184 ep ? : parent->endpoint, name);
186 return blkin_init_new_trace(this, name, ep);
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)
194 return blkin_init_child_info(this, i, ep, _name.c_str());
196 return blkin_set_trace_properties(this, i, _name.c_str(), ep);
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)
205 _name.assign(newname);
206 name = _name.c_str();
209 const blkin_trace_info* get_info() const { return &info; }
210 void set_info(const blkin_trace_info *i) { info = *i; }
212 // record key-value annotations
213 void keyval(const char *key, const char *val) const
216 BLKIN_KEYVAL_STRING(this, endpoint, key, val);
218 void keyval(const char *key, int64_t val) const
221 BLKIN_KEYVAL_INTEGER(this, endpoint, key, val);
223 void keyval(const char *key, const char *val, const Endpoint *ep) const
226 BLKIN_KEYVAL_STRING(this, ep, key, val);
228 void keyval(const char *key, int64_t val, const Endpoint *ep) const
231 BLKIN_KEYVAL_INTEGER(this, ep, key, val);
234 // record timestamp annotations
235 void event(const char *event) const
238 BLKIN_TIMESTAMP(this, endpoint, event);
240 void event(const char *event, const Endpoint *ep) const
243 BLKIN_TIMESTAMP(this, ep, event);
248 #endif /* end of include guard: ZTRACER_H */