summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_tail/tail_multiline.c
blob: 71c031014a3ec853c7400f460b55c4ac41037d42 (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
/* -*- 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 <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_kv.h>

#include "tail_config.h"
#include "tail_multiline.h"

static int tail_mult_append(struct flb_parser *parser,
                            struct flb_tail_config *ctx)
{
    struct flb_tail_mult *mp;

    mp = flb_malloc(sizeof(struct flb_tail_mult));
    if (!mp) {
        flb_errno();
        return -1;
    }

    mp->parser = parser;
    mk_list_add(&mp->_head, &ctx->mult_parsers);

    return 0;
}

int flb_tail_mult_create(struct flb_tail_config *ctx,
                         struct flb_input_instance *ins,
                         struct flb_config *config)
{
    int ret;
    const char *tmp;
    struct mk_list *head;
    struct flb_parser *parser;
    struct flb_kv *kv;

    if (ctx->multiline_flush <= 0) {
        ctx->multiline_flush = 1;
    }

    mk_list_init(&ctx->mult_parsers);

    /* Get firstline parser */
    tmp = flb_input_get_property("parser_firstline", ins);
    if (!tmp) {
        flb_plg_error(ctx->ins, "multiline: no parser defined for firstline");
        return -1;
    }
    parser = flb_parser_get(tmp, config);
    if (!parser) {
        flb_plg_error(ctx->ins, "multiline: invalid parser '%s'", tmp);
        return -1;
    }

    ctx->mult_parser_firstline = parser;

    /* Read all multiline rules */
    mk_list_foreach(head, &ins->properties) {
        kv = mk_list_entry(head, struct flb_kv, _head);
        if (strcasecmp("parser_firstline", kv->key) == 0) {
            continue;
        }

        if (strncasecmp("parser_", kv->key, 7) == 0) {
            parser = flb_parser_get(kv->val, config);
            if (!parser) {
                flb_plg_error(ctx->ins, "multiline: invalid parser '%s'", kv->val);
                return -1;
            }

            ret = tail_mult_append(parser, ctx);
            if (ret == -1) {
                return -1;
            }
        }
    }

    return 0;
}

int flb_tail_mult_destroy(struct flb_tail_config *ctx)
{
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_tail_mult *mp;

    if (ctx->multiline == FLB_FALSE) {
        return 0;
    }

    mk_list_foreach_safe(head, tmp, &ctx->mult_parsers) {
        mp = mk_list_entry(head, struct flb_tail_mult, _head);
        mk_list_del(&mp->_head);
        flb_free(mp);
    }

    return 0;
}

/* Process the result of a firstline match */
int flb_tail_mult_process_first(time_t now,
                                char *buf, size_t size,
                                struct flb_time *out_time,
                                struct flb_tail_file *file,
                                struct flb_tail_config *ctx)
{
    int ret;
    size_t off;
    msgpack_object map;
    msgpack_unpacked result;

    /* If a previous multiline context already exists, flush first */
    if (file->mult_firstline && !file->mult_skipping) {
        flb_tail_mult_flush(file, ctx);
    }

    /* Remark as first multiline message */
    file->mult_firstline = FLB_TRUE;

    /* Validate obtained time, if not set, set the current time */
    if (flb_time_to_nanosec(out_time) == 0L) {
        flb_time_get(out_time);
    }

    /* Should we skip this multiline record ? */
    if (ctx->ignore_older > 0) {
        if ((now - ctx->ignore_older) > out_time->tm.tv_sec) {
            flb_free(buf);
            file->mult_skipping = FLB_TRUE;
            file->mult_firstline = FLB_TRUE;

            /* we expect more data to skip */
            return FLB_TAIL_MULT_MORE;
        }
    }

    /* Re-initiate buffers */
    msgpack_sbuffer_init(&file->mult_sbuf);
    msgpack_packer_init(&file->mult_pck, &file->mult_sbuf, msgpack_sbuffer_write);

    /*
     * flb_parser_do() always return a msgpack buffer, so we tweak our
     * local msgpack reference to avoid an extra allocation. The only
     * concern is that we don't know what's the real size of the memory
     * allocated, so we assume it's just 'out_size'.
     */
    file->mult_flush_timeout = now + (ctx->multiline_flush - 1);
    file->mult_sbuf.data = buf;
    file->mult_sbuf.size = size;
    file->mult_sbuf.alloc = size;

    /* Set multiline status */
    file->mult_firstline = FLB_TRUE;
    file->mult_skipping = FLB_FALSE;
    flb_time_copy(&file->mult_time, out_time);

    off = 0;
    msgpack_unpacked_init(&result);
    ret = msgpack_unpack_next(&result, buf, size, &off);
    if (ret != MSGPACK_UNPACK_SUCCESS) {
        msgpack_sbuffer_destroy(&file->mult_sbuf);
        msgpack_unpacked_destroy(&result);
        return FLB_TAIL_MULT_NA;
    }

    map = result.data;
    file->mult_keys = map.via.map.size;
    msgpack_unpacked_destroy(&result);

    /* We expect more data */
    return FLB_TAIL_MULT_MORE;
}

/* Append a raw log entry to the last structured field in the mult buffer */
static inline void flb_tail_mult_append_raw(char *buf, int size,
                                            struct flb_tail_file *file,
                                            struct flb_tail_config *config)
{
    /* Append the raw string */
    msgpack_pack_str(&file->mult_pck, size);
    msgpack_pack_str_body(&file->mult_pck, buf, size);
}

/* Check if the last key value type of a map is string or not */
static inline int is_last_key_val_string(char *buf, size_t size)
{
    int ret = FLB_FALSE;
    size_t off;
    msgpack_unpacked result;
    msgpack_object v;
    msgpack_object root;

    off = 0;
    msgpack_unpacked_init(&result);
    ret = msgpack_unpack_next(&result, buf, size, &off);
    if (ret != MSGPACK_UNPACK_SUCCESS) {
        return ret;
    }

    root = result.data;
    if (root.type != MSGPACK_OBJECT_MAP) {
        ret = FLB_FALSE;
    }
    else {
        if (root.via.map.size == 0) {
            ret = FLB_FALSE;
        }
        else {
            v = root.via.map.ptr[root.via.map.size - 1].val;
            if (v.type == MSGPACK_OBJECT_STR) {
                ret = FLB_TRUE;
            }
        }
    }

    msgpack_unpacked_destroy(&result);
    return ret;
}

int flb_tail_mult_process_content(time_t now,
                                  char *buf, size_t len,
                                  struct flb_tail_file *file,
                                  struct flb_tail_config *ctx,
                                  size_t processed_bytes)
{
    int ret;
    size_t off;
    void *out_buf;
    size_t out_size = 0;
    struct mk_list *head;
    struct flb_tail_mult *mult_parser = NULL;
    struct flb_time out_time = {0};
    msgpack_object map;
    msgpack_unpacked result;

    /* Always check if this line is the beginning of a new multiline message */
    ret = flb_parser_do(ctx->mult_parser_firstline,
                        buf, len,
                        &out_buf, &out_size, &out_time);
    if (ret >= 0) {
        /*
         * The content is a candidate for a firstline, but we need to perform
         * the extra-mandatory check where the last key value type must be
         * a string, otherwise no string concatenation with continuation lines
         * will be possible.
         */
        ret = is_last_key_val_string(out_buf, out_size);
        if (ret == FLB_TRUE)
            file->mult_firstline_append = FLB_TRUE;
        else
            file->mult_firstline_append = FLB_FALSE;

        flb_tail_mult_process_first(now, out_buf, out_size, &out_time,
                                    file, ctx);
        return FLB_TAIL_MULT_MORE;
    }

    if (file->mult_skipping == FLB_TRUE) {
        return FLB_TAIL_MULT_MORE;
    }

    /*
     * Once here means we have some data that is a continuation, iterate
     * parsers trying to find a match
     */
    out_buf = NULL;
    mk_list_foreach(head, &ctx->mult_parsers) {
        mult_parser = mk_list_entry(head, struct flb_tail_mult, _head);

        /* Process line text with current parser */
        out_buf = NULL;
        out_size = 0;
        ret = flb_parser_do(mult_parser->parser,
                            buf, len,
                            &out_buf, &out_size, &out_time);
        if (ret < 0) {
            mult_parser = NULL;
            continue;
        }

        /* The line was processed, break the loop and buffer the data */
        break;
    }

    if (!mult_parser) {
        /*
         * If no parser was found means the string log must be appended
         * to the last structured field.
         */
        if (file->mult_firstline && file->mult_firstline_append) {
            flb_tail_mult_append_raw(buf, len, file, ctx);
        }
        else {
            flb_tail_file_pack_line(NULL, buf, len, file, processed_bytes);
        }

        return FLB_TAIL_MULT_MORE;
    }

    off = 0;
    msgpack_unpacked_init(&result);
    msgpack_unpack_next(&result, out_buf, out_size, &off);
    map = result.data;

    /* Append new map to our local msgpack buffer */
    file->mult_keys += map.via.map.size;
    msgpack_unpacked_destroy(&result);
    msgpack_sbuffer_write(&file->mult_sbuf, out_buf, out_size);
    flb_free(out_buf);

    return FLB_TAIL_MULT_MORE;
}

static int flb_tail_mult_pack_line_body(
    struct flb_log_event_encoder *context,
    struct flb_tail_file *file)
{
    size_t                  adjacent_object_offset;
    size_t                  continuation_length;
    msgpack_unpacked        adjacent_object;
    msgpack_unpacked        current_object;
    size_t                  entry_index;
    msgpack_object          entry_value;
    msgpack_object          entry_key;
    msgpack_object_map     *data_map;
    int                     map_size;
    size_t                  offset;
    struct flb_tail_config *config;
    int                     result;

    result = FLB_EVENT_ENCODER_SUCCESS;
    config = (struct flb_tail_config *) file->config;

    /* New Map size */
    map_size = file->mult_keys;

    if (file->config->path_key != NULL) {
        map_size++;

        result = flb_log_event_encoder_append_body_values(
                    context,
                    FLB_LOG_EVENT_CSTRING_VALUE(config->path_key),
                    FLB_LOG_EVENT_CSTRING_VALUE(file->name));
    }


    msgpack_unpacked_init(&current_object);
    msgpack_unpacked_init(&adjacent_object);

    offset = 0;

    while (result == FLB_EVENT_ENCODER_SUCCESS &&
           msgpack_unpack_next(&current_object,
                               file->mult_sbuf.data,
                               file->mult_sbuf.size,
                               &offset) == MSGPACK_UNPACK_SUCCESS) {
        if (current_object.data.type != MSGPACK_OBJECT_MAP) {
            continue;
        }

        data_map = &current_object.data.via.map;

        continuation_length = 0;

        for (entry_index = 0; entry_index < data_map->size; entry_index++) {
            entry_key   = data_map->ptr[entry_index].key;
            entry_value = data_map->ptr[entry_index].val;

            result = flb_log_event_encoder_append_body_msgpack_object(context,
                                                                      &entry_key);

            if (result != FLB_EVENT_ENCODER_SUCCESS) {
                break;
            }

            /* Check if this is the last entry in the map and if that is
             * the case then add the lengths of all the trailing string
             * objects after the map in order to append them to the value
             * but only if the value object is a string
             */
            if (entry_index + 1 == data_map->size &&
                entry_value.type == MSGPACK_OBJECT_STR) {
                adjacent_object_offset = offset;

                while (msgpack_unpack_next(
                        &adjacent_object,
                        file->mult_sbuf.data,
                        file->mult_sbuf.size,
                        &adjacent_object_offset) == MSGPACK_UNPACK_SUCCESS) {
                    if (adjacent_object.data.type != MSGPACK_OBJECT_STR) {
                        break;
                    }

                    /* Sum total bytes to append */
                    continuation_length += adjacent_object.data.via.str.size + 1;
                }

                result = flb_log_event_encoder_append_body_string_length(
                            context,
                            entry_value.via.str.size +
                            continuation_length);

                if (result != FLB_EVENT_ENCODER_SUCCESS) {
                    break;
                }

                result = flb_log_event_encoder_append_body_string_body(
                            context,
                            (char *) entry_value.via.str.ptr,
                            entry_value.via.str.size);

                if (result != FLB_EVENT_ENCODER_SUCCESS) {
                    break;
                }

                if (continuation_length > 0) {
                    adjacent_object_offset = offset;

                    while (msgpack_unpack_next(
                            &adjacent_object,
                            file->mult_sbuf.data,
                            file->mult_sbuf.size,
                            &adjacent_object_offset) == MSGPACK_UNPACK_SUCCESS) {
                        if (adjacent_object.data.type != MSGPACK_OBJECT_STR) {
                            break;
                        }

                        result = flb_log_event_encoder_append_body_string_body(
                                    context,
                                    "\n",
                                    1);

                        if (result != FLB_EVENT_ENCODER_SUCCESS) {
                            break;
                        }

                        result = flb_log_event_encoder_append_body_string_body(
                                    context,
                                    (char *) adjacent_object.data.via.str.ptr,
                                    adjacent_object.data.via.str.size);

                        if (result != FLB_EVENT_ENCODER_SUCCESS) {
                            break;
                        }
                    }
                }
            }
            else {
                result = flb_log_event_encoder_append_body_msgpack_object(context,
                                                                          &entry_value);
            }
        }
    }

    msgpack_unpacked_destroy(&current_object);
    msgpack_unpacked_destroy(&adjacent_object);

    /* Reset status */
    file->mult_firstline = FLB_FALSE;
    file->mult_skipping = FLB_FALSE;
    file->mult_keys = 0;
    file->mult_flush_timeout = 0;

    msgpack_sbuffer_destroy(&file->mult_sbuf);

    file->mult_sbuf.data = NULL;

    flb_time_zero(&file->mult_time);

    return result;
}

/* Flush any multiline context data into outgoing buffers */
int flb_tail_mult_flush(struct flb_tail_file *file, struct flb_tail_config *ctx)
{
    int result;

    /* nothing to flush */
    if (file->mult_firstline == FLB_FALSE) {
        return -1;
    }

    if (file->mult_keys == 0) {
        return -1;
    }

    result = flb_log_event_encoder_begin_record(file->ml_log_event_encoder);

    if (result == FLB_EVENT_ENCODER_SUCCESS) {
        result = flb_log_event_encoder_set_timestamp(
                    file->ml_log_event_encoder, &file->mult_time);
    }

    if (result == FLB_EVENT_ENCODER_SUCCESS) {
        result = flb_tail_mult_pack_line_body(
                    file->ml_log_event_encoder,
                    file);
    }

    if (result == FLB_EVENT_ENCODER_SUCCESS) {
        result = flb_log_event_encoder_commit_record(
                    file->ml_log_event_encoder);
    }

    if (result == FLB_EVENT_ENCODER_SUCCESS) {
        flb_input_log_append(ctx->ins,
                             file->tag_buf,
                             file->tag_len,
                             file->ml_log_event_encoder->output_buffer,
                             file->ml_log_event_encoder->output_length);
        result = 0;
    }
    else {
        flb_plg_error(file->config->ins, "error packing event : %d", result);

        result = -1;
    }

    flb_log_event_encoder_reset(file->ml_log_event_encoder);

    return result;
}

static void file_pending_flush(struct flb_tail_config *ctx,
                               struct flb_tail_file *file, time_t now)
{
    if (file->mult_flush_timeout > now) {
        return;
    }

    if (file->mult_firstline == FLB_FALSE) {
        if (file->mult_sbuf.data == NULL || file->mult_sbuf.size <= 0) {
            return;
        }
    }

    flb_tail_mult_flush(file, ctx);
}

int flb_tail_mult_pending_flush_all(struct flb_tail_config *ctx)
{
    time_t expired;
    struct mk_list *head;
    struct flb_tail_file *file;

    expired = time(NULL) + 3600;

    /* Iterate promoted event files with pending bytes */
    mk_list_foreach(head, &ctx->files_static) {
        file = mk_list_entry(head, struct flb_tail_file, _head);
        file_pending_flush(ctx, file, expired);
    }

    /* Iterate promoted event files with pending bytes */
    mk_list_foreach(head, &ctx->files_event) {
        file = mk_list_entry(head, struct flb_tail_file, _head);
        file_pending_flush(ctx, file, expired);
    }

    return 0;
}

int flb_tail_mult_pending_flush(struct flb_input_instance *ins,
                                struct flb_config *config, void *context)
{
    time_t now;
    struct mk_list *head;
    struct flb_tail_file *file;
    struct flb_tail_config *ctx = context;

    now = time(NULL);

    /* Iterate promoted event files with pending bytes */
    mk_list_foreach(head, &ctx->files_static) {
        file = mk_list_entry(head, struct flb_tail_file, _head);

        file_pending_flush(ctx, file, now);
    }

    /* Iterate promoted event files with pending bytes */
    mk_list_foreach(head, &ctx->files_event) {
        file = mk_list_entry(head, struct flb_tail_file, _head);

        file_pending_flush(ctx, file, now);
    }

    return 0;
}