summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/ctraces/src/ctr_span.c
blob: d93935a72335e21c7efafb174559e552fb4a4d1e (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  CTraces
 *  =======
 *  Copyright 2022 The CTraces Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <ctraces/ctraces.h>

#include <cfl/cfl.h>
#include <cfl/cfl_time.h>
#include <cfl/cfl_kvlist.h>

struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span *scope_span, cfl_sds_t name,
                                    struct ctrace_span *parent)
{
    struct ctrace_span *span;

    if (!ctx || !scope_span || !name) {
        return NULL;
    }

    /* allocate a spanc context */
    span = calloc(1, sizeof(struct ctrace_span));
    if (!span) {
        ctr_errno();
        return NULL;
    }

    /* references */
    span->scope_span = scope_span;
    span->ctx = ctx;

    /* name */
    span->name = cfl_sds_create(name);
    if (!span->name) {
        free(span);
        return NULL;
    }

    /* attributes */
    span->attr = ctr_attributes_create();
    if (!span->attr) {
        free(span);
        return NULL;
    }
    cfl_list_init(&span->events);
    cfl_list_init(&span->links);

    /* dropped attributes count */
    span->dropped_attr_count = 0;

    /* if a parent context was given, populate the span parent id */
    if (parent && parent->span_id) {
        ctr_span_set_parent_span_id_with_cid(span, parent->span_id);
    }

    /* link span to struct scope_span->spans */
    cfl_list_add(&span->_head, &scope_span->spans);

    /* link span to the struct ctrace->span_list */
    cfl_list_add(&span->_head_global, &ctx->span_list);

    /* set default kind */
    ctr_span_kind_set(span, CTRACE_SPAN_INTERNAL);

    /* always start a span by default, the start can be overriden later if needed */
    ctr_span_start(ctx, span);
    return span;
}

/* Set the Span ID with a given buffer and length */
int ctr_span_set_trace_id(struct ctrace_span *span, void *buf, size_t len)
{
    if (!buf || len <= 0) {
        return -1;
    }

    /* If trace_id is already set, free it first */
    if (span->trace_id != NULL) {
        ctr_id_destroy(span->trace_id);
        span->trace_id = NULL;
    }

    span->trace_id = ctr_id_create(buf, len);
    if (!span->trace_id) {
        return -1;
    }

    return 0;
}

/* Set the Span ID by using a ctrace_id context */
int ctr_span_set_trace_id_with_cid(struct ctrace_span *span, struct ctrace_id *cid)
{
    return ctr_span_set_trace_id(span,
                                 ctr_id_get_buf(cid),
                                 ctr_id_get_len(cid));
}

/* Set the Span ID with a given buffer and length */
int ctr_span_set_span_id(struct ctrace_span *span, void *buf, size_t len)
{
    if (!buf || len <= 0) {
        return -1;
    }

    span->span_id = ctr_id_create(buf, len);
    if (!span->span_id) {
        return -1;
    }

    return 0;
}

/* Set the Span ID by using a ctrace_id context */
int ctr_span_set_span_id_with_cid(struct ctrace_span *span, struct ctrace_id *cid)
{
    return ctr_span_set_span_id(span,
                                ctr_id_get_buf(cid),
                                ctr_id_get_len(cid));
}

/* Set the Span Parent ID with a given buffer and length */
int ctr_span_set_parent_span_id(struct ctrace_span *span, void *buf, size_t len)
{
    if (!buf || len <= 0) {
        return -1;
    }

    if (span->parent_span_id) {
        ctr_id_destroy(span->parent_span_id);
    }

    span->parent_span_id = ctr_id_create(buf, len);
    if (!span->parent_span_id) {
        return -1;
    }

    return 0;
}

/* Set the Span ID by using a ctrace_id context */
int ctr_span_set_parent_span_id_with_cid(struct ctrace_span *span, struct ctrace_id *cid)
{
    return ctr_span_set_parent_span_id(span,
                                       ctr_id_get_buf(cid),
                                       ctr_id_get_len(cid));
}

int ctr_span_kind_set(struct ctrace_span *span, int kind)
{
    if (kind < CTRACE_SPAN_UNSPECIFIED || kind > CTRACE_SPAN_CONSUMER) {
        return -1;
    }

    span->kind = kind;
    return 0;
}

/* returns a read-only version of the Span kind */
char *ctr_span_kind_string(struct ctrace_span *span)
{
    switch (span->kind) {
        case CTRACE_SPAN_INTERNAL:
            return "internal";
        case CTRACE_SPAN_SERVER:
            return "server";
        case CTRACE_SPAN_CLIENT:
            return "client";
        case CTRACE_SPAN_PRODUCER:
            return "producer";
        case CTRACE_SPAN_CONSUMER:
            return "consumer";
        default:
            return "unspecified";
    };
}

/*
 * Span attributes
 * ---------------
 */
int ctr_span_set_attribute_string(struct ctrace_span *span, char *key, char *value)
{
    return ctr_attributes_set_string(span->attr, key, value);
}

int ctr_span_set_attribute_bool(struct ctrace_span *span, char *key, int b)
{
    return ctr_attributes_set_bool(span->attr, key, b);
}

int ctr_span_set_attribute_int64(struct ctrace_span *span, char *key, int64_t value)
{
    return ctr_attributes_set_int64(span->attr, key, value);
}

int ctr_span_set_attribute_double(struct ctrace_span *span, char *key, double value)
{
    return ctr_attributes_set_double(span->attr, key, value);
}

int ctr_span_set_attribute_array(struct ctrace_span *span, char *key,
                                 struct cfl_array *value)
{
    return ctr_attributes_set_array(span->attr, key, value);
}

int ctr_span_set_attribute_kvlist(struct ctrace_span *span, char *key,
                                  struct cfl_kvlist *value)
{

    return ctr_attributes_set_kvlist(span->attr, key, value);
}

void ctr_span_start(struct ctrace *ctx, struct ctrace_span *span)
{
    uint64_t ts;

    ts = cfl_time_now();
    ctr_span_start_ts(ctx, span, ts);
}

void ctr_span_start_ts(struct ctrace *ctx, struct ctrace_span *span, uint64_t ts)
{
    /* set the initial timestamp */
    span->start_time_unix_nano = ts;

    /* always set the span end time as the start time, so duration can be zero */
    ctr_span_end_ts(ctx, span, ts);
}

void ctr_span_end(struct ctrace *ctx, struct ctrace_span *span)
{
    uint64_t ts;

    ts = cfl_time_now();
    ctr_span_end_ts(ctx, span, ts);
}

void ctr_span_end_ts(struct ctrace *ctx, struct ctrace_span *span, uint64_t ts)
{
    span->end_time_unix_nano = ts;
}

int ctr_span_set_status(struct ctrace_span *span, int code, char *message)
{
    struct ctrace_span_status *status;

    status = &span->status;
    if (status->message) {
        cfl_sds_destroy(status->message);
    }

    if (message) {
        status->message = cfl_sds_create(message);
        if (!status->message) {
            return -1;
        }
    }

    status->code = code;
    return 0;
}

void ctr_span_set_dropped_events_count(struct ctrace_span *span, uint32_t count)
{
    span->dropped_events_count = count;
}

void ctr_span_set_dropped_attributes_count(struct ctrace_span *span, uint32_t count)
{
    span->dropped_attr_count = count;
}

void ctr_span_destroy(struct ctrace_span *span)
{
    struct cfl_list *tmp;
    struct cfl_list *head;
    struct ctrace_span_event *event;
    struct ctrace_span_status *status;
    struct ctrace_link *link;

    if (span->name) {
        cfl_sds_destroy(span->name);
    }

    if (span->trace_id) {
        ctr_id_destroy(span->trace_id);
    }

    if (span->span_id) {
        ctr_id_destroy(span->span_id);
    }

    if (span->parent_span_id) {
        ctr_id_destroy(span->parent_span_id);
    }

    /* attributes */
    if (span->attr) {
        ctr_attributes_destroy(span->attr);
    }

    /* events */
    cfl_list_foreach_safe(head, tmp, &span->events) {
        event = cfl_list_entry(head, struct ctrace_span_event, _head);
        ctr_span_event_delete(event);
    }

    /* links */
    cfl_list_foreach_safe(head, tmp, &span->links) {
        link = cfl_list_entry(head, struct ctrace_link, _head);
        ctr_link_destroy(link);
    }

    /* status */
    status = &span->status;
    if (status->message) {
        cfl_sds_destroy(status->message);
    }

    cfl_list_del(&span->_head);
    cfl_list_del(&span->_head_global);
    free(span);
}

/*
 * Span Events
 * -----------
 */
struct ctrace_span_event *ctr_span_event_add_ts(struct ctrace_span *span, char *name, uint64_t ts)
{
    struct ctrace_span_event *ev;

    if (!name) {
        return NULL;
    }

    ev = calloc(1, sizeof(struct ctrace_span_event));
    if (!ev) {
        ctr_errno();
        return NULL;
    }
    ev->name = cfl_sds_create(name);
    if (!ev->name) {
        free(ev);
        return NULL;
    }
    ev->attr = ctr_attributes_create(128);
    ev->dropped_attr_count = 0;

    /* if no timestamp is given, use the current time */
    if (ts == 0) {
        ev->time_unix_nano = cfl_time_now();
    }
    else {
        ev->time_unix_nano = ts;
    }

    cfl_list_add(&ev->_head, &span->events);
    return ev;
}

struct ctrace_span_event *ctr_span_event_add(struct ctrace_span *span, char *name)
{
    return ctr_span_event_add_ts(span, name, 0);
}

int ctr_span_event_set_attribute_string(struct ctrace_span_event *event, char *key, char *value)
{
    return ctr_attributes_set_string(event->attr, key, value);
}

int ctr_span_event_set_attribute_bool(struct ctrace_span_event *event, char *key, int b)
{
    return ctr_attributes_set_bool(event->attr, key, b);
}

int ctr_span_event_set_attribute_int64(struct ctrace_span_event *event, char *key, int64_t value)
{
    return ctr_attributes_set_int64(event->attr, key, value);
}

int ctr_span_event_set_attribute_double(struct ctrace_span_event *event, char *key, double value)
{
    return ctr_attributes_set_double(event->attr, key, value);
}

int ctr_span_event_set_attribute_array(struct ctrace_span_event *event, char *key,
                                       struct cfl_array *value)
{
    return ctr_attributes_set_array(event->attr, key, value);
}

int ctr_span_event_set_attribute_kvlist(struct ctrace_span_event *event, char *key,
                                        struct cfl_kvlist *value)
{

    return ctr_attributes_set_kvlist(event->attr, key, value);
}

void ctr_span_event_set_dropped_attributes_count(struct ctrace_span_event *event, uint32_t count)
{
    event->dropped_attr_count = count;
}

void ctr_span_event_delete(struct ctrace_span_event *event)
{
    if (event->name) {
        cfl_sds_destroy(event->name);
    }

    if (event->attr) {
        ctr_attributes_destroy(event->attr);
    }

    cfl_list_del(&event->_head);
    free(event);
}