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

#include <fluent-bit.h>
#include "flb_tests_runtime.h"
#include <stdio.h>

#define FLUSH_DURATION 1
#define WAIT_FOR_FLUSH sleep(FLUSH_DURATION);

/* Test data */
static const char *simple_log =
    "[1519233660.000000, {\"log\":\"%s\", \"stream\":\"%s\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"value\":\"%d\"}]";
static const char *simple_log_with_missing_log_key =
    "[1519233660.000000, {\"msg\":\"%s\", \"stream\":\"%s\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"value\":\"%d\"}]";
static const char *simple_log_with_missing_stream_key =
    "[1519233660.000000, {\"log\":\"%s\", \"source\":\"%s\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"value\":\"%d\"}]";
static const char *nested_log =
    "[1519234013.360921, {\"logwrapper\":{\"log\":\"%s\", \"extra\":\"field\"}, \"stream\":\"stdout\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"kubernetes\":{\"pod_name\":\"%s\", \"namespace_name\":\"default\", \"pod_id\":\"64f7da23-172c-11e8-bfad-080027749cbc\", \"labels\":{\"run\":\"apache-logs\"}, \"host\":\"minikube\", \"container_name\":\"apache-logs\", \"docker_id\":\"ac6095b6c715d823d732dcc9067f75b1299de5cc69a012b08d616a6058bdc0ad\"}, \"va\":\"%d\"}]";
static const char *swap_log_field =
    "[1519234013.360921, {\"log\":{\"logwrapper\":\"%s\", \"extra\":\"field\"}, \"stream\":\"stdout\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"kubernetes\":{\"pod_name\":\"%s\", \"namespace_name\":\"default\", \"pod_id\":\"64f7da23-172c-11e8-bfad-080027749cbc\", \"labels\":{\"run\":\"apache-logs\"}, \"host\":\"minikube\", \"container_name\":\"apache-logs\", \"docker_id\":\"ac6095b6c715d823d732dcc9067f75b1299de5cc69a012b08d616a6058bdc0ad\"}, \"va\":\"%d\"}]";
static const char *swap_name_field =
    "[1519234013.360921, {\"logwrapper\":{\"log\":\"%s\", \"extra\":\"field\"}, \"stream\":\"stdout\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"pod_name\":{\"kubernetes\":\"%s\", \"namespace_name\":\"default\", \"pod_id\":\"64f7da23-172c-11e8-bfad-080027749cbc\", \"labels\":{\"run\":\"apache-logs\"}, \"host\":\"minikube\", \"container_name\":\"apache-logs\", \"docker_id\":\"ac6095b6c715d823d732dcc9067f75b1299de5cc69a012b08d616a6058bdc0ad\"}, \"va\":\"%d\"}]";
static const char *nested_log_with_missing_log_field =
    "[1519234013.360921, {\"logwrapper\":{\"msg\":\"%s\", \"extra\":\"field\"}, \"stream\":\"stdout\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"kubernetes\":{\"pod_name\":\"%s\", \"namespace_name\":\"default\", \"pod_id\":\"64f7da23-172c-11e8-bfad-080027749cbc\", \"labels\":{\"run\":\"apache-logs\"}, \"host\":\"minikube\", \"container_name\":\"apache-logs\", \"docker_id\":\"ac6095b6c715d823d732dcc9067f75b1299de5cc69a012b08d616a6058bdc0ad\"}, \"va\":\"%d\"}]";
static const char *nested_log_with_missing_name_field =
    "[1519234013.360921, {\"logwrapper\":{\"log\":\"%s\", \"extra\":\"field\"}, \"stream\":\"stdout\", \"time\":\"2018-02-21T17:26:53.360920913Z\", \"kubernetes\":{\"pod_alias\":\"%s\", \"namespace_name\":\"default\", \"pod_id\":\"64f7da23-172c-11e8-bfad-080027749cbc\", \"labels\":{\"run\":\"apache-logs\"}, \"host\":\"minikube\", \"container_name\":\"apache-logs\", \"docker_id\":\"ac6095b6c715d823d732dcc9067f75b1299de5cc69a012b08d616a6058bdc0ad\"}, \"va\":\"%d\"}]";
static const char *_32_bytes_msg = "This meeesage is 32 symbols long";
static const char *_11_bytes_msg = "I will pass";
static const char *_6_bytes_msg = "I pass";
static const char *_180_bytes_msg =
    "This message is 180 bytes long, so it will be used where we are sure that this message will pass every time or not at all. So used it carefully and at the proper place. Understand?";
static const char *stdout_str = "stdout";
static const char *stderr_str = "stderr";
static const char *apiserver = "kube-apiserver";
static const char *alertmanager = "alertmanager";

/* Utility functions */
char *push_data_to_engine_and_take_output(flb_ctx_t * ctx, int in_ffd,
                                          char *message);
void check_if_message_pass_through_engine(flb_ctx_t * ctx, int in_ffd,
                                          char *message);
void check_if_message_doesnt_pass_through_engine(flb_ctx_t * ctx, int in_ffd,
                                                 char *message);
pthread_mutex_t result_mutex = PTHREAD_MUTEX_INITIALIZER;
char *output = NULL;



/* Test functions */

void flb_test_simple_log(void);
void test_nestest_name_fields(void);
void test_default_name_field(void);
void test_default_log_field(void);


/* Test list */
TEST_LIST = {
    {
    "throttle_size", flb_test_simple_log}, {
    "throttle_size2", test_nestest_name_fields}, {
    "throttle_size3", test_default_name_field}, {
    "throttle_size4", test_default_log_field}, {
    NULL, NULL}
};

void set_output(char *val)
{
    pthread_mutex_lock(&result_mutex);
    output = val;
    pthread_mutex_unlock(&result_mutex);
}

char *get_output(void)
{
    char *val;

    pthread_mutex_lock(&result_mutex);
    val = output;
    output = NULL;
    pthread_mutex_unlock(&result_mutex);

    return val;
}

int callback_test(void *data, size_t size, void *cb_data)
{
    if (size > 0) {
        flb_debug("[test_filter_throttle_size] received message: %s", data);
        set_output(data);       /* success */
    }
    return 0;
}

void flb_test_simple_log(void)
{
    int i;
    int ret;
    char p[1000];
    flb_ctx_t *ctx;
    int in_ffd;
    int out_ffd;
    int filter_ffd;


    struct flb_lib_out_cb cb;
    cb.cb = callback_test;
    cb.data = NULL;

    ctx = flb_create();

    /* Configure service */
    flb_service_set(ctx, "Flush", "1", "Grace" "1", "Log_Level", "debug",
                    NULL);

    in_ffd = flb_input(ctx, (char *) "lib", NULL);
    TEST_CHECK(in_ffd >= 0);
    flb_input_set(ctx, in_ffd, "tag", "test", NULL);

    /* Output */
    out_ffd = flb_output(ctx, (char *) "lib", &cb);
    TEST_CHECK(out_ffd >= 0);
    flb_output_set(ctx, out_ffd, "Match", "*", "format", "json", NULL);

    filter_ffd = flb_filter(ctx, (char *) "throttle_size", NULL);
    TEST_CHECK(filter_ffd >= 0);
    ret = flb_filter_set(ctx, filter_ffd, "match", "*", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "rate", "10", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "window", "30", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "interval", "3s", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "print_status", "true", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "log_field", "log", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "name_field", "stream", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "window_time_duration", "10s", NULL);
    TEST_CHECK(ret == 0);

    ret = flb_start(ctx);
    TEST_CHECK(ret == 0);

    /*  Verify that the size throttle plugin differentiates logs by a non nested name_field.
       We put 9 logs 32 bytes long which is 288 bytes of total or rate of 9.6.
       If all logs passed this means that the the plugin sees them as two seperates tipes or
       does now work at all.Or each logs is seen as different group of logs. */
    for (i = 0; i < 9; i++) {
        /*Make message with sream: stdout */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stdout_str, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with sream: stderr */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stderr_str, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*  Verify that the plugin cut logs wen rate exceeds 10.
       By add next message which is 32 bytes log the total must become 320 which
       makes the rate 10.66. If the messege is dropped this means that the plugin
       works properly. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stdout_str, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);


    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stderr_str, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);

    /*Now we will pass two messages with 11 bytes of lenght an they will make the
       rate 9.96 which is less than 10 and they must pass */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _11_bytes_msg, stdout_str, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _11_bytes_msg, stderr_str, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    /*check that if log field is missing then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with sream: stdout */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log_with_missing_log_key,
                 _180_bytes_msg, stdout_str, i + 11);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with sream: stderr */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log_with_missing_log_key,
                 _180_bytes_msg, stderr_str, i + 11);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*check that if stream field is missing then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with sream: stdout */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log_with_missing_stream_key,
                 _180_bytes_msg, stdout_str, i + 13);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with sream: stderr */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log_with_missing_stream_key,
                 _180_bytes_msg, stderr_str, i + 13);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    flb_stop(ctx);
    flb_destroy(ctx);
}

void test_nestest_name_fields(void)
{
    int i;
    int ret;
    char p[1000];
    flb_ctx_t *ctx;
    int in_ffd;
    int out_ffd;
    int filter_ffd;


    struct flb_lib_out_cb cb;
    cb.cb = callback_test;
    cb.data = NULL;

    ctx = flb_create();

    /* Configure service */
    flb_service_set(ctx, "Flush", "1", "Grace" "1", "Log_Level", "debug",
                    NULL);

    in_ffd = flb_input(ctx, (char *) "lib", NULL);
    TEST_CHECK(in_ffd >= 0);
    flb_input_set(ctx, in_ffd, "tag", "test", NULL);

    /* Output */
    out_ffd = flb_output(ctx, (char *) "lib", &cb);
    TEST_CHECK(out_ffd >= 0);
    flb_output_set(ctx, out_ffd, "Match", "*", "format", "json", NULL);

    filter_ffd = flb_filter(ctx, (char *) "throttle_size", NULL);
    TEST_CHECK(filter_ffd >= 0);
    ret = flb_filter_set(ctx, filter_ffd, "match", "*", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "rate", "10", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "window", "30", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "interval", "3s", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "print_status", "false", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "log_field", "logwrapper|log", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "name_field", "kubernetes|pod_name",
                       NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "window_time_duration", "10s", NULL);
    TEST_CHECK(ret == 0);

    ret = flb_start(ctx);
    TEST_CHECK(ret == 0);

    /*  Verify that the size throttle plugin differentiates logs by a nested name_field and nested log_field.
       We put 9 logs 32 bytes long which is 288 bytes of total or rate of 9.6.
       If all logs passed this means that the the plugin sees them as two seperates tipes or
       does now work at all.Or each logs is seen as different group of logs. */
    for (i = 0; i < 9; i++) {
        /*Make message with sream: stdout */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log, _32_bytes_msg, apiserver, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernetes.pod_name: alermanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log, _32_bytes_msg, alertmanager, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*  Verify that the plugin cut logs when rate exceeds 10.
       By add next message which is 32 bytes log the total must become 320 which
       makes the rate 10.66. If the messege is dropped this means that the plugin
       works properly. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _32_bytes_msg, apiserver, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);


    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _32_bytes_msg, alertmanager, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);

    /*Now we will pass two messages with 11 bytes of lenght an they will make the
       rate 9.96 which is less than 10 and they must pass */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _11_bytes_msg, apiserver, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _11_bytes_msg, alertmanager, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    /*check that if log field is missing then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with kubernetes.pod_name: kube-apiserver */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log_with_missing_log_field,
                 _180_bytes_msg, apiserver, i + 11);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernetes.pod_name: alertmanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log_with_missing_log_field,
                 _180_bytes_msg, alertmanager, i + 11);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*check that if pod_name field is missing then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with kubernetes.pod_name: kube-apiserver */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log_with_missing_name_field,
                 _180_bytes_msg, apiserver, i + 13);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernetes.pod_name: alermanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log_with_missing_name_field,
                 _180_bytes_msg, alertmanager, i + 13);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*check that if pod_name is not in the right order then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with kubernetes.pod_name: kube-apiserver */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), swap_name_field, _180_bytes_msg, apiserver,
                 i + 15);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernets.pod_name: alertmanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), swap_name_field, _180_bytes_msg, alertmanager,
                 i + 15);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*check that if log field is wrong order then the messages will pass throughout the engine */
    for (i = 0; i < 2; i++) {
        /*Make message with kubernetes.pod_name: kube-apiserver */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), swap_log_field, _180_bytes_msg, apiserver,
                 i + 17);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernetes: alertmanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), swap_log_field, _180_bytes_msg, alertmanager,
                 i + 17);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    flb_stop(ctx);
    flb_destroy(ctx);
}

void test_default_name_field(void)
{
    int i;
    int ret;
    char p[200];
    flb_ctx_t *ctx;
    int in_ffd;
    int out_ffd;
    int filter_ffd;

    struct flb_lib_out_cb cb;
    cb.cb = callback_test;
    cb.data = NULL;

    ctx = flb_create();

    /* Configure service */
    flb_service_set(ctx, "Flush", "1", "Grace" "1", "Log_Level", "debug",
                    NULL);

    in_ffd = flb_input(ctx, (char *) "lib", NULL);
    TEST_CHECK(in_ffd >= 0);
    flb_input_set(ctx, in_ffd, "tag", "test", NULL);

    /* Output */
    out_ffd = flb_output(ctx, (char *) "lib", &cb);
    TEST_CHECK(out_ffd >= 0);
    flb_output_set(ctx, out_ffd, "Match", "*", "format", "json", NULL);

    filter_ffd = flb_filter(ctx, (char *) "throttle_size", NULL);
    TEST_CHECK(filter_ffd >= 0);
    ret = flb_filter_set(ctx, filter_ffd, "match", "*", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "rate", "10", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "window", "30", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "interval", "3s", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "print_status", "false", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "log_field", "log", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "window_time_duration", "10s", NULL);
    TEST_CHECK(ret == 0);

    ret = flb_start(ctx);
    TEST_CHECK(ret == 0);

    /*  Verify that the size throttle plugin do not differentiates logs by name_field.
       We put 8 logs 32 bytes long which is 256 bytes of total or rate of 8.53.
       If all logs passed this means that the the plugin sees them as one or
       does now work at all.Or each logs is seen as different group of logs. */
    for (i = 0; i < 4; i++) {
        /*Make message with sream: stdout */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stdout_str, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with sream: stderr */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stderr_str, i);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*Add one exra message with lenght of 32 bytes to make the total 288 or rate of 9.6 */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stdout_str, 9);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    /*  Verify that the plugin cut logs when rate exceeds 10.
       By add next message which is 32 bytes log the total must become 320 which
       makes the rate 10.66. If the messege is dropped this means that the plugin
       works properly. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stdout_str, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);
    /*  Verify that the plugin cut logs when rate exceeds 10.
       By add next message which is 32 bytes log the total must become 320 which
       makes the rate 10.66. If the messege is dropped this means that the plugin
       works properly. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _32_bytes_msg, stderr_str, 9);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);


    /*Now we will pass two messages with 6 bytes of lenght an they will make the
       rate 10 whch is the limit and the message must pass. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _6_bytes_msg, stdout_str, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), simple_log, _6_bytes_msg, stderr_str, 10);
    check_if_message_pass_through_engine(ctx, in_ffd, p);


    flb_stop(ctx);
    flb_destroy(ctx);
}


void test_default_log_field(void)
{
    int i;
    int ret;
    char p[1000];
    flb_ctx_t *ctx;
    int in_ffd;
    int out_ffd;
    int filter_ffd;


    struct flb_lib_out_cb cb;
    cb.cb = callback_test;
    cb.data = NULL;

    ctx = flb_create();

    /* Configure service */
    flb_service_set(ctx, "Flush", "1", "Grace" "1", "Log_Level", "debug",
                    NULL);

    in_ffd = flb_input(ctx, (char *) "lib", NULL);
    TEST_CHECK(in_ffd >= 0);
    flb_input_set(ctx, in_ffd, "tag", "test", NULL);

    /* Output */
    out_ffd = flb_output(ctx, (char *) "lib", &cb);
    TEST_CHECK(out_ffd >= 0);
    flb_output_set(ctx, out_ffd, "Match", "*", "format", "json", NULL);

    filter_ffd = flb_filter(ctx, (char *) "throttle_size", NULL);
    TEST_CHECK(filter_ffd >= 0);
    ret = flb_filter_set(ctx, filter_ffd, "match", "*", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "rate", "43", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "window", "30", NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "interval", "3s", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "name_field", "kubernetes|pod_name",
                       NULL);
    TEST_CHECK(ret == 0);
    ret = flb_filter_set(ctx, filter_ffd, "print_status", "false", NULL);
    TEST_CHECK(ret == 0);
    ret =
        flb_filter_set(ctx, filter_ffd, "window_time_duration", "10s", NULL);
    TEST_CHECK(ret == 0);

    ret = flb_start(ctx);
    TEST_CHECK(ret == 0);

    /*  Verify that fluent-bit take in account all of the message payload when log_field is missing.
       We shall put two messages with differen kubernetes.podname which will pass.
       One message is about 463 bytes long and two makes the rate about 30.87 */
    for (i = 0; i < 2; i++) {
        /*Make message with kubernetes.pod_name:kube-apiserver */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log, _180_bytes_msg, apiserver, 1);
        check_if_message_pass_through_engine(ctx, in_ffd, p);

        /*Make message with kubernetes.pod_name:alermanager */
        memset(p, '\0', sizeof(p));
        snprintf(p, sizeof(p), nested_log, _180_bytes_msg, alertmanager, 1);
        check_if_message_pass_through_engine(ctx, in_ffd, p);
    }

    /*  Verify that the plugin cut logs when rate exceeds 37.
       We shall add again two messages with size 463 and they will
       fail passing. */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _180_bytes_msg, apiserver, 1);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);

    /*Make message with kubernetes.pod_name:alermanager */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _180_bytes_msg, alertmanager, 1);
    check_if_message_doesnt_pass_through_engine(ctx, in_ffd, p);

    /*The next two must pass */
    /*Make message with kubernetes.pod_name:kube-apiserver */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _32_bytes_msg, apiserver, 1);
    check_if_message_pass_through_engine(ctx, in_ffd, p);

    /*Make message with kubernetes.pod_name:alermanager */
    memset(p, '\0', sizeof(p));
    snprintf(p, sizeof(p), nested_log, _32_bytes_msg, alertmanager, 1);
    check_if_message_pass_through_engine(ctx, in_ffd, p);


    flb_stop(ctx);
    flb_destroy(ctx);
}

char *push_data_to_engine_and_take_output(flb_ctx_t * ctx, int in_ffd,
                                          char *message)
{
    char *result = NULL;
    int bytes;
    /*Push the message into the engine */
    bytes = flb_lib_push(ctx, in_ffd, (void *) message, strlen(message));
    WAIT_FOR_FLUSH              /*wait the output data to be flushed */
        result = get_output();  /*get the output message */
    TEST_CHECK(bytes == strlen(message));       /*Chech if all of the message was proceesed */
    return result;
}

void check_if_message_pass_through_engine(flb_ctx_t * ctx, int in_ffd,
                                          char *message)
{
    char *result;
    result = push_data_to_engine_and_take_output(ctx, in_ffd, message);
    /*Check that the message go throught engine without modification */
    TEST_CHECK(strncmp(result, message, strlen(result)) == 0);
}

void check_if_message_doesnt_pass_through_engine(flb_ctx_t * ctx, int in_ffd,
                                                 char *message)
{
    char *result;
    result = push_data_to_engine_and_take_output(ctx, in_ffd, message);
    /*Check that the message didn't throught engine */
    TEST_CHECK(result == NULL);
}