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

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_metrics.h>

#include "prom.h"
#include "prom_http.h"

static int config_add_labels(struct flb_output_instance *ins,
                             struct prom_exporter *ctx)
{
    struct mk_list *head;
    struct flb_config_map_val *mv;
    struct flb_slist_entry *k = NULL;
    struct flb_slist_entry *v = NULL;
    struct flb_kv *kv;

    if (!ctx->add_labels || mk_list_size(ctx->add_labels) == 0) {
        return 0;
    }

    /* iterate all 'add_label' definitions */
    flb_config_map_foreach(head, mv, ctx->add_labels) {
        if (mk_list_size(mv->val.list) != 2) {
            flb_plg_error(ins, "'add_label' expects a key and a value, "
                          "e.g: 'add_label version 1.8.0'");
            return -1;
        }

        k = mk_list_entry_first(mv->val.list, struct flb_slist_entry, _head);
        v = mk_list_entry_last(mv->val.list, struct flb_slist_entry, _head);

        kv = flb_kv_item_create(&ctx->kv_labels, k->str, v->str);
        if (!kv) {
            flb_plg_error(ins, "could not append label %s=%s\n", k->str, v->str);
            return -1;
        }
    }

    return 0;
}

static int cb_prom_init(struct flb_output_instance *ins,
                        struct flb_config *config,
                        void *data)
{
    int ret;
    struct prom_exporter *ctx;

    flb_output_net_default("0.0.0.0", 2021 , ins);

    ctx = flb_calloc(1, sizeof(struct prom_exporter));
    if (!ctx) {
        flb_errno();
        return -1;
    }
    ctx->ins = ins;
    flb_kv_init(&ctx->kv_labels);
    flb_output_set_context(ins, ctx);

    /* Load config map */
    ret = flb_output_config_map_set(ins, (void *) ctx);
    if (ret == -1) {
        return -1;
    }

    /* Parse 'add_label' */
    ret = config_add_labels(ins, ctx);
    if (ret == -1) {
        return -1;
    }

    /* HTTP Server context */
    ctx->http = prom_http_server_create(ctx,
                                        ins->host.name, ins->host.port, config);
    if (!ctx->http) {
        flb_plg_error(ctx->ins, "could not initialize HTTP server, aborting");
        return -1;
    }

    /* Hash table for metrics */
    ctx->ht_metrics = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 32, 0);
    if (!ctx->ht_metrics) {
        flb_plg_error(ctx->ins, "could not initialize hash table for metrics");
        return -1;
    }

    /* Start HTTP Server */
    ret = prom_http_server_start(ctx->http);
    if (ret == -1) {
        return -1;
    }

    flb_plg_info(ctx->ins, "listening iface=%s tcp_port=%d",
                 ins->host.name, ins->host.port);
    return 0;
}

static void append_labels(struct prom_exporter *ctx, struct cmt *cmt)
{
    struct flb_kv *kv;
    struct mk_list *head;

    mk_list_foreach(head, &ctx->kv_labels) {
        kv = mk_list_entry(head, struct flb_kv, _head);
        cmt_label_add(cmt, kv->key, kv->val);
    }
}

static int hash_store(struct prom_exporter *ctx, struct flb_input_instance *ins,
                      cfl_sds_t buf)
{
    int ret;
    int len;

    len = strlen(ins->name);

    /* store/override the content into the hash table */
    ret = flb_hash_table_add(ctx->ht_metrics, ins->name, len,
                             buf, cfl_sds_len(buf));
    if (ret < 0) {
        return -1;
    }

    return 0;
}

static flb_sds_t hash_format_metrics(struct prom_exporter *ctx)
{
    int size = 2048;
    flb_sds_t buf;

    struct mk_list *head;
    struct flb_hash_table_entry *entry;


    buf = flb_sds_create_size(size);
    if (!buf) {
        return NULL;
    }

    /* Take every hash entry and compose one buffer with the whole content */
    mk_list_foreach(head, &ctx->ht_metrics->entries) {
        entry = mk_list_entry(head, struct flb_hash_table_entry, _head_parent);
        flb_sds_cat_safe(&buf, entry->val, entry->val_size);
    }

    return buf;
}

static void cb_prom_flush(struct flb_event_chunk *event_chunk,
                          struct flb_output_flush *out_flush,
                          struct flb_input_instance *ins, void *out_context,
                          struct flb_config *config)
{
    int ret;
    int add_ts;
    size_t off = 0;
    flb_sds_t metrics;
    cfl_sds_t text;
    struct cmt *cmt;
    struct prom_exporter *ctx = out_context;

    /*
     * A new set of metrics has arrived, perform decoding, apply labels,
     * convert to Prometheus text format and store the output in the
     * hash table for metrics.
     */
    ret = cmt_decode_msgpack_create(&cmt,
                                    (char *) event_chunk->data,
                                    event_chunk->size, &off);
    if (ret != 0) {
        FLB_OUTPUT_RETURN(FLB_ERROR);
    }

    /* append labels set by config */
    append_labels(ctx, cmt);

    /* add timestamp in the output format ? */
    if (ctx->add_timestamp) {
        add_ts = CMT_TRUE;
    }
    else {
        add_ts = CMT_FALSE;
    }

    /* convert to text representation */
    text = cmt_encode_prometheus_create(cmt, add_ts);
    if (!text) {
        cmt_destroy(cmt);
        FLB_OUTPUT_RETURN(FLB_ERROR);
    }
    cmt_destroy(cmt);

    if (cfl_sds_len(text) == 0) {
        flb_plg_debug(ctx->ins, "context without metrics (empty)");
        cmt_encode_text_destroy(text);
        FLB_OUTPUT_RETURN(FLB_OK);
    }

    /* register payload of metrics / override previous one */
    ret = hash_store(ctx, ins, text);
    if (ret == -1) {
        flb_plg_error(ctx->ins, "could not store metrics coming from: %s",
                      flb_input_name(ins));
        cmt_encode_prometheus_destroy(text);
        cmt_destroy(cmt);
        FLB_OUTPUT_RETURN(FLB_ERROR);
    }
    cmt_encode_prometheus_destroy(text);

    /* retrieve a full copy of all metrics */
    metrics = hash_format_metrics(ctx);
    if (!metrics) {
        flb_plg_error(ctx->ins, "could not retrieve metrics");
        FLB_OUTPUT_RETURN(FLB_ERROR);
    }

    /* push new (full) metrics payload */
    ret = prom_http_server_mq_push_metrics(ctx->http,
                                           (char *) metrics,
                                           flb_sds_len(metrics));
    flb_sds_destroy(metrics);

    if (ret != 0) {
        FLB_OUTPUT_RETURN(FLB_ERROR);
    }

    FLB_OUTPUT_RETURN(FLB_OK);
}

static int cb_prom_exit(void *data, struct flb_config *config)
{
    struct prom_exporter *ctx = data;

    if (!ctx) {
        return 0;
    }

    if (ctx->ht_metrics) {
        flb_hash_table_destroy(ctx->ht_metrics);
    }

    flb_kv_release(&ctx->kv_labels);
    prom_http_server_stop(ctx->http);
    prom_http_server_destroy(ctx->http);
    flb_free(ctx);

    return 0;
}

/* Configuration properties map */
static struct flb_config_map config_map[] = {
    {
     FLB_CONFIG_MAP_BOOL, "add_timestamp", "false",
     0, FLB_TRUE, offsetof(struct prom_exporter, add_timestamp),
     "Add timestamp to every metric honoring collection time."
    },

    {
     FLB_CONFIG_MAP_SLIST_1, "add_label", NULL,
     FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct prom_exporter, add_labels),
     "TCP port for listening for HTTP connections."
    },

    /* EOF */
    {0}
};

/* Plugin reference */
struct flb_output_plugin out_prometheus_exporter_plugin = {
    .name        = "prometheus_exporter",
    .description = "Prometheus Exporter",
    .cb_init     = cb_prom_init,
    .cb_flush    = cb_prom_flush,
    .cb_exit     = cb_prom_exit,
    .flags       = FLB_OUTPUT_NET,
    .event_type  = FLB_OUTPUT_METRICS,
    .config_map  = config_map,
};