summaryrefslogtreecommitdiffstats
path: root/test/opentracing.c
blob: 2e30fe5faa9fd7f54761a066f887c6d6c32c10e1 (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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/***
 * Copyright 2020 HAProxy Technologies
 *
 * 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 "include.h"


static void ot_text_map_show(const struct otc_text_map *text_map)
{
	OT_FUNC("%p", text_map);

	if (_NULL(text_map))
		return;

	OT_DBG(OT, "%p:{ %p %p %zu/%zu %hhu }", text_map, text_map->key, text_map->value, text_map->count, text_map->size, text_map->is_dynamic);

	if (_nNULL(text_map->key) && _nNULL(text_map->value) && (text_map->count > 0)) {
		size_t i;

		for (i = 0; i < text_map->count; i++)
			OT_DBG(OT, "  \"%s\" -> \"%s\"", text_map->key[i], text_map->value[i]);
	}
}


/***
 * NAME
 *   ot_span_init -
 *
 * ARGUMENTS
 *   operation_name -
 *   ref_type       -
 *   ref_ctx_idx    -
 *   ref_span       -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span *ot_span_init(struct otc_tracer *tracer, const char *operation_name, int ref_type, int ref_ctx_idx, const struct otc_span *ref_span)
{
	struct otc_start_span_options  options;
	struct otc_span_context        context = { .idx = ref_ctx_idx, .span = ref_span };
	struct otc_span_reference      references = { ref_type, &context };
	struct otc_span               *retptr = NULL;

	OT_FUNC("%p, \"%s\", %d, %d, %p", tracer, operation_name, ref_type, ref_ctx_idx, ref_span);

	(void)memset(&options, 0, sizeof(options));

	if (IN_RANGE(ref_type, otc_span_reference_child_of, otc_span_reference_follows_from)) {
		options.references     = &references;
		options.num_references = 1;
	}

	retptr = tracer->start_span_with_options(tracer, operation_name, &options);
	if (_NULL(retptr))
		OT_DBG(OT, "cannot init new span");
	else
		OT_DBG(OT, "span %p:%zu initialized", retptr, retptr->idx);

	return retptr;
}


/***
 * NAME
 *   ot_span_tag -
 *
 * ARGUMENTS
 *   span  -
 *   key   -
 *   type  -
 *   value -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int ot_span_tag(struct otc_span *span, const char *key, int type, ...)
{
	va_list          ap;
	struct otc_value ot_value;
	int              retval = -1;

	OT_FUNC("%p, \"%s\", %d, ...", span, key, type);

	if (_NULL(span))
		return retval;

	va_start(ap, type);
	for (retval = 0; _nNULL(key) && IN_RANGE(type, otc_value_bool, otc_value_null); retval++) {
		ot_value.type = type;
		if (type == otc_value_bool)
			ot_value.value.bool_value = va_arg(ap, typeof(ot_value.value.bool_value));
		else if (type == otc_value_double)
			ot_value.value.double_value = va_arg(ap, typeof(ot_value.value.double_value));
		else if (type == otc_value_int64)
			ot_value.value.int64_value = va_arg(ap, typeof(ot_value.value.int64_value));
		else if (type == otc_value_uint64)
			ot_value.value.uint64_value = va_arg(ap, typeof(ot_value.value.uint64_value));
		else if (type == otc_value_string)
			ot_value.value.string_value = va_arg(ap, typeof(ot_value.value.string_value));
		else if (type == otc_value_null)
			ot_value.value.string_value = va_arg(ap, typeof(ot_value.value.string_value));
		span->set_tag(span, key, &ot_value);

		if (_nNULL(key = va_arg(ap, typeof(key))))
			type = va_arg(ap, typeof(type));
	}
	va_end(ap);

	return retval;
}


/***
 * NAME
 *   ot_span_set_baggage -
 *
 * ARGUMENTS
 *   span  -
 *   key   -
 *   value -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int ot_span_set_baggage(struct otc_span *span, const char *key, const char *value, ...)
{
	va_list ap;
	int     retval = -1;

	OT_FUNC("%p, \"%s\", \"%s\", ...", span, key, value);

	if (_NULL(span))
		return retval;

	va_start(ap, value);
	for (retval = 0; _nNULL(key); retval++) {
		OT_DBG(OT, "set baggage: \"%s\" \"%s\"", key, value);

		span->set_baggage_item(span, key, value);

		if (_nNULL(key = va_arg(ap, typeof(key))))
			value = va_arg(ap, typeof(value));
	}
	va_end(ap);

	return retval;
}


/***
 * NAME
 *   ot_span_baggage -
 *
 * ARGUMENTS
 *   span -
 *   key  -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_text_map *ot_span_baggage(const struct otc_span *span, const char *key, ...)
{
	va_list              ap;
	struct otc_text_map *retptr = NULL;
	int                  i, n;

	OT_FUNC("%p, \"%s\", ...", span, key);

	if (_NULL(span) || _NULL(key))
		return retptr;

	va_start(ap, key);
	for (n = 1; _nNULL(va_arg(ap, typeof(key))); n++);
	va_end(ap);

	if (_NULL(retptr = otc_text_map_new(NULL, n)))
		return retptr;

	va_start(ap, key);
	for (i = 0; (i < n) && _nNULL(key); i++) {
		char *value;

		if (_nNULL(value = (char *)span->baggage_item(span, key))) {
			(void)otc_text_map_add(retptr, key, 0, value, 0, OTC_TEXT_MAP_DUP_KEY);

			OT_DBG(OT, "get baggage[%d]: \"%s\" -> \"%s\"", i, retptr->key[i], retptr->value[i]);
		} else {
			OT_DBG(OT, "get baggage[%d]: \"%s\" -> invalid key", i, key);
		}

		key = va_arg(ap, typeof(key));
	}
	va_end(ap);

	return retptr;
}


/***
 * NAME
 *   ot_span_log_kv -
 *
 * ARGUMENTS
 *   span  -
 *   key   -
 *   value -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int ot_span_log_kv(struct otc_span *span, const char *key, const char *value, ...)
{
	va_list              ap;
	struct otc_log_field log_data[OTC_MAXLOGFIELDS];
	int                  retval = -1;

	OT_FUNC("%p, \"%s\", \"%s\", ...", span, key, value);

	if (_NULL(span) || _NULL(key) || _NULL(value))
		return retval;

	va_start(ap, value);
	for (retval = 0; (retval < TABLESIZE(log_data)) && _nNULL(key); retval++) {
		log_data[retval].key                      = key;
		log_data[retval].value.type               = otc_value_string;
		log_data[retval].value.value.string_value = value;

		if (_nNULL(key = va_arg(ap, typeof(key))))
			value = va_arg(ap, typeof(value));
	}
	va_end(ap);

	span->log_fields(span, log_data, retval);

	return retval;
}


/***
 * NAME
 *   ot_span_log -
 *
 * ARGUMENTS
 *   span   -
 *   key    -
 *   format -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int ot_span_log(struct otc_span *span, const char *key, const char *format, ...)
{
	va_list ap;
	char    value[BUFSIZ];

	OT_FUNC("%p, \"%s\", \"%s\", ...", span, key, format);

	if (_NULL(span) || _NULL(key) || _NULL(format))
		return -1;

	va_start(ap, format);
	(void)vsnprintf(value, sizeof(value), format, ap);
	va_end(ap);

	return ot_span_log_kv(span, key, value, NULL);
}


#ifdef OT_USE_INJECT_CB

/***
 * NAME
 *   ot_text_map_writer_set_cb -
 *
 * ARGUMENTS
 *   writer -
 *   key    -
 *   value  -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
static otc_propagation_error_code_t ot_text_map_writer_set_cb(struct otc_text_map_writer *writer, const char *key, const char *value)
{
	otc_propagation_error_code_t retval = otc_propagation_error_code_success;

	OT_FUNC("%p, \"%s\", \"%s\"", writer, key, value);

	if (otc_text_map_add(&(writer->text_map), key, 0, value, 0, OTC_TEXT_MAP_DUP_KEY | OTC_TEXT_MAP_DUP_VALUE) == -1)
		retval = otc_propagation_error_code_unknown;

	return retval;
}

#endif /* OT_USE_INJECT_CB */


#ifdef OT_USE_EXTRACT_CB

/***
 * NAME
 *   ot_text_map_reader_foreach_key_cb -
 *
 * ARGUMENTS
 *   reader  -
 *   handler -
 *   arg     -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
static otc_propagation_error_code_t ot_text_map_reader_foreach_key_cb(struct otc_text_map_reader *reader, otc_propagation_error_code_t (*handler)(void *arg, const char *key, const char *value), void *arg)
{
	size_t                       i;
	otc_propagation_error_code_t retval = otc_propagation_error_code_success;

	OT_FUNC("%p, %p, %p", reader, handler, arg);

	for (i = 0; (retval == otc_propagation_error_code_success) && (i < reader->text_map.count); i++) {
		OT_DBG(OT, "\"%s\" -> \"%s\"", reader->text_map.key[i], reader->text_map.value[i]);

		retval = handler(arg, reader->text_map.key[i], reader->text_map.value[i]);
	}

	return retval;
}

#endif /* OT_USE_EXTRACT_CB */


/***
 * NAME
 *   ot_inject_text_map -
 *
 * ARGUMENTS
 *   tracer  -
 *   span    -
 *   carrier -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_inject_text_map(struct otc_tracer *tracer, const struct otc_span *span, struct otc_text_map_writer *carrier)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, span, carrier);

	if (_NULL(span))
		return retptr;

	if (_NULL(retptr = span->span_context((struct otc_span *)span)))
		return retptr;

	(void)memset(carrier, 0, sizeof(*carrier));
#ifdef OT_USE_INJECT_CB
	carrier->set = ot_text_map_writer_set_cb;
#endif

	rc = tracer->inject_text_map(tracer, carrier, retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: inject_text_map() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	} else {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	OT_DBG(OT, "carrier %p: { { %p %p %zu/%zu %hhu } %p }", carrier, carrier->text_map.key, carrier->text_map.value, carrier->text_map.count, carrier->text_map.size, carrier->text_map.is_dynamic, carrier->set);
	ot_text_map_show(&(carrier->text_map));

	return retptr;
}


/***
 * NAME
 *   ot_extract_text_map -
 *
 * ARGUMENTS
 *   tracer   -
 *   carrier  -
 *   text_map -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_extract_text_map(struct otc_tracer *tracer, struct otc_text_map_reader *carrier, const struct otc_text_map *text_map)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, carrier, text_map);

	(void)memset(carrier, 0, sizeof(*carrier));
#ifdef OT_USE_EXTRACT_CB
	carrier->foreach_key = ot_text_map_reader_foreach_key_cb;
#endif

	if (_nNULL(text_map))
		(void)memcpy(&(carrier->text_map), text_map, sizeof(carrier->text_map));

	OT_DBG(OT, "carrier %p: { { %p %p %zu/%zu %hhu } }", carrier, carrier->text_map.key, carrier->text_map.value, carrier->text_map.count, carrier->text_map.size, carrier->text_map.is_dynamic);

	rc = tracer->extract_text_map(tracer, carrier, &retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: extract_text_map() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	}
	else if (_nNULL(retptr)) {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	return retptr;
}


#ifdef OT_USE_INJECT_CB

/***
 * NAME
 *   ot_http_headers_writer_set_cb -
 *
 * ARGUMENTS
 *   writer -
 *   key    -
 *   value  -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
static otc_propagation_error_code_t ot_http_headers_writer_set_cb(struct otc_http_headers_writer *writer, const char *key, const char *value)
{
	otc_propagation_error_code_t retval = otc_propagation_error_code_success;

	OT_FUNC("%p, \"%s\", \"%s\"", writer, key, value);

	if (otc_text_map_add(&(writer->text_map), key, 0, value, 0, OTC_TEXT_MAP_DUP_KEY | OTC_TEXT_MAP_DUP_VALUE) == -1)
		retval = otc_propagation_error_code_unknown;

	return retval;
}

#endif /* OT_USE_INJECT_CB */


#ifdef OT_USE_EXTRACT_CB

/***
 * NAME
 *   ot_http_headers_reader_foreach_key_cb -
 *
 * ARGUMENTS
 *   reader  -
 *   handler -
 *   arg     -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
static otc_propagation_error_code_t ot_http_headers_reader_foreach_key_cb(struct otc_http_headers_reader *reader, otc_propagation_error_code_t (*handler)(void *arg, const char *key, const char *value), void *arg)
{
	size_t                       i;
	otc_propagation_error_code_t retval = otc_propagation_error_code_success;

	OT_FUNC("%p, %p, %p", reader, handler, arg);

	for (i = 0; (retval == otc_propagation_error_code_success) && (i < reader->text_map.count); i++) {
		OT_DBG(OT, "\"%s\" -> \"%s\"", reader->text_map.key[i], reader->text_map.value[i]);

		retval = handler(arg, reader->text_map.key[i], reader->text_map.value[i]);
	}

	return retval;
}

#endif /* OT_USE_EXTRACT_CB */


/***
 * NAME
 *   ot_inject_http_headers -
 *
 * ARGUMENTS
 *   tracer  -
 *   span    -
 *   carrier -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_inject_http_headers(struct otc_tracer *tracer, const struct otc_span *span, struct otc_http_headers_writer *carrier)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, span, carrier);

	if (_NULL(span))
		return retptr;

	if (_NULL(retptr = span->span_context((struct otc_span *)span)))
		return retptr;

	(void)memset(carrier, 0, sizeof(*carrier));
#ifdef OT_USE_INJECT_CB
	carrier->set = ot_http_headers_writer_set_cb;
#endif

	rc = tracer->inject_http_headers(tracer, carrier, retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: inject_http_headers() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	} else {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	OT_DBG(OT, "carrier %p: { { %p %p %zu/%zu %hhu } %p }", carrier, carrier->text_map.key, carrier->text_map.value, carrier->text_map.count, carrier->text_map.size, carrier->text_map.is_dynamic, carrier->set);
	ot_text_map_show(&(carrier->text_map));

	return retptr;
}


/***
 * NAME
 *   ot_extract_http_headers -
 *
 * ARGUMENTS
 *   tracer  -
 *   carrier  -
 *   text_map -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_extract_http_headers(struct otc_tracer *tracer, struct otc_http_headers_reader *carrier, const struct otc_text_map *text_map)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, carrier, text_map);

	(void)memset(carrier, 0, sizeof(*carrier));
#ifdef OT_USE_EXTRACT_CB
	carrier->foreach_key = ot_http_headers_reader_foreach_key_cb;
#endif

	if (_nNULL(text_map))
		(void)memcpy(&(carrier->text_map), text_map, sizeof(carrier->text_map));

	OT_DBG(OT, "carrier %p: { { %p %p %zu/%zu %hhu } }", carrier, carrier->text_map.key, carrier->text_map.value, carrier->text_map.count, carrier->text_map.size, carrier->text_map.is_dynamic);

	rc = tracer->extract_http_headers(tracer, carrier, &retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: extract_http_headers() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	}
	else if (_nNULL(retptr)) {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	return retptr;
}


/***
 * NAME
 *   ot_inject_binary -
 *
 * ARGUMENTS
 *   tracer  -
 *   span    -
 *   carrier -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_inject_binary(struct otc_tracer *tracer, const struct otc_span *span, struct otc_custom_carrier_writer *carrier)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, span, carrier);

	if (_NULL(span))
		return retptr;

	if (_NULL(retptr = span->span_context((struct otc_span *)span)))
		return retptr;

	(void)memset(carrier, 0, sizeof(*carrier));

	rc = tracer->inject_binary(tracer, carrier, retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: inject_binary() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	} else {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	OT_DBG(OT, "carrier %p: { { %p %zu %hhu } %p }", carrier, carrier->binary_data.data, carrier->binary_data.size, carrier->binary_data.is_dynamic, carrier->inject);

#ifdef DEBUG
	if (carrier->binary_data.data != NULL) {
		struct otc_jaeger_trace_context *ctx_jaeger = carrier->binary_data.data;
		struct otc_dd_trace_context     *ctx_dd = carrier->binary_data.data;

		OT_DBG(OT, "Jaeger trace context: %016" PRIx64 "%016" PRIx64 ":%016" PRIx64 ":%016" PRIx64 ":%02hhx <%s> <%s>",
		       ctx_jaeger->trace_id[0], ctx_jaeger->trace_id[1], ctx_jaeger->span_id, ctx_jaeger->parent_span_id, ctx_jaeger->flags,
		       str_hex(ctx_jaeger->baggage, carrier->binary_data.size - sizeof(*ctx_jaeger)),
		       str_ctrl(ctx_jaeger->baggage, carrier->binary_data.size - sizeof(*ctx_jaeger)));
		OT_DBG(OT, "DataDog trace context: <%s> <%s>",
		       str_hex(ctx_dd->data, carrier->binary_data.size),
		       str_ctrl(ctx_dd->data, carrier->binary_data.size));
	}
#endif /* DEBUG */

	return retptr;
}


/***
 * NAME
 *   ot_extract_binary -
 *
 * ARGUMENTS
 *   tracer      -
 *   carrier     -
 *   binary_data -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
struct otc_span_context *ot_extract_binary(struct otc_tracer *tracer, struct otc_custom_carrier_reader *carrier, const struct otc_binary_data *binary_data)
{
	struct otc_span_context *retptr = NULL;
	int                      rc;

	OT_FUNC("%p, %p, %p", tracer, carrier, binary_data);

	(void)memset(carrier, 0, sizeof(*carrier));

	if (_nNULL(binary_data) && _nNULL(binary_data->data) && (binary_data->size > 0))
		(void)memcpy(&(carrier->binary_data), binary_data, sizeof(carrier->binary_data));

	OT_DBG(OT, "carrier %p: { { %p %zu %hhu } %p }", carrier, carrier->binary_data.data, carrier->binary_data.size, carrier->binary_data.is_dynamic, carrier->extract);

	rc = tracer->extract_binary(tracer, carrier, &retptr);
	if (rc != otc_propagation_error_code_success) {
		OT_LOG("  ERROR: extract_binary() failed: %d", rc);

		OTC_DBG_FREE(retptr);
	}
	else if (_nNULL(retptr)) {
		OT_DBG(OT, "context %p: { %" PRId64 " %p %p }", retptr, retptr->idx, retptr->span, retptr->destroy);
	}

	return retptr;
}


/***
 * NAME
 *   ot_span_finish -
 *
 * ARGUMENTS
 *   span      -
 *   ts_finish -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   This function does not return a value.
 */
void ot_span_finish(struct otc_span **span, const struct timespec *ts_finish)
{
	struct otc_finish_span_options options;

	OT_FUNC("%p, %p", span, ts_finish);

	if (_NULL(span) || _NULL(*span))
		return;

	(void)memset(&options, 0, sizeof(options));

	if (_nNULL(ts_finish))
		(void)memcpy(&(options.finish_time.value), ts_finish, sizeof(options.finish_time.value));

	OT_DBG(OT, "span %p:%zu finished", *span, (*span)->idx);

	(*span)->finish_with_options(*span, &options);

	*span = NULL;
}

/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 * End:
 *
 * vi: noexpandtab shiftwidth=8 tabstop=8
 */