Coverage Report

Created: 2025-06-24 08:09

/src/fluent-bit/plugins/in_opentelemetry/opentelemetry_config.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2024 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
#include <fluent-bit/flb_downstream.h>
22
23
#include "opentelemetry.h"
24
#include "http_conn.h"
25
26
/* default HTTP port for OTLP/HTTP is 4318 */
27
0
#define OTLP_HTTP_PORT    4318
28
29
struct flb_opentelemetry *opentelemetry_config_create(struct flb_input_instance *ins)
30
0
{
31
0
    int ret;
32
0
    char port[8];
33
0
    struct flb_opentelemetry *ctx;
34
35
0
    ctx = flb_calloc(1, sizeof(struct flb_opentelemetry));
36
0
    if (!ctx) {
37
0
        flb_errno();
38
0
        return NULL;
39
0
    }
40
0
    ctx->ins = ins;
41
0
    mk_list_init(&ctx->connections);
42
43
    /* Load the config map */
44
0
    ret = flb_input_config_map_set(ins, (void *) ctx);
45
0
    if (ret == -1) {
46
0
        flb_free(ctx);
47
0
        return NULL;
48
0
    }
49
50
    /* Listen interface (if not set, defaults to 0.0.0.0:4318) */
51
0
    flb_input_net_default_listener("0.0.0.0", OTLP_HTTP_PORT, ins);
52
53
0
    ctx->listen = flb_strdup(ins->host.listen);
54
0
    snprintf(port, sizeof(port) - 1, "%d", ins->host.port);
55
0
    ctx->tcp_port = flb_strdup(port);
56
57
    /* HTTP Server specifics */
58
0
    ctx->server = flb_calloc(1, sizeof(struct mk_server));
59
0
    ctx->server->keep_alive = MK_TRUE;
60
61
    /* monkey detects server->workers == 0 as the server not being initialized at the
62
     * moment so we want to make sure that it stays that way!
63
     */
64
65
0
    return ctx;
66
0
}
67
68
int opentelemetry_config_destroy(struct flb_opentelemetry *ctx)
69
0
{
70
    /* release all connections */
71
0
    opentelemetry_conn_release_all(ctx);
72
73
0
    if (ctx->collector_id != -1) {
74
0
        flb_input_collector_delete(ctx->collector_id, ctx->ins);
75
76
0
        ctx->collector_id = -1;
77
0
    }
78
79
0
    if (ctx->downstream != NULL) {
80
0
        flb_downstream_destroy(ctx->downstream);
81
0
    }
82
83
0
    if (ctx->enable_http2) {
84
0
        flb_http_server_destroy(&ctx->http_server);
85
0
    }
86
87
0
    if (ctx->server) {
88
0
        flb_free(ctx->server);
89
0
    }
90
91
0
    flb_free(ctx->listen);
92
0
    flb_free(ctx->tcp_port);
93
0
    flb_free(ctx);
94
95
0
    return 0;
96
0
}