summaryrefslogtreecommitdiffstats
path: root/addons/ot/src/util.c
blob: fd040164d55f2ad71d3caf33b42c8dd23d71f3e0 (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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/***
 * Copyright 2020 HAProxy Technologies
 *
 * This file is part of the HAProxy OpenTracing filter.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "include.h"


#ifdef DEBUG_OT

/***
 * NAME
 *   flt_ot_args_dump -
 *
 * ARGUMENTS
 *   args -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   This function does not return a value.
 */
void flt_ot_args_dump(char **args)
{
	int i, argc;

	argc = flt_ot_args_count(args);

	(void)fprintf(stderr, FLT_OT_DBG_FMT("%.*sargs[%d]: { '%s' "), dbg_indent_level, FLT_OT_DBG_INDENT, argc, args[0]);

	for (i = 1; i < argc; i++)
		(void)fprintf(stderr, "'%s' ", args[i]);

	(void)fprintf(stderr, "}\n");
}


/***
 * NAME
 *   flt_ot_filters_dump -
 *
 * ARGUMENTS
 *   This function takes no arguments.
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   This function does not return a value.
 */
void flt_ot_filters_dump(void)
{
	struct flt_conf *fconf;
	struct proxy    *px;

	FLT_OT_FUNC("");

	for (px = proxies_list; px != NULL; px = px->next) {
		FLT_OT_DBG(2, "proxy '%s'", px->id);

		list_for_each_entry(fconf, &(px->filter_configs), list)
			if (fconf->id == ot_flt_id) {
				struct flt_ot_conf *conf = fconf->conf;

				FLT_OT_DBG(2, "  OT filter '%s'", conf->id);
			}
	}

	FLT_OT_RETURN();
}


/***
 * NAME
 *   flt_ot_chn_label -
 *
 * ARGUMENTS
 *   chn -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_chn_label(const struct channel *chn)
{
	return (chn->flags & CF_ISRESP) ? "RESponse" : "REQuest";
}


/***
 * NAME
 *   flt_ot_pr_mode -
 *
 * ARGUMENTS
 *   s -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_pr_mode(const struct stream *s)
{
	struct proxy *px = (s->flags & SF_BE_ASSIGNED) ? s->be : strm_fe(s);

	return (px->mode == PR_MODE_HTTP) ? "HTTP" : "TCP";
}


/***
 * NAME
 *   flt_ot_stream_pos -
 *
 * ARGUMENTS
 *   s -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_stream_pos(const struct stream *s)
{
	return (s->flags & SF_BE_ASSIGNED) ? "backend" : "frontend";
}


/***
 * NAME
 *   flt_ot_type -
 *
 * ARGUMENTS
 *   f -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_type(const struct filter *f)
{
	return (f->flags & FLT_FL_IS_BACKEND_FILTER) ? "backend" : "frontend";
}


/***
 * NAME
 *   flt_ot_analyzer -
 *
 * ARGUMENTS
 *   an_bit -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_analyzer(uint an_bit)
{
#define FLT_OT_AN_DEF(a)   { a, #a },
	static const struct {
		uint        an_bit;
		const char *str;
	} flt_ot_an[] = { FLT_OT_AN_DEFINES };
#undef FLT_OT_AN_DEF
	const char *retptr = "invalid an_bit";
	int         i;

	for (i = 0; i < FLT_OT_TABLESIZE(flt_ot_an); i++)
		if (flt_ot_an[i].an_bit == an_bit) {
			retptr = flt_ot_an[i].str;

			break;
		}

	return retptr;
}


/***
 * NAME
 *   flt_ot_str_hex -
 *
 * ARGUMENTS
 *   data -
 *   size -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_str_hex(const void *data, size_t size)
{
	static THREAD_LOCAL char  retbuf[BUFSIZ];
	const uint8_t            *ptr = data;
	size_t                    i;

	if (data == NULL)
		return "(null)";
	else if (size == 0)
		return "()";

	for (i = 0, size <<= 1; (i < (sizeof(retbuf) - 2)) && (i < size); ptr++) {
		retbuf[i++] = FLT_OT_NIBBLE_TO_HEX(*ptr >> 4);
		retbuf[i++] = FLT_OT_NIBBLE_TO_HEX(*ptr & 0x0f);
	}

	retbuf[i] = '\0';

	return retbuf;
}


/***
 * NAME
 *   flt_ot_str_ctrl -
 *
 * ARGUMENTS
 *   data -
 *   size -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_str_ctrl(const void *data, size_t size)
{
	static THREAD_LOCAL char  retbuf[BUFSIZ];
	const uint8_t            *ptr = data;
	size_t                    i, n = 0;

	if (data == NULL)
		return "(null)";
	else if (size == 0)
		return "()";

	for (i = 0; (n < (sizeof(retbuf) - 1)) && (i < size); i++)
		retbuf[n++] = ((ptr[i] >= 0x20) && (ptr[i] <= 0x7e)) ? ptr[i] : '.';

	retbuf[n] = '\0';

	return retbuf;
}


/***
 * NAME
 *   flt_ot_list_debug -
 *
 * ARGUMENTS
 *   head -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
const char *flt_ot_list_debug(const struct list *head)
{
	FLT_OT_BUFFER_THR(retbuf, 4, 64, retptr);

	if ((head == NULL) || LIST_ISEMPTY(head)) {
		(void)strncpy(retptr, (head == NULL) ? "{ null list }" : "{ empty list }", sizeof(retbuf[0]));
	}
	else if (head->p == head->n) {
		(void)snprintf(retptr, sizeof(retbuf[0]), "{ %p * 1 }", head->p);
	}
	else {
		const struct list *ptr;
		size_t             count = 0;

		for (ptr = head->n; ptr != head; ptr = ptr->n, count++);

		(void)snprintf(retptr, sizeof(retbuf[0]), "{ %p %p %zu }", head->p, head->n, count);
	}

	return (retptr);
}

#endif /* DEBUG_OT */


/***
 * NAME
 *   flt_ot_chunk_add -
 *
 * ARGUMENTS
 *   chk -
 *   src -
 *   n   -
 *   err -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
ssize_t flt_ot_chunk_add(struct buffer *chk, const void *src, size_t n, char **err)
{
	FLT_OT_FUNC("%p, %p, %zu, %p:%p", chk, src, n, FLT_OT_DPTR_ARGS(err));

	if ((chk == NULL) || (src == NULL))
		FLT_OT_RETURN_EX(-1, ssize_t, "%ld");

	if (chk->area == NULL)
		chunk_init(chk, FLT_OT_CALLOC(1, global.tune.bufsize), global.tune.bufsize);

	if (chk->area == NULL) {
		FLT_OT_ERR("out of memory");

		FLT_OT_RETURN_EX(-1, ssize_t, "%ld");
	}
	else if (n > (chk->size - chk->data)) {
		FLT_OT_ERR("chunk size too small");

		FLT_OT_RETURN_EX(-1, ssize_t, "%ld");
	}

	(void)memcpy(chk->area + chk->data, src, n);
	chk->data += n;

	FLT_OT_RETURN_EX(chk->data, ssize_t, "%ld");
}


/***
 * NAME
 *   flt_ot_args_count -
 *
 * ARGUMENTS
 *   args -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int flt_ot_args_count(char **args)
{
	int i, retval = 0;

	if (args == NULL)
		return retval;

	/*
	 * It is possible that some arguments within the configuration line
	 * are not specified; that is, they are set to a blank string.
	 *
	 * For example:
	 *   keyword '' arg_2
	 *
	 * In that case the content of the args field will be like this:
	 *   args[0]:                  'keyword'
	 *   args[1]:                  NULL pointer
	 *   args[2]:                  'arg_2'
	 *   args[3 .. MAX_LINE_ARGS): NULL pointers
	 *
	 * The total number of arguments is the index of the last argument
	 * (increased by 1) that is not a NULL pointer.
	 */
	for (i = 0; i < MAX_LINE_ARGS; i++)
		if (FLT_OT_ARG_ISVALID(i))
			retval = i + 1;

	return retval;
}


/***
 * NAME
 *   flt_ot_args_to_str -
 *
 * ARGUMENTS
 *   args -
 *   idx  -
 *   str  -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   This function does not return a value.
 */
void flt_ot_args_to_str(char **args, int idx, char **str)
{
	int i, argc;

	if ((args == NULL) || (*args == NULL))
		return;

	argc = flt_ot_args_count(args);

	for (i = idx; i < argc; i++)
		(void)memprintf(str, "%s%s%s", (*str == NULL) ? "" : *str, (i == idx) ? "" : " ", (args[i] == NULL) ? "" : args[i]);
}


/***
 * NAME
 *   flt_ot_strtod -
 *
 * ARGUMENTS
 *   nptr      -
 *   limit_min -
 *   limit_max -
 *   err       -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
double flt_ot_strtod(const char *nptr, double limit_min, double limit_max, char **err)
{
	char   *endptr = NULL;
	double  retval;

	errno = 0;

	retval = strtod(nptr, &endptr);
	if ((errno != 0) || FLT_OT_STR_ISVALID(endptr))
		FLT_OT_ERR("'%s' : invalid value", nptr);
	else if (!FLT_OT_IN_RANGE(retval, limit_min, limit_max))
		FLT_OT_ERR("'%s' : value out of range [%.2f, %.2f]", nptr, limit_min, limit_max);

	return retval;
}


/***
 * NAME
 *   flt_ot_strtoll -
 *
 * ARGUMENTS
 *   nptr      -
 *   limit_min -
 *   limit_max -
 *   err       -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int64_t flt_ot_strtoll(const char *nptr, int64_t limit_min, int64_t limit_max, char **err)
{
	char    *endptr = NULL;
	int64_t  retval;

	errno = 0;

	retval = strtoll(nptr, &endptr, 0);
	if ((errno != 0) || FLT_OT_STR_ISVALID(endptr))
		FLT_OT_ERR("'%s' : invalid value", nptr);
	else if (!FLT_OT_IN_RANGE(retval, limit_min, limit_max))
		FLT_OT_ERR("'%s' : value out of range [%" PRId64 ", %" PRId64 "]", nptr, limit_min, limit_max);

	return retval;
}


/***
 * NAME
 *   flt_ot_sample_to_str -
 *
 * ARGUMENTS
 *   data  -
 *   value -
 *   size  -
 *   err   -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int flt_ot_sample_to_str(const struct sample_data *data, char *value, size_t size, char **err)
{
	int retval = -1;

	FLT_OT_FUNC("%p, %p, %zu, %p:%p", data, value, size, FLT_OT_DPTR_ARGS(err));

	if ((data == NULL) || (value == NULL) || (size == 0))
		FLT_OT_RETURN_INT(retval);

	*value = '\0';

	if (data->type == SMP_T_ANY) {
		FLT_OT_ERR("invalid sample data type %d", data->type);
	}
	else if (data->type == SMP_T_BOOL) {
		value[0] = data->u.sint ? '1' : '0';
		value[1] = '\0';

		retval = 1;
	}
	else if (data->type == SMP_T_SINT) {
		retval = snprintf(value, size, "%lld", data->u.sint);
	}
	else if (data->type == SMP_T_ADDR) {
		/* This type is never used to qualify a sample. */
	}
	else if (data->type == SMP_T_IPV4) {
		if (INET_ADDRSTRLEN > size)
			FLT_OT_ERR("sample data size too large");
		else if (inet_ntop(AF_INET, &(data->u.ipv4), value, INET_ADDRSTRLEN) == NULL)
			FLT_OT_ERR("invalid IPv4 address");
		else
			retval = strlen(value);
	}
	else if (data->type == SMP_T_IPV6) {
		if (INET6_ADDRSTRLEN > size)
			FLT_OT_ERR("sample data size too large");
		else if (inet_ntop(AF_INET6, &(data->u.ipv6), value, INET6_ADDRSTRLEN) == NULL)
			FLT_OT_ERR("invalid IPv6 address");
		else
			retval = strlen(value);
	}
	else if (data->type == SMP_T_STR) {
		if (data->u.str.data >= size) {
			FLT_OT_ERR("sample data size too large");
		}
		else if (data->u.str.data > 0) {
			retval = data->u.str.data;
			memcpy(value, data->u.str.area, retval);
			value[retval] = '\0';
		}
		else {
			/*
			 * There is no content to add but we will still return
			 * the correct status.
			 */
			retval = 0;
		}
	}
	else if (data->type == SMP_T_BIN) {
		FLT_OT_ERR("invalid sample data type %d", data->type);
	}
	else if (data->type != SMP_T_METH) {
		FLT_OT_ERR("invalid sample data type %d", data->type);
	}
	else if (data->u.meth.meth == HTTP_METH_OPTIONS) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_OPTIONS);

		(void)memcpy(value, HTTP_METH_STR_OPTIONS, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_GET) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_GET);

		(void)memcpy(value, HTTP_METH_STR_GET, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_HEAD) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_HEAD);

		(void)memcpy(value, HTTP_METH_STR_HEAD, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_POST) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_POST);

		(void)memcpy(value, HTTP_METH_STR_POST, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_PUT) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_PUT);

		(void)memcpy(value, HTTP_METH_STR_PUT, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_DELETE) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_DELETE);

		(void)memcpy(value, HTTP_METH_STR_DELETE, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_TRACE) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_TRACE);

		(void)memcpy(value, HTTP_METH_STR_TRACE, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_CONNECT) {
		retval = FLT_OT_STR_SIZE(HTTP_METH_STR_CONNECT);

		(void)memcpy(value, HTTP_METH_STR_CONNECT, retval + 1);
	}
	else if (data->u.meth.meth == HTTP_METH_OTHER) {
		if (data->u.meth.str.data >= size) {
			FLT_OT_ERR("sample data size too large");
		} else {
			retval = data->u.meth.str.data;
			memcpy(value, data->u.meth.str.area, retval);
			value[retval] = '\0';
		}
	}
	else {
		FLT_OT_ERR("invalid HTTP method");
	}

	FLT_OT_RETURN_INT(retval);
}


/***
 * NAME
 *   flt_ot_sample_to_value -
 *
 * ARGUMENTS
 *   key   -
 *   data  -
 *   value -
 *   err   -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   -
 */
int flt_ot_sample_to_value(const char *key, const struct sample_data *data, struct otc_value *value, char **err)
{
	int retval = -1;

	FLT_OT_FUNC("\"%s\", %p, %p, %p:%p", key, data, value, FLT_OT_DPTR_ARGS(err));

	if ((data == NULL) || (value == NULL))
		FLT_OT_RETURN_INT(retval);

	if (data->type == SMP_T_BOOL) {
		value->type             = otc_value_bool;
		value->value.bool_value = data->u.sint ? 1 : 0;

		retval = sizeof(value->value.bool_value);
	}
	else if (data->type == SMP_T_SINT) {
		value->type              = otc_value_int64;
		value->value.int64_value = data->u.sint;

		retval = sizeof(value->value.int64_value);
	}
	else {
		value->type               = otc_value_string;
		value->value.string_value = FLT_OT_MALLOC(global.tune.bufsize);

		if (value->value.string_value == NULL)
			FLT_OT_ERR("out of memory");
		else
			retval = flt_ot_sample_to_str(data, (char *)value->value.string_value, global.tune.bufsize, err);
	}

	FLT_OT_RETURN_INT(retval);
}


/***
 * NAME
 *   flt_ot_sample_add -
 *
 * ARGUMENTS
 *   s      -
 *   dir    -
 *   sample -
 *   data   -
 *   type   -
 *   err    -
 *
 * DESCRIPTION
 *   -
 *
 * RETURN VALUE
 *   Returns a negative value if an error occurs, 0 if it needs to wait,
 *   any other value otherwise.
 */
int flt_ot_sample_add(struct stream *s, uint dir, struct flt_ot_conf_sample *sample, struct flt_ot_scope_data *data, int type, char **err)
{
	const struct flt_ot_conf_sample_expr *expr;
	struct sample                         smp;
	struct otc_value                      value;
	struct buffer                         buffer;
	int                                   idx = 0, rc, retval = FLT_OT_RET_OK;

	FLT_OT_FUNC("%p, %u, %p, %p, %d, %p:%p", s, dir, data, sample, type, FLT_OT_DPTR_ARGS(err));

	FLT_OT_DBG_CONF_SAMPLE("sample ", sample);

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

	list_for_each_entry(expr, &(sample->exprs), list) {
		FLT_OT_DBG_CONF_SAMPLE_EXPR("sample expression ", expr);

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

		/*
		 * If we have only one expression to process, then the data
		 * type that is the result of the expression is converted to
		 * an equivalent data type (if possible) that is written to
		 * the tracer.
		 *
		 * If conversion is not possible, or if we have multiple
		 * expressions to process, then the result is converted to
		 * a string and as such sent to the tracer.
		 */
		if (sample_process(s->be, s->sess, s, dir | SMP_OPT_FINAL, expr->expr, &smp) != NULL) {
			FLT_OT_DBG(3, "data type %d: '%s'", smp.data.type, expr->value);
		} else {
			FLT_OT_DBG(2, "WARNING: failed to fetch '%s' value", expr->value);

			/*
			 * In case the fetch failed, we will set the result
			 * (sample) to an empty static string.
			 */
			(void)memset(&(smp.data), 0, sizeof(smp.data));
			smp.data.type       = SMP_T_STR;
			smp.data.u.str.area = "";
		}

		if ((sample->num_exprs == 1) && (type == FLT_OT_EVENT_SAMPLE_TAG)) {
			if (flt_ot_sample_to_value(sample->key, &(smp.data), &value, err) == -1)
				retval = FLT_OT_RET_ERROR;
		} else {
			if (buffer.area == NULL) {
				chunk_init(&buffer, FLT_OT_CALLOC(1, global.tune.bufsize), global.tune.bufsize);
				if (buffer.area == NULL) {
					FLT_OT_ERR("out of memory");

					retval = FLT_OT_RET_ERROR;

					break;
				}
			}

			rc = flt_ot_sample_to_str(&(smp.data), buffer.area + buffer.data, buffer.size - buffer.data, err);
			if (rc == -1) {
				retval = FLT_OT_RET_ERROR;
			} else {
				buffer.data += rc;

				if (sample->num_exprs == ++idx) {
					value.type               = otc_value_string;
					value.value.string_value = buffer.area;
				}
			}
		}
	}

	if (retval == FLT_OT_RET_ERROR) {
		/* Do nothing. */
	}
	else if (type == FLT_OT_EVENT_SAMPLE_TAG) {
		struct otc_tag *tag = data->tags + data->num_tags++;

		tag->key = sample->key;
		(void)memcpy(&(tag->value), &value, sizeof(tag->value));
	}
	else if (type == FLT_OT_EVENT_SAMPLE_LOG) {
		struct otc_log_field *log_field = data->log_fields + data->num_log_fields++;

		log_field->key = sample->key;
		(void)memcpy(&(log_field->value), &value, sizeof(log_field->value));
	}
	else {
		if (data->baggage == NULL)
			data->baggage = otc_text_map_new(NULL, FLT_OT_MAXBAGGAGES);

		if (data->baggage == NULL) {
			FLT_OT_ERR("out of memory");

			retval = FLT_OT_RET_ERROR;
		}
		else if (otc_text_map_add(data->baggage, sample->key, 0, value.value.string_value, 0, 0) == -1) {
			FLT_OT_ERR("out of memory");

			retval = FLT_OT_RET_ERROR;
		}
		else
			FLT_OT_DBG(3, "baggage[%zu]: '%s' -> '%s'", data->baggage->count - 1, data->baggage->key[data->baggage->count - 1], data->baggage->value[data->baggage->count - 1]);
	}

	FLT_OT_RETURN_INT(retval);
}

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