Coverage Report

Created: 2026-06-07 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/out_exit/exit.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2026 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <fluent-bit/flb_output_plugin.h>
21
#include <fluent-bit/flb_utils.h>
22
#include <fluent-bit/flb_time.h>
23
#include <fluent-bit/flb_log_event_decoder.h>
24
25
#define FLB_EXIT_FLUSH_COUNT  "-1"
26
#define FLB_EXIT_RECORD_COUNT "-1"
27
#define FLB_EXIT_TIME_COUNT   "-1"
28
29
struct flb_exit {
30
    int is_running;
31
    struct flb_time start_time;
32
33
    /* config */
34
    int flush_count;
35
    int record_count;
36
    int time_count;
37
    struct flb_output_instance *ins;
38
};
39
40
static int cb_exit_init(struct flb_output_instance *ins, struct flb_config *config,
41
                        void *data)
42
0
{
43
0
    int ret;
44
0
    (void) config;
45
0
    (void) data;
46
0
    struct flb_exit *ctx;
47
48
0
    ctx = flb_malloc(sizeof(struct flb_exit));
49
0
    if (!ctx) {
50
0
        flb_errno();
51
0
        return -1;
52
0
    }
53
0
    ctx->ins = ins;
54
0
    ctx->is_running = FLB_TRUE;
55
0
    flb_time_get(&ctx->start_time);
56
57
0
    ctx->flush_count = -1;
58
0
    ctx->record_count = -1;
59
0
    ctx->time_count = -1;
60
61
0
    ret = flb_output_config_map_set(ins, (void *) ctx);
62
0
    if (ret == -1) {
63
0
        flb_free(ctx);
64
0
        return -1;
65
0
    }
66
67
0
    if (ctx->flush_count == -1 &&
68
0
        ctx->record_count == -1 &&
69
0
        ctx->time_count == -1) {
70
        // emulate legacy behaviour by setting to a single flush.
71
0
        ctx->flush_count = 1;
72
0
    }
73
74
0
    flb_output_set_context(ins, ctx);
75
76
0
    return 0;
77
0
}
78
79
static void cb_exit_flush(struct flb_event_chunk *event_chunk,
80
                          struct flb_output_flush *out_flush,
81
                          struct flb_input_instance *i_ins,
82
                          void *out_context,
83
                          struct flb_config *config)
84
0
{
85
0
    (void) i_ins;
86
0
    (void) out_context;
87
0
    struct flb_exit *ctx = out_context;
88
0
    struct flb_log_event_decoder log_decoder;
89
0
    struct flb_log_event         log_event;
90
0
    struct flb_time now;
91
0
    struct flb_time run;
92
0
    int result;
93
94
0
    if (ctx->is_running == FLB_TRUE) {
95
0
        if (ctx->flush_count > 0) {
96
0
            ctx->flush_count--;
97
0
        }
98
99
0
        if (ctx->record_count > 0 && event_chunk->type == FLB_EVENT_TYPE_LOGS) {
100
0
            result = flb_log_event_decoder_init(&log_decoder,
101
0
                                               (char *) event_chunk->data,
102
0
                                               event_chunk->size);
103
0
            if (result != FLB_EVENT_DECODER_SUCCESS) {
104
0
                flb_plg_error(ctx->ins,
105
0
                              "Log event decoder initialization error : %d", result);
106
107
0
                FLB_OUTPUT_RETURN(FLB_RETRY);
108
0
            }
109
110
0
            while (flb_log_event_decoder_next(&log_decoder,
111
0
                                              &log_event) == FLB_EVENT_DECODER_SUCCESS) {
112
0
                if (ctx->record_count > 0) {
113
0
                    ctx->record_count--;
114
0
                }
115
0
            }
116
117
0
            result = flb_log_event_decoder_get_last_result(&log_decoder);
118
0
            flb_log_event_decoder_destroy(&log_decoder);
119
120
0
            if (result != FLB_EVENT_DECODER_SUCCESS) {
121
0
                flb_plg_error(ctx->ins, "Log event decoder error : %d", result);
122
0
                FLB_OUTPUT_RETURN(FLB_ERROR);
123
0
            }
124
125
0
            FLB_OUTPUT_RETURN(FLB_OK);
126
0
        }
127
128
0
        if (ctx->time_count > 0) {
129
0
            flb_time_get(&now);
130
0
            flb_time_diff(&now, &ctx->start_time, &run);
131
0
        }
132
133
0
        if (ctx->flush_count == 0 ||
134
0
            ctx->record_count == 0 ||
135
0
            (ctx->time_count > 0 && flb_time_to_millisec(&run) > (ctx->time_count*1000))) {
136
0
            flb_engine_exit(config);
137
0
            ctx->is_running = FLB_FALSE;
138
0
        }
139
0
    }
140
141
0
    FLB_OUTPUT_RETURN(FLB_OK);
142
0
}
143
144
static int cb_exit_exit(void *data, struct flb_config *config)
145
0
{
146
0
    struct flb_exit *ctx = data;
147
0
    (void) config;
148
149
0
    flb_free(ctx);
150
0
    return 0;
151
0
}
152
153
/* Configuration properties map */
154
static struct flb_config_map config_map[] = {
155
    {
156
     FLB_CONFIG_MAP_INT, "flush_count", FLB_EXIT_FLUSH_COUNT,
157
     0, FLB_TRUE, offsetof(struct flb_exit, flush_count),
158
     "number of flushes before exiting"
159
    },
160
    {
161
     FLB_CONFIG_MAP_INT, "record_count", FLB_EXIT_RECORD_COUNT,
162
     0, FLB_TRUE, offsetof(struct flb_exit, record_count),
163
     "number of records received before exiting"
164
    },
165
    {
166
     FLB_CONFIG_MAP_INT, "time_count", FLB_EXIT_TIME_COUNT,
167
     0, FLB_TRUE, offsetof(struct flb_exit, time_count),
168
     "number of seconds before exiting (will trigger upon receiving a flush)"
169
    },
170
171
    /* EOF */
172
    {0}
173
};
174
175
struct flb_output_plugin out_exit_plugin = {
176
    .name         = "exit",
177
    .description  = "Exit after a number of flushes (test purposes)",
178
    .cb_init      = cb_exit_init,
179
    .cb_flush     = cb_exit_flush,
180
    .cb_exit      = cb_exit_exit,
181
    .config_map   = config_map,
182
    .flags        = 0,
183
};