Coverage Report

Created: 2026-02-09 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/in_elasticsearch/in_elasticsearch_config.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_input_plugin.h>
21
22
#include "in_elasticsearch.h"
23
#include "in_elasticsearch_config.h"
24
#include "in_elasticsearch_bulk_conn.h"
25
26
struct flb_in_elasticsearch *in_elasticsearch_config_create(struct flb_input_instance *ins)
27
0
{
28
0
    int ret;
29
0
    char port[8];
30
0
    struct flb_in_elasticsearch *ctx;
31
32
0
    ctx = flb_calloc(1, sizeof(struct flb_in_elasticsearch));
33
0
    if (!ctx) {
34
0
        flb_errno();
35
0
        return NULL;
36
0
    }
37
0
    ctx->ins = ins;
38
0
    mk_list_init(&ctx->connections);
39
40
    /* Load the config map */
41
0
    ret = flb_input_config_map_set(ins, (void *) ctx);
42
0
    if (ret == -1) {
43
0
        flb_free(ctx);
44
0
        return NULL;
45
0
    }
46
47
    /* Listen interface (if not set, defaults to 0.0.0.0:9200) */
48
0
    flb_input_net_default_listener("0.0.0.0", 9200, ins);
49
50
0
    ctx->listen = flb_sds_create(ins->host.listen);
51
0
    snprintf(port, sizeof(port) - 1, "%d", ins->host.port);
52
0
    ctx->tcp_port = flb_sds_create(port);
53
54
    /* HTTP Server specifics */
55
0
    ctx->server = flb_calloc(1, sizeof(struct mk_server));
56
0
    ctx->server->keep_alive = MK_TRUE;
57
58
    /* monkey detects server->workers == 0 as the server not being initialized at the
59
     * moment so we want to make sure that it stays that way!
60
     */
61
62
0
    ctx->log_encoder = flb_log_event_encoder_create(FLB_LOG_EVENT_FORMAT_DEFAULT);
63
0
    if (ctx->log_encoder == NULL) {
64
0
        flb_plg_error(ctx->ins, "event encoder initialization error");
65
0
        in_elasticsearch_config_destroy(ctx);
66
67
0
        return NULL;
68
0
    }
69
70
    /* Create record accessor for tag_key if specified */
71
0
    if (ctx->tag_key) {
72
0
        ctx->ra_tag_key = flb_ra_create(ctx->tag_key, FLB_TRUE);
73
0
        if (!ctx->ra_tag_key) {
74
0
            flb_plg_error(ctx->ins, "invalid record accessor pattern for tag_key: %s", ctx->tag_key);
75
0
            in_elasticsearch_config_destroy(ctx);
76
0
            return NULL;
77
0
        }
78
0
    }
79
80
0
    return ctx;
81
0
}
82
83
int in_elasticsearch_config_destroy(struct flb_in_elasticsearch *ctx)
84
0
{
85
0
    if (ctx->ra_tag_key) {
86
0
        flb_ra_destroy(ctx->ra_tag_key);
87
0
    }
88
89
0
    flb_log_event_encoder_destroy(ctx->log_encoder);
90
91
    /* release all connections */
92
0
    in_elasticsearch_bulk_conn_release_all(ctx);
93
94
95
0
    if (ctx->collector_id != -1) {
96
0
        flb_input_collector_delete(ctx->collector_id, ctx->ins);
97
98
0
        ctx->collector_id = -1;
99
0
    }
100
101
0
    if (ctx->downstream != NULL) {
102
0
        flb_downstream_destroy(ctx->downstream);
103
0
    }
104
105
0
    if (ctx->enable_http2) {
106
0
        flb_http_server_destroy(&ctx->http_server);
107
0
    }
108
109
0
    if (ctx->server) {
110
0
        flb_free(ctx->server);
111
0
    }
112
113
0
    flb_sds_destroy(ctx->listen);
114
0
    flb_sds_destroy(ctx->tcp_port);
115
116
0
    flb_free(ctx);
117
118
0
    return 0;
119
0
}