summaryrefslogtreecommitdiffstats
path: root/src/blkin/blkin-lib/ztracer.hpp
blob: 3c4707ea4155b36d0c271cc4dce76a259430b528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright 2014 Marios Kogias <marioskogias@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 *   1. Redistributions of source code must retain the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer.
 *   2. Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef ZTRACER_H

#define ZTRACER_H

#include <string>
extern "C" {
#include <zipkin_c.h>
}

namespace ZTracer {
    using std::string;

    const char* const CLIENT_SEND = "cs";
    const char* const CLIENT_RECV = "cr";
    const char* const SERVER_SEND = "ss";
    const char* const SERVER_RECV = "sr";
    const char* const WIRE_SEND = "ws";
    const char* const WIRE_RECV = "wr";
    const char* const CLIENT_SEND_FRAGMENT = "csf";
    const char* const CLIENT_RECV_FRAGMENT = "crf";
    const char* const SERVER_SEND_FRAGMENT = "ssf";
    const char* const SERVER_RECV_FRAGMENT = "srf";

    static inline int ztrace_init() { return blkin_init(); }

    class Endpoint : private blkin_endpoint {
    private:
	string _ip; // storage for blkin_endpoint.ip, see copy_ip()
	string _name; // storage for blkin_endpoint.name, see copy_name()

	friend class Trace;
    public:
	Endpoint(const char *name)
	    {
		blkin_init_endpoint(this, "0.0.0.0", 0, name);
	    }
	Endpoint(const char *ip, int port, const char *name)
	    {
		blkin_init_endpoint(this, ip, port, name);
	    }

	// copy constructor and operator need to account for ip/name storage
	Endpoint(const Endpoint &rhs) : _ip(rhs._ip), _name(rhs._name)
	    {
		blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
				    rhs.port,
				    _name.size() ? _name.c_str(): rhs.name);
	    }
	const Endpoint& operator=(const Endpoint &rhs)
	    {
		_ip.assign(rhs._ip);
		_name.assign(rhs._name);
		blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip,
				    rhs.port,
				    _name.size() ? _name.c_str() : rhs.name);
		return *this;
	    }

	// Endpoint assumes that ip and name will be string literals, and
	// avoids making a copy and freeing on destruction.  if you need to
	// initialize Endpoint with a temporary string, copy_ip/copy_name()
	// will store it in a std::string and assign from that
	void copy_ip(const string &newip)
	    {
		_ip.assign(newip);
		ip = _ip.c_str();
	    }
	void copy_name(const string &newname)
	    {
		_name.assign(newname);
		name = _name.c_str();
	    }

	void copy_address_from(const Endpoint *endpoint)
	    {
		_ip.assign(endpoint->ip);
		ip = _ip.c_str();
		port = endpoint->port;
	    }
	void share_address_from(const Endpoint *endpoint)
	    {
		ip = endpoint->ip;
		port = endpoint->port;
	    }
	void set_port(int p) { port = p; }
    };

    class Trace : private blkin_trace {
    private:
	string _name; // storage for blkin_trace.name, see copy_name()

    public:
	// default constructor zero-initializes blkin_trace valid()
	// will return false on a default-constructed Trace until
	// init()
	Trace()
	    {
		// zero-initialize so valid() returns false
		name = NULL;
		info.trace_id = 0;
		info.span_id = 0;
		info.parent_span_id = 0;
		endpoint = NULL;
	    }

	// construct a Trace with an optional parent
	Trace(const char *name, const Endpoint *ep, const Trace *parent = NULL)
	    {
		if (parent && parent->valid()) {
		    blkin_init_child(this, parent, ep ? : parent->endpoint,
				     name);
		} else {
		    blkin_init_new_trace(this, name, ep);
		}
	    }

	// construct a Trace from blkin_trace_info
	Trace(const char *name, const Endpoint *ep,
	      const blkin_trace_info *i, bool child=false)
	    {
		if (child)
		    blkin_init_child_info(this, i, ep, name);
		else {
		    blkin_init_new_trace(this, name, ep);
		    set_info(i);
		}
	    }

	// copy constructor and operator need to account for name storage
	Trace(const Trace &rhs) : _name(rhs._name)
	    {
		name = _name.size() ? _name.c_str() : rhs.name;
		info = rhs.info;
		endpoint = rhs.endpoint;
	    }
	const Trace& operator=(const Trace &rhs)
	    {
		_name.assign(rhs._name);
		name = _name.size() ? _name.c_str() : rhs.name;
		info = rhs.info;
		endpoint = rhs.endpoint;
		return *this;
	    }

	// return true if the Trace has been initialized
	bool valid() const { return info.trace_id != 0; }
	operator bool() const { return valid(); }

	// (re)initialize a Trace with an optional parent
	int init(const char *name, const Endpoint *ep,
		 const Trace *parent = NULL)
	    {
		if (parent && parent->valid())
		    return blkin_init_child(this, parent,
					    ep ? : parent->endpoint, name);

		return blkin_init_new_trace(this, name, ep);
	    }

	// (re)initialize a Trace from blkin_trace_info
	int init(const char *name, const Endpoint *ep,
		 const blkin_trace_info *i, bool child=false)
	    {
		if (child)
		    return blkin_init_child_info(this, i, ep, _name.c_str());

		return blkin_set_trace_properties(this, i, _name.c_str(), ep);
	    }

	// Trace assumes that name will be a string literal, and avoids
	// making a copy and freeing on destruction.  if you need to
	// initialize Trace with a temporary string, copy_name() will store
	// it in a std::string and assign from that
	void copy_name(const string &newname)
	    {
		_name.assign(newname);
		name = _name.c_str();
	    }

	const blkin_trace_info* get_info() const { return &info; }
	void set_info(const blkin_trace_info *i) { info = *i; }

	// record key-value annotations
	void keyval(const char *key, const char *val) const
	    {
		if (valid())
		    BLKIN_KEYVAL_STRING(this, endpoint, key, val);
	    }
	void keyval(const char *key, int64_t val) const
	    {
		if (valid())
		    BLKIN_KEYVAL_INTEGER(this, endpoint, key, val);
	    }
	void keyval(const char *key, const char *val, const Endpoint *ep) const
	    {
		if (valid())
		    BLKIN_KEYVAL_STRING(this, ep, key, val);
	    }
	void keyval(const char *key, int64_t val, const Endpoint *ep) const
	    {
		if (valid())
		    BLKIN_KEYVAL_INTEGER(this, ep, key, val);
	    }

	// record timestamp annotations
	void event(const char *event) const
	    {
		if (valid())
		    BLKIN_TIMESTAMP(this, endpoint, event);
	    }
	void event(const char *event, const Endpoint *ep) const
	    {
		if (valid())
		    BLKIN_TIMESTAMP(this, ep, event);
	    }
    };

}
#endif /* end of include guard: ZTRACER_H */