Coverage Report

Created: 2026-11-12 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/out_udp/udp_conf.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_sds.h>
23
#include <fluent-bit/flb_pack.h>
24
25
#include "udp.h"
26
#include "udp_conf.h"
27
28
struct flb_out_udp *flb_udp_conf_create(struct flb_output_instance *ins,
29
                                        struct flb_config *config)
30
0
{
31
0
    int ret;
32
0
    const char *tmp;
33
0
    struct flb_out_udp *ctx = NULL;
34
35
    /* Allocate plugin context */
36
0
    ctx = flb_calloc(1, sizeof(struct flb_out_udp));
37
0
    if (!ctx) {
38
0
        flb_errno();
39
0
        return NULL;
40
0
    }
41
0
    ctx->ins = ins;
42
43
0
    ret = flb_output_config_map_set(ins, (void *) ctx);
44
0
    if (ret == -1) {
45
0
        flb_free(ctx);
46
0
        return NULL;
47
0
    }
48
49
    /* Set default network configuration if not set */
50
0
    flb_output_net_default("127.0.0.1", 5170, ins);
51
52
    /* raw message key mode */
53
0
    if (ctx->raw_message_key) {
54
0
        ctx->ra_raw_message_key = flb_ra_create(ctx->raw_message_key, FLB_TRUE);
55
0
        if (!ctx->ra_raw_message_key) {
56
0
            flb_plg_error(ctx->ins, "could not create record accessor for raw_message_key");
57
0
            flb_free(ctx);
58
0
            return NULL;
59
0
        }
60
0
    }
61
62
    /* Output format */
63
0
    ctx->out_format = FLB_PACK_JSON_FORMAT_NONE;
64
0
    tmp = flb_output_get_property("format", ins);
65
0
    if (tmp) {
66
0
        ret = flb_pack_to_json_format_type(tmp);
67
0
        if (ret == -1) {
68
0
            flb_plg_error(ctx->ins, "unrecognized 'format' option '%s'. "
69
0
                          "Using 'msgpack'", tmp);
70
0
        }
71
0
        else if (ret == FLB_PACK_JSON_FORMAT_OTLP ||
72
0
                 ret == FLB_PACK_JSON_FORMAT_OTLP_PRETTY) {
73
0
            flb_plg_error(ctx->ins,
74
0
                          "format '%s' is not supported by out_udp",
75
0
                          tmp);
76
0
        }
77
0
        else {
78
0
            ctx->out_format = ret;
79
0
        }
80
0
    }
81
82
    /* Date key */
83
0
    ctx->date_key = ctx->json_date_key;
84
0
    tmp = flb_output_get_property("json_date_key", ins);
85
0
    if (tmp) {
86
        /* Just check if we have to disable it */
87
0
        if (flb_utils_bool(tmp) == FLB_FALSE) {
88
0
            ctx->date_key = NULL;
89
0
        }
90
0
    }
91
92
    /* Date format for JSON output */
93
0
    ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE;
94
0
    tmp = flb_output_get_property("json_date_format", ins);
95
0
    if (tmp) {
96
0
        ret = flb_pack_to_json_date_type(tmp);
97
0
        if (ret == -1) {
98
0
            flb_plg_error(ctx->ins, "unrecognized 'json_date_format' option '%s'. "
99
0
                          "Using 'double'", tmp);
100
0
        }
101
0
        else {
102
0
            ctx->json_date_format = ret;
103
0
        }
104
0
    }
105
106
0
    ctx->host = ins->host.name;
107
0
    ctx->port = ins->host.port;
108
109
0
    ctx->endpoint_descriptor = flb_net_udp_connect(ins->host.name,
110
0
                                                   ins->host.port,
111
0
                                                   ins->net_setup.source_address);
112
113
0
    if (ctx->endpoint_descriptor < 0) {
114
0
        flb_udp_conf_destroy(ctx);
115
116
0
        flb_plg_error(ctx->ins, "Error creating upstream socket");
117
118
0
        ctx = NULL;
119
0
    }
120
121
0
    return ctx;
122
0
}
123
124
void flb_udp_conf_destroy(struct flb_out_udp *ctx)
125
0
{
126
0
    if (!ctx) {
127
0
        return;
128
0
    }
129
130
0
    if (ctx->ra_raw_message_key) {
131
0
        flb_ra_destroy(ctx->ra_raw_message_key);
132
0
    }
133
134
0
    if (ctx->endpoint_descriptor >= 0) {
135
0
        flb_socket_close(ctx->endpoint_descriptor);
136
0
    }
137
138
0
    flb_free(ctx);
139
140
    ctx = NULL;
141
0
}