summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/flb_chunk_trace.c
blob: adacb73f21a58c19140087f0a633b3038cad04d1 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit 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 <fcntl.h>

#include <msgpack.h>
#include <chunkio/chunkio.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input_chunk.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_chunk_trace.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_base64.h>
#include <fluent-bit/flb_storage.h>
#include <fluent-bit/flb_router.h>
#include <fluent-bit/flb_kv.h>


/* Register external function to emit records, check 'plugins/in_emitter' */
int in_emitter_add_record(const char *tag, int tag_len,
                          const char *buf_data, size_t buf_size,
                          struct flb_input_instance *in);

/****************************************************************************/
/* To avoid double frees when enabling and disabling tracing as well        */
/* as avoiding race conditions when stopping fluent-bit while someone is    */
/* toggling tracing via the HTTP API this set of APIS with a mutex lock     */
/* is used:                                                                 */
/*   * flb_chunk_trace_to_be_destroyed - query to see if the trace context  */
/*     is slated to be freed                                                */
/*   * flb_chunk_trace_set_destroy - set the trace context to be destroyed  */
/*     once all chunks are freed (executed in flb_chunk_trace_destroy).     */
/*   * flb_chunk_trace_has_chunks - see if there are still chunks using     */
/*     using the tracing context                                            */
/*   * flb_chunk_trace_add - increment the traces chunk count               */
/*   * flb_chunk_trace_sub - decrement the traces chunk count               */
/****************************************************************************/
static inline int flb_chunk_trace_to_be_destroyed(struct flb_chunk_trace_context *ctxt)
{
    int ret = FLB_FALSE;

    ret = (ctxt->to_destroy == 1 ? FLB_TRUE : FLB_FALSE);
    return ret;
}

static inline int flb_chunk_trace_has_chunks(struct flb_chunk_trace_context *ctxt)
{
    int ret = FLB_FALSE;

    ret = ((ctxt->chunks > 0) ? FLB_TRUE : FLB_FALSE);
    return ret;
}

static inline void flb_chunk_trace_add(struct flb_chunk_trace_context *ctxt)
{
    ctxt->chunks++;
}

static inline void flb_chunk_trace_sub(struct flb_chunk_trace_context *ctxt)
{
    ctxt->chunks--;
}

static inline void flb_chunk_trace_set_destroy(struct flb_chunk_trace_context *ctxt)
{
    ctxt->to_destroy = 1;
}

static struct flb_output_instance *find_calyptia_output_instance(struct flb_config *config)
{
    struct mk_list *head = NULL;
    struct flb_output_instance *output = NULL;

    mk_list_foreach(head, &config->outputs) {
        output = mk_list_entry(head, struct flb_output_instance, _head);
        if (strcmp(output->p->name, "calyptia") == 0) {
            return output;
        }
    }
    return NULL;
}

static void trace_chunk_context_destroy(struct flb_chunk_trace_context *ctxt)
{
    int i;


    if (flb_chunk_trace_has_chunks(ctxt) == FLB_TRUE) {
        flb_chunk_trace_set_destroy(ctxt);
        flb_input_pause_all(ctxt->flb->config);
        return;
    }
    
    /* pause all inputs, then destroy the input storage. */
    flb_input_pause_all(ctxt->flb->config);
    /* waiting for all tasks to end is key to safely stopping and destroying */
    /* the fluent-bit pipeline. */
    for (i = 0; i < 5 && flb_task_running_count(ctxt->flb->config) > 0; i++) {
        usleep(10 * 1000);
    }

    flb_sds_destroy(ctxt->trace_prefix);
    flb_stop(ctxt->flb);
    flb_destroy(ctxt->flb);
    flb_free(ctxt);
}

void flb_chunk_trace_context_destroy(void *input)
{
    struct flb_input_instance *in = (struct flb_input_instance *)input;
    pthread_mutex_lock(&in->chunk_trace_lock);
    if (in->chunk_trace_ctxt != NULL) {
        trace_chunk_context_destroy(in->chunk_trace_ctxt);
        in->chunk_trace_ctxt = NULL;
    }
    pthread_mutex_unlock(&in->chunk_trace_lock);
}

struct flb_chunk_trace_context *flb_chunk_trace_context_new(void *trace_input,
                                                            const char *output_name,
                                                            const char *trace_prefix,
                                                            void *data, struct mk_list *props)
{
    struct flb_input_instance *in = (struct flb_input_instance *)trace_input;
    struct flb_config *config = in->config;
    struct flb_input_instance *input = NULL;
    struct flb_output_instance *output = NULL;
    struct flb_output_instance *calyptia = NULL;
    struct flb_chunk_trace_context *ctx = NULL;
    struct mk_list *head = NULL;
    struct flb_kv *prop = NULL;
    int ret;

    if (config->enable_chunk_trace == FLB_FALSE) {
        flb_warn("[chunk trace] enable chunk tracing via the configuration or "
                 " command line to be able to activate tracing.");
        return NULL;
    }

    pthread_mutex_lock(&in->chunk_trace_lock);

    if (in->chunk_trace_ctxt) {
        trace_chunk_context_destroy(in->chunk_trace_ctxt);
    }

    ctx = flb_calloc(1, sizeof(struct flb_chunk_trace_context));
    if (ctx == NULL) {
        flb_errno();
        pthread_mutex_unlock(&in->chunk_trace_lock);
        return NULL;
    }

    ctx->flb = flb_create();
    if (ctx->flb == NULL) {
        flb_errno();
        goto error_ctxt;
    }

    flb_service_set(ctx->flb, "flush", "1", "grace", "1", NULL);

    input = (void *)flb_input_new(ctx->flb->config, "emitter", NULL, FLB_FALSE);
    if (input == NULL) {
        flb_error("could not load trace emitter");
        goto error_flb;
    }

    ret = flb_input_set_property(input, "alias", "trace-emitter");
    if (ret != 0) {
        flb_error("unable to set alias for trace emitter");
        goto error_input;
    }

    ret = flb_input_set_property(input, "ring_buffer_size", "4096");
    if (ret != 0) {
        flb_error("unable to set ring buffer size for trace emitter");
        goto error_input;
    }

    output = flb_output_new(ctx->flb->config, output_name, data, 1);
    if (output == NULL) {
        flb_error("could not create trace output");
        goto error_input;
    }
    
    /* special handling for the calyptia plugin so we can copy the API */
    /* key and other configuration properties. */
    if (strcmp(output_name, "calyptia") == 0) {
        calyptia = find_calyptia_output_instance(config);
        if (calyptia == NULL) {
            flb_error("unable to find calyptia output instance");
            goto error_output;
        }
        mk_list_foreach(head, &calyptia->properties) {
            prop = mk_list_entry(head, struct flb_kv, _head);
            flb_output_set_property(output, prop->key, prop->val);
        }        
    }
    else if (props != NULL) {
        mk_list_foreach(head, props) {
            prop = mk_list_entry(head, struct flb_kv, _head);
            flb_output_set_property(output, prop->key, prop->val);
        }
    }

    ret = flb_router_connect_direct(input, output);
    if (ret != 0) {
        flb_error("unable to route traces");
        goto error_output;
    }

    ctx->output = (void *)output;
    ctx->input = (void *)input;
    ctx->trace_prefix = flb_sds_create(trace_prefix);

    flb_start_trace(ctx->flb);

    in->chunk_trace_ctxt = ctx;
    pthread_mutex_unlock(&in->chunk_trace_lock);
    return ctx;

error_output:
    flb_output_instance_destroy(output);
error_input:
    if (ctx->cio) {
        cio_destroy(ctx->cio);
    }
    flb_input_instance_destroy(input);
error_flb:
    flb_destroy(ctx->flb);
error_ctxt:
    flb_free(ctx);
    pthread_mutex_unlock(&in->chunk_trace_lock);
    return NULL;
}

struct flb_chunk_trace *flb_chunk_trace_new(struct flb_input_chunk *chunk)
{
    struct flb_chunk_trace *trace = NULL;
    struct flb_input_instance *f_ins = (struct flb_input_instance *)chunk->in;

    pthread_mutex_lock(&f_ins->chunk_trace_lock);

    if (flb_chunk_trace_to_be_destroyed(f_ins->chunk_trace_ctxt) == FLB_TRUE) {
        pthread_mutex_unlock(&f_ins->chunk_trace_lock);
        return NULL;
    }

    trace = flb_calloc(1, sizeof(struct flb_chunk_trace));
    if (trace == NULL) {
        flb_errno();
        pthread_mutex_unlock(&f_ins->chunk_trace_lock);
        return NULL;
    }

    trace->ctxt = f_ins->chunk_trace_ctxt;
    flb_chunk_trace_add(trace->ctxt);

    trace->trace_id = flb_sds_create("");
    if (flb_sds_printf(&trace->trace_id, "%s%d", trace->ctxt->trace_prefix,
                  trace->ctxt->trace_count++) == NULL) {
        pthread_mutex_unlock(&f_ins->chunk_trace_lock);
        flb_sds_destroy(trace->trace_id);
        flb_free(trace);
        return NULL;
    }

    trace->ic = chunk;

    pthread_mutex_unlock(&f_ins->chunk_trace_lock);
    return trace;
}

void flb_chunk_trace_destroy(struct flb_chunk_trace *trace)
{
    pthread_mutex_lock(&trace->ic->in->chunk_trace_lock);
    flb_chunk_trace_sub(trace->ctxt);

    /* check to see if we need to free the trace context. */
    if (flb_chunk_trace_has_chunks(trace->ctxt) == FLB_FALSE &&
        flb_chunk_trace_to_be_destroyed(trace->ctxt) == FLB_TRUE) {
        trace_chunk_context_destroy(trace->ctxt);
    }
    else if (flb_chunk_trace_has_chunks(trace->ctxt) == FLB_TRUE &&
        flb_chunk_trace_to_be_destroyed(trace->ctxt) == FLB_TRUE) {
    }
    pthread_mutex_unlock(&trace->ic->in->chunk_trace_lock);

    flb_sds_destroy(trace->trace_id);
    flb_free(trace);
}

int flb_chunk_trace_context_set_limit(void *input, int limit_type, int limit_arg)
{
    struct flb_input_instance *in = (struct flb_input_instance *)input;
    struct flb_chunk_trace_context *ctxt = NULL;
    struct flb_time tm;

    pthread_mutex_lock(&in->chunk_trace_lock);

    ctxt = in->chunk_trace_ctxt;
    if (ctxt == NULL) {
        pthread_mutex_unlock(&in->chunk_trace_lock);
        return -1;
    }

    switch(limit_type) {
    case FLB_CHUNK_TRACE_LIMIT_TIME:
        flb_time_get(&tm);
        ctxt->limit.type = FLB_CHUNK_TRACE_LIMIT_TIME;
        ctxt->limit.seconds_started = tm.tm.tv_sec;
        ctxt->limit.seconds = limit_arg;
        
        pthread_mutex_unlock(&in->chunk_trace_lock);
        return 0;
    case FLB_CHUNK_TRACE_LIMIT_COUNT:
        ctxt->limit.type = FLB_CHUNK_TRACE_LIMIT_COUNT;
        ctxt->limit.count = limit_arg;

        pthread_mutex_unlock(&in->chunk_trace_lock);
        return 0;
    }

    pthread_mutex_unlock(&in->chunk_trace_lock);
    return -1;
}

int flb_chunk_trace_context_hit_limit(void *input)
{
    struct flb_input_instance *in = (struct flb_input_instance *)input;
    struct flb_time tm;
    struct flb_chunk_trace_context *ctxt = NULL;

    pthread_mutex_lock(&in->chunk_trace_lock);

    ctxt = in->chunk_trace_ctxt;
    if (ctxt == NULL) {
        pthread_mutex_unlock(&in->chunk_trace_lock);
        return FLB_FALSE;
    }

    switch(ctxt->limit.type) {
    case FLB_CHUNK_TRACE_LIMIT_TIME:
        flb_time_get(&tm);
        if ((tm.tm.tv_sec - ctxt->limit.seconds_started) > ctxt->limit.seconds) {
            pthread_mutex_unlock(&in->chunk_trace_lock);
            return FLB_TRUE;
        }
        return FLB_FALSE;
    case FLB_CHUNK_TRACE_LIMIT_COUNT:
        if (ctxt->limit.count <= ctxt->trace_count) {
            pthread_mutex_unlock(&in->chunk_trace_lock);
            return FLB_TRUE;
        }
        pthread_mutex_unlock(&in->chunk_trace_lock);
        return FLB_FALSE;
    }
    pthread_mutex_unlock(&in->chunk_trace_lock);
    return FLB_FALSE;
}

void flb_chunk_trace_do_input(struct flb_input_chunk *ic)
{
    pthread_mutex_lock(&ic->in->chunk_trace_lock);
    if (ic->in->chunk_trace_ctxt == NULL) {
        pthread_mutex_unlock(&ic->in->chunk_trace_lock);
    	return;
    }
    pthread_mutex_unlock(&ic->in->chunk_trace_lock);
    
    if (ic->trace == NULL) {
        ic->trace = flb_chunk_trace_new(ic);
    }

    if (ic->trace) {
        flb_chunk_trace_input(ic->trace);
        if (flb_chunk_trace_context_hit_limit(ic->in) == FLB_TRUE) {
            flb_chunk_trace_context_destroy(ic->in);
        }
    }
}

int flb_chunk_trace_input(struct flb_chunk_trace *trace)
{
    msgpack_packer mp_pck;
    msgpack_sbuffer mp_sbuf;
    msgpack_unpacked result;
    msgpack_object *record = NULL;
    char *buf = NULL;
    size_t buf_size;
    struct flb_time tm;
    struct flb_time tm_end;
    struct flb_input_instance *input = (struct flb_input_instance *)trace->ic->in;
    int rc = -1;
    size_t off = 0;
    flb_sds_t tag = flb_sds_create("trace");
    int records = 0;


    /* initiailize start time */
    flb_time_get(&tm);
    flb_time_get(&tm_end);

    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
    msgpack_unpacked_init(&result);

    cio_chunk_get_content(trace->ic->chunk, &buf, &buf_size);
    
    msgpack_pack_array(&mp_pck, 2);
    flb_pack_time_now(&mp_pck);
    if (input->alias != NULL) {
        msgpack_pack_map(&mp_pck, 7);
    } 
    else {
        msgpack_pack_map(&mp_pck, 6);
    }

    msgpack_pack_str_with_body(&mp_pck, "type", 4);
    msgpack_pack_int(&mp_pck, FLB_CHUNK_TRACE_TYPE_INPUT);

    msgpack_pack_str_with_body(&mp_pck, "trace_id", strlen("trace_id"));
    msgpack_pack_str_with_body(&mp_pck, trace->trace_id, strlen(trace->trace_id));

    msgpack_pack_str_with_body(&mp_pck, "plugin_instance", strlen("plugin_instance"));
    msgpack_pack_str_with_body(&mp_pck, input->name, strlen(input->name));

    if (input->alias != NULL) {
        msgpack_pack_str_with_body(&mp_pck, "plugin_alias", strlen("plugin_alias"));
        msgpack_pack_str_with_body(&mp_pck, input->alias, strlen(input->alias));
    }

    msgpack_pack_str_with_body(&mp_pck, "records", strlen("records"));

    if (buf_size > 0) {
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto sbuffer_error;
            }
            records++;
        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);

        msgpack_pack_array(&mp_pck, records);

        off = 0;
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto sbuffer_error;
            }
            flb_time_pop_from_msgpack(&tm, &result, &record);

            msgpack_pack_map(&mp_pck, 2);
            msgpack_pack_str_with_body(&mp_pck, "timestamp", strlen("timestamp"));
            flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_INT);
            msgpack_pack_str_with_body(&mp_pck, "record", strlen("record"));
            msgpack_pack_object(&mp_pck, *record);

        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);
    }

    msgpack_pack_str_with_body(&mp_pck, "start_time", strlen("start_time"));
    flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_INT);
    msgpack_pack_str_with_body(&mp_pck, "end_time", strlen("end_time"));
    flb_time_append_to_msgpack(&tm_end, &mp_pck, FLB_TIME_ETFMT_INT);
    in_emitter_add_record(tag, flb_sds_len(tag), mp_sbuf.data, mp_sbuf.size,
                          trace->ctxt->input);
sbuffer_error:
    flb_sds_destroy(tag);
    msgpack_unpacked_destroy(&result);
    msgpack_sbuffer_destroy(&mp_sbuf);
    return rc;
}

int flb_chunk_trace_pre_output(struct flb_chunk_trace *trace)
{
    msgpack_packer mp_pck;
    msgpack_sbuffer mp_sbuf;
    msgpack_unpacked result;
    msgpack_object *record = NULL;
    char *buf = NULL;
    size_t buf_size;
    struct flb_time tm;
    struct flb_time tm_end;
    struct flb_input_instance *input = (struct flb_input_instance *)trace->ic->in;
    int rc = -1;
    size_t off = 0;
    flb_sds_t tag = flb_sds_create("trace");
    int records = 0;


    /* initiailize start time */
    flb_time_get(&tm);
    flb_time_get(&tm_end);

    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
    msgpack_unpacked_init(&result);

    cio_chunk_get_content(trace->ic->chunk, &buf, &buf_size);

    msgpack_pack_array(&mp_pck, 2);
    flb_pack_time_now(&mp_pck);
    if (input->alias != NULL) {
        msgpack_pack_map(&mp_pck, 7);
    } 
    else {
        msgpack_pack_map(&mp_pck, 6);
    }

    msgpack_pack_str_with_body(&mp_pck, "type", 4);
    msgpack_pack_int(&mp_pck, FLB_CHUNK_TRACE_TYPE_PRE_OUTPUT);

    msgpack_pack_str_with_body(&mp_pck, "trace_id", strlen("trace_id"));
    msgpack_pack_str_with_body(&mp_pck, trace->trace_id, strlen(trace->trace_id));

    msgpack_pack_str_with_body(&mp_pck, "plugin_instance", strlen("plugin_instance"));
    msgpack_pack_str_with_body(&mp_pck, input->name, strlen(input->name));

    if (input->alias != NULL) {
        msgpack_pack_str_with_body(&mp_pck, "plugin_alias", strlen("plugin_alias"));
        msgpack_pack_str_with_body(&mp_pck, input->alias, strlen(input->alias));
    }

    msgpack_pack_str_with_body(&mp_pck, "records", strlen("records"));

    if (buf_size > 0) {
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto sbuffer_error;
            }
            records++;
        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);

        msgpack_pack_array(&mp_pck, records);
        off = 0;
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto sbuffer_error;
            }
            flb_time_pop_from_msgpack(&tm, &result, &record);

            msgpack_pack_map(&mp_pck, 2);
            msgpack_pack_str_with_body(&mp_pck, "timestamp", strlen("timestamp"));
            flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_INT);
            msgpack_pack_str_with_body(&mp_pck, "record", strlen("record"));
            msgpack_pack_object(&mp_pck, *record);

        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);
    }

    msgpack_pack_str_with_body(&mp_pck, "start_time", strlen("start_time"));
    flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_INT);
    msgpack_pack_str_with_body(&mp_pck, "end_time", strlen("end_time"));
    flb_time_append_to_msgpack(&tm_end, &mp_pck, FLB_TIME_ETFMT_INT);
    in_emitter_add_record(tag, flb_sds_len(tag), mp_sbuf.data, mp_sbuf.size,
                          trace->ctxt->input);
sbuffer_error:
    flb_sds_destroy(tag);
    msgpack_unpacked_destroy(&result);
    msgpack_sbuffer_destroy(&mp_sbuf);
    return rc;
}

int flb_chunk_trace_filter(struct flb_chunk_trace *tracer, void *pfilter, struct flb_time *tm_start, struct flb_time *tm_end, char *buf, size_t buf_size)
{
    msgpack_packer mp_pck;
    msgpack_sbuffer mp_sbuf;
    msgpack_unpacked result;
    msgpack_object *record = NULL;
    int rc = -1;
    struct flb_filter_instance *filter = (struct flb_filter_instance *)pfilter;
    flb_sds_t tag = flb_sds_create("trace");
    struct flb_time tm;
    size_t off = 0;
    int records = 0;


    if (tracer == NULL) {
        goto tracer_error;
    }

    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

    msgpack_pack_array(&mp_pck, 2);
    flb_pack_time_now(&mp_pck);
    if (filter->alias == NULL) {
        msgpack_pack_map(&mp_pck, 6);
    }
    else {
        msgpack_pack_map(&mp_pck, 7);	
    }

    msgpack_pack_str_with_body(&mp_pck, "type", strlen("type"));
    rc = msgpack_pack_int(&mp_pck, FLB_CHUNK_TRACE_TYPE_FILTER);
    if (rc == -1) {
        goto sbuffer_error;
    }

    msgpack_pack_str_with_body(&mp_pck, "start_time", strlen("start_time"));
    //msgpack_pack_double(&mp_pck, flb_time_to_double(tm_start));
    flb_time_append_to_msgpack(tm_start, &mp_pck, FLB_TIME_ETFMT_INT);
    msgpack_pack_str_with_body(&mp_pck, "end_time", strlen("end_time"));
    //msgpack_pack_double(&mp_pck, flb_time_to_double(tm_end));
    flb_time_append_to_msgpack(tm_end, &mp_pck, FLB_TIME_ETFMT_INT);

    msgpack_pack_str_with_body(&mp_pck, "trace_id", strlen("trace_id"));
    msgpack_pack_str_with_body(&mp_pck, tracer->trace_id, strlen(tracer->trace_id));

    
    msgpack_pack_str_with_body(&mp_pck, "plugin_instance", strlen("plugin_instance"));
    rc = msgpack_pack_str_with_body(&mp_pck, filter->name, strlen(filter->name));
    if (rc == -1) {
        goto sbuffer_error;
    }
    
    if (filter->alias != NULL) {
        msgpack_pack_str_with_body(&mp_pck, "plugin_alias", strlen("plugin_alias"));
        msgpack_pack_str_with_body(&mp_pck, filter->alias, strlen(filter->alias));
    }

    msgpack_pack_str_with_body(&mp_pck, "records", strlen("records"));

    msgpack_unpacked_init(&result);

    if (buf_size > 0) {
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto unpack_error;
            }
            records++;
        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);

        msgpack_pack_array(&mp_pck, records);
        off = 0;
        do {
            rc = msgpack_unpack_next(&result, buf, buf_size, &off);
            if (rc != MSGPACK_UNPACK_SUCCESS) {
                flb_error("unable to unpack record");
                goto unpack_error;
            }
            flb_time_pop_from_msgpack(&tm, &result, &record);

            msgpack_pack_map(&mp_pck, 2);
            msgpack_pack_str_with_body(&mp_pck, "timestamp", strlen("timestamp"));
            flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_INT);
            msgpack_pack_str_with_body(&mp_pck, "record", strlen("record"));
            msgpack_pack_object(&mp_pck, *record);

        } while (rc == MSGPACK_UNPACK_SUCCESS && off < buf_size);
    }

    in_emitter_add_record(tag, flb_sds_len(tag), mp_sbuf.data, mp_sbuf.size,
                          tracer->ctxt->input);
    
    rc = 0;

unpack_error:
    msgpack_unpacked_destroy(&result);
sbuffer_error:
    msgpack_sbuffer_destroy(&mp_sbuf);
tracer_error:
    flb_sds_destroy(tag);
    return rc;
}