summaryrefslogtreecommitdiffstats
path: root/src/blkin/blkin-lib/zipkin_c.h
blob: 77b5bc6d768385c411fed06bf4c8b268e8a6eb87 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * 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 ZIPKIN_C_H_
#define ZIPKIN_C_H_

#include <stdint.h>
#include <asm/byteorder.h>

#define BLKIN_TIMESTAMP(trace, endp, event)				\
	do {								\
		struct blkin_annotation __annot;			\
		blkin_init_timestamp_annotation(&__annot, event, endp); \
		blkin_record(trace, &__annot);				\
	} while (0);

#define BLKIN_KEYVAL_STRING(trace, endp, key, val)			\
	do {								\
		struct blkin_annotation __annot;			\
		blkin_init_string_annotation(&__annot, key, val, endp);	\
		blkin_record(trace, &__annot);				\
	} while (0);

#define BLKIN_KEYVAL_INTEGER(trace, endp, key, val)			\
	do {								\
		struct blkin_annotation __annot;			\
		blkin_init_integer_annotation(&__annot, key, val, endp);\
		blkin_record(trace, &__annot);				\
	} while (0);

/**
 * Core annotations used by Zipkin used to denote the beginning and end of 
 * client and server spans.
 * For more information refer to
 * https://github.com/openzipkin/zipkin/blob/master/zipkin-thrift/src/main/thrift/com/twitter/zipkin/zipkinCore.thrift
 */
extern const char* const CLIENT_SEND;
extern const char* const CLIENT_RECV;
extern const char* const SERVER_SEND;
extern const char* const SERVER_RECV;
extern const char* const WIRE_SEND;
extern const char* const WIRE_RECV;
extern const char* const CLIENT_SEND_FRAGMENT;
extern const char* const CLIENT_RECV_FRAGMENT;
extern const char* const SERVER_SEND_FRAGMENT;
extern const char* const SERVER_RECV_FRAGMENT;

/**
 * @struct blkin_endpoint
 * Information about an endpoint of our instrumented application where
 * annotations take place
 */
struct blkin_endpoint {
    const char *ip;
    int16_t port;
    const char *name;
};

/**
 * @struct blkin_trace_info
 * The information exchanged between different layers offering the needed
 * trace semantics
 */
struct blkin_trace_info {
    int64_t trace_id;
    int64_t span_id;
    int64_t parent_span_id;
};

/**
 * @struct blkin_trace_info_packed
 *
 * Packed version of the struct blkin_trace_info. Usefull when sending over
 * network.
 *
 */
struct blkin_trace_info_packed {
	__be64 trace_id;
	__be64 span_id;
	__be64 parent_span_id;
} __attribute__((packed));


/**
 * @struct blkin_trace
 * Struct used to define the context in which an annotation happens
 */
struct blkin_trace {
    const char *name;
    struct blkin_trace_info info;
    const struct blkin_endpoint *endpoint;
};

/**
 * @typedef blkin_annotation_type
 * There are 2 kinds of annotation key-val and timestamp
 */
typedef enum {
    ANNOT_STRING = 0,
    ANNOT_INTEGER,
    ANNOT_TIMESTAMP
} blkin_annotation_type;

/**
 * @struct blkin_annotation
 * Struct carrying information about an annotation. This information can either
 * be key-val or that a specific event happened
 */
struct blkin_annotation {
    blkin_annotation_type type;
    const char *key;
    union {
	const char *val_str;
	int64_t val_int;
    };
    const struct blkin_endpoint *endpoint;
};

/**
 * Initialize the zipkin library.
 *
 * @return 0 on success
 */
int blkin_init();

/**
 * Initialize a new blkin_trace with the information given. The new trace will
 * have no parent so the parent id will be zero.
 *
 * @param new_trace the blkin_trace to be initialized
 * @param name the trace's name
 * @param endpoint a pointer to a blkin_endpoint struct that contains
 *        info about where the specific trace takes place
 *
 * @returns 0 on success or negative error code
 */
int blkin_init_new_trace(struct blkin_trace *new_trace, const char *name,
			 const struct blkin_endpoint *endpoint);

/**
 * Initialize blkin_trace_info for a root span. The new trace will
 * have no parent so the parent id will be zero, and the id and trace id will
 * be the same.
 *
 * @param trace_info the blkin_trace_info to be initialized
 */
void blkin_init_trace_info(struct blkin_trace_info *trace_info);

/**
 * Initialize a blkin_trace as a child of the given parent
 * bkin_trace. The child trace will have the same trace_id, new
 * span_id and parent_span_id its parent's span_id.
 *
 * @param child the blkin_trace to be initialized
 * @param parent the parent blkin_trace
 * @param child_name the blkin_trace name of the child
 *
 * @returns 0 on success or negative error code
 */
int blkin_init_child(struct blkin_trace *child,
		     const struct blkin_trace *parent,
		     const struct blkin_endpoint *endpoint,
		     const char *child_name);

/**
 * Initialize a blkin_trace struct and set the blkin_trace_info field to be
 * child of the given blkin_trace_info. This means
 * Same trace_id
 * Different span_id
 * Child's parent_span_id == parent's span_id
 *
 * @param child the new child blkin_trace_info
 * @param info the parent's blkin_trace_info struct
 * @param child_name the blkin_trace struct name field
 *
 * @returns 0 on success or negative error code
 */
int blkin_init_child_info(struct blkin_trace *child,
			  const struct blkin_trace_info *info,
			  const struct blkin_endpoint *endpoint,
			  const char *child_name);

/**
 * Initialize a blkin_endpoint struct with the information given
 *
 * @param endp the endpoint to be initialized
 * @param ip the ip address of the specific endpoint
 * @param port the TCP/UDP port of the specific endpoint
 * @param name the name of the service running on the specific endpoint
 *
 * @returns 0 on success or negative error code
 */
int blkin_init_endpoint(struct blkin_endpoint *endpoint,
			const char *ip, int16_t port, const char *name);

/**
 * Initialize a key-value blkin_annotation
 *
 * @param annotation the annotation to be initialized
 * @param key the annotation's key
 * @param val the annotation's string value
 * @param endpoint where did this annotation occured
 *
 * @returns 0 on success or negative error code
 */
int blkin_init_string_annotation(struct blkin_annotation *annotation,
				 const char *key, const char *val,
				 const struct blkin_endpoint *endpoint);
/**
 * Initialize a key-value blkin_annotation
 *
 * @param annotation the annotation to be initialized
 * @param key the annotation's key
 * @param val the annotation's  int value
 * @param endpoint where did this annotation occured
 *
 * @returns 0 on success or negative error code
 */

int blkin_init_integer_annotation(struct blkin_annotation *annotation,
				  const char *key, int64_t val,
				  const struct blkin_endpoint *endpoint);

/**
 * Initialize a timestamp blkin_annotation
 *
 * @param annotation the annotation to be initialized
 * @param event the event happened to be annotated
 * @param endpoint where did this annotation occured
 *
 * @returns 0 on success or negative error code
 */

int blkin_init_timestamp_annotation(struct blkin_annotation *annot,
				    const char *event,
				    const struct blkin_endpoint *endpoint);

/**
 * Log an annotation in terms of a specific trace
 *
 * @param trace the trace to which the annotation belongs
 * @param annotation the annotation to be logged
 *
 * @returns 0 on success or negative error code
 */
int blkin_record(const struct blkin_trace *trace,
		 const struct blkin_annotation *annotation);

/**
 * Copy a blkin_trace_info struct into a the field info of a blkin_trace struct
 *
 * @param trace the destination
 * @param info where to copy from
 *
 * @returns 0 on success or negative error code
 */
int blkin_get_trace_info(const struct blkin_trace *trace,
			 struct blkin_trace_info *info);

/**
 * Copy the blkin_trace_info from a blkin_trace to another blkin_trace_info
 *
 * @param trace the trace with the essential info
 * @param info the destination
 *
 * @returns 0 on success or negative error code
 */
int blkin_set_trace_info(struct blkin_trace *trace,
			 const struct blkin_trace_info *info);

/**
 * Set the trace information, name and endpoint of a trace.
 *
 * @param trace the trace to which the properties will be assigned
 * @param info blkin_trace_information with the trace identifiers
 * @param name span name
 * @param endpoint associated host
 *
 * @returns 0 on success or negative error code
 */
int blkin_set_trace_properties(struct blkin_trace *trace,
			 const struct blkin_trace_info *info,
			 const char *name,
			 const struct blkin_endpoint *endpoint);

/**
 * Convert a blkin_trace_info to the packed version.
 *
 * @param info The unpacked trace info.
 * @param pinfo The provided packed version to be initialized.
 *
 * @returns 0 on success or negative error code
 */
int blkin_pack_trace_info(const struct blkin_trace_info *info,
			  struct blkin_trace_info_packed *pinfo);

/**
 * Convert a packed blkin_trace_info to the unpacked version.
 *
 * @param pinfo The provided packed version to be unpacked.
 * @param info The unpacked trace info.
 *
 * @returns 0 on success or negative error code
 */
int blkin_unpack_trace_info(const struct blkin_trace_info_packed *pinfo,
			    struct blkin_trace_info *info);

#endif /* ZIPKIN_C_H_ */