/src/fluent-bit/plugins/out_prometheus_exporter/prom.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_kv.h> |
22 | | #include <fluent-bit/flb_metrics.h> |
23 | | |
24 | | #include "prom.h" |
25 | | #include "prom_http.h" |
26 | | |
27 | | static int config_add_labels(struct flb_output_instance *ins, |
28 | | struct prom_exporter *ctx) |
29 | 0 | { |
30 | 0 | struct mk_list *head; |
31 | 0 | struct flb_config_map_val *mv; |
32 | 0 | struct flb_slist_entry *k = NULL; |
33 | 0 | struct flb_slist_entry *v = NULL; |
34 | 0 | struct flb_kv *kv; |
35 | |
|
36 | 0 | if (!ctx->add_labels || mk_list_size(ctx->add_labels) == 0) { |
37 | 0 | return 0; |
38 | 0 | } |
39 | | |
40 | | /* iterate all 'add_label' definitions */ |
41 | 0 | flb_config_map_foreach(head, mv, ctx->add_labels) { |
42 | 0 | if (mk_list_size(mv->val.list) != 2) { |
43 | 0 | flb_plg_error(ins, "'add_label' expects a key and a value, " |
44 | 0 | "e.g: 'add_label version 1.8.0'"); |
45 | 0 | return -1; |
46 | 0 | } |
47 | | |
48 | 0 | k = mk_list_entry_first(mv->val.list, struct flb_slist_entry, _head); |
49 | 0 | v = mk_list_entry_last(mv->val.list, struct flb_slist_entry, _head); |
50 | |
|
51 | 0 | kv = flb_kv_item_create(&ctx->kv_labels, k->str, v->str); |
52 | 0 | if (!kv) { |
53 | 0 | flb_plg_error(ins, "could not append label %s=%s\n", k->str, v->str); |
54 | 0 | return -1; |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | 0 | return 0; |
59 | 0 | } |
60 | | |
61 | | static int cb_prom_init(struct flb_output_instance *ins, |
62 | | struct flb_config *config, |
63 | | void *data) |
64 | 0 | { |
65 | 0 | int ret; |
66 | 0 | struct prom_exporter *ctx; |
67 | |
|
68 | 0 | flb_output_net_default("0.0.0.0", 2021 , ins); |
69 | |
|
70 | 0 | ctx = flb_calloc(1, sizeof(struct prom_exporter)); |
71 | 0 | if (!ctx) { |
72 | 0 | flb_errno(); |
73 | 0 | return -1; |
74 | 0 | } |
75 | 0 | ctx->ins = ins; |
76 | 0 | flb_kv_init(&ctx->kv_labels); |
77 | 0 | flb_output_set_context(ins, ctx); |
78 | | |
79 | | /* Load config map */ |
80 | 0 | ret = flb_output_config_map_set(ins, (void *) ctx); |
81 | 0 | if (ret == -1) { |
82 | 0 | return -1; |
83 | 0 | } |
84 | | |
85 | | /* Parse 'add_label' */ |
86 | 0 | ret = config_add_labels(ins, ctx); |
87 | 0 | if (ret == -1) { |
88 | 0 | return -1; |
89 | 0 | } |
90 | | |
91 | | /* HTTP Server context */ |
92 | 0 | ctx->http = prom_http_server_create(ctx, config); |
93 | 0 | if (!ctx->http) { |
94 | 0 | flb_plg_error(ctx->ins, "could not initialize HTTP server, aborting"); |
95 | 0 | return -1; |
96 | 0 | } |
97 | | |
98 | | /* Hash table for metrics */ |
99 | 0 | ctx->ht_metrics = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 32, 0); |
100 | 0 | if (!ctx->ht_metrics) { |
101 | 0 | flb_plg_error(ctx->ins, "could not initialize hash table for metrics"); |
102 | 0 | return -1; |
103 | 0 | } |
104 | | |
105 | | /* Start HTTP Server */ |
106 | 0 | ret = prom_http_server_start(ctx->http); |
107 | 0 | if (ret == -1) { |
108 | 0 | return -1; |
109 | 0 | } |
110 | | |
111 | 0 | flb_plg_info(ctx->ins, "listening iface=%s tcp_port=%d", |
112 | 0 | ins->host.name, ins->host.port); |
113 | 0 | return 0; |
114 | 0 | } |
115 | | |
116 | | static void append_labels(struct prom_exporter *ctx, struct cmt *cmt) |
117 | 0 | { |
118 | 0 | struct flb_kv *kv; |
119 | 0 | struct mk_list *head; |
120 | |
|
121 | 0 | mk_list_foreach(head, &ctx->kv_labels) { |
122 | 0 | kv = mk_list_entry(head, struct flb_kv, _head); |
123 | 0 | cmt_label_add(cmt, kv->key, kv->val); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | static int hash_store(struct prom_exporter *ctx, struct flb_input_instance *ins, |
128 | | cfl_sds_t buf) |
129 | 0 | { |
130 | 0 | int ret; |
131 | 0 | int len; |
132 | |
|
133 | 0 | len = strlen(ins->name); |
134 | | |
135 | | /* store/override the content into the hash table */ |
136 | 0 | ret = flb_hash_table_add(ctx->ht_metrics, ins->name, len, |
137 | 0 | buf, cfl_sds_len(buf)); |
138 | 0 | if (ret < 0) { |
139 | 0 | return -1; |
140 | 0 | } |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | | static flb_sds_t hash_format_metrics(struct prom_exporter *ctx) |
146 | 0 | { |
147 | 0 | int size = 2048; |
148 | 0 | flb_sds_t buf; |
149 | |
|
150 | 0 | struct mk_list *head; |
151 | 0 | struct flb_hash_table_entry *entry; |
152 | | |
153 | |
|
154 | 0 | buf = flb_sds_create_size(size); |
155 | 0 | if (!buf) { |
156 | 0 | return NULL; |
157 | 0 | } |
158 | | |
159 | | /* Take every hash entry and compose one buffer with the whole content */ |
160 | 0 | mk_list_foreach(head, &ctx->ht_metrics->entries) { |
161 | 0 | entry = mk_list_entry(head, struct flb_hash_table_entry, _head_parent); |
162 | 0 | flb_sds_cat_safe(&buf, entry->val, entry->val_size); |
163 | 0 | } |
164 | |
|
165 | 0 | return buf; |
166 | 0 | } |
167 | | |
168 | | static void cb_prom_flush(struct flb_event_chunk *event_chunk, |
169 | | struct flb_output_flush *out_flush, |
170 | | struct flb_input_instance *ins, void *out_context, |
171 | | struct flb_config *config) |
172 | 0 | { |
173 | 0 | int ret; |
174 | 0 | int add_ts; |
175 | 0 | size_t off = 0; |
176 | 0 | flb_sds_t metrics; |
177 | 0 | cfl_sds_t text = NULL; |
178 | 0 | cfl_sds_t tmp = NULL; |
179 | 0 | struct cmt *cmt; |
180 | 0 | struct prom_exporter *ctx = out_context; |
181 | 0 | int ok = CMT_DECODE_MSGPACK_SUCCESS; |
182 | |
|
183 | 0 | text = flb_sds_create_size(128); |
184 | 0 | if (text == NULL) { |
185 | 0 | flb_plg_debug(ctx->ins, "failed to allocate buffer for text representation of metrics"); |
186 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
187 | 0 | } |
188 | | |
189 | | /* |
190 | | * A new set of metrics has arrived, perform decoding, apply labels, |
191 | | * convert to Prometheus text format and store the output in the |
192 | | * hash table for metrics. |
193 | | * Note that metrics might be concatenated. So, we need to consume |
194 | | * until the end of event_chunk. |
195 | | */ |
196 | 0 | while ((ret = cmt_decode_msgpack_create(&cmt, |
197 | 0 | (char *) event_chunk->data, |
198 | 0 | event_chunk->size, &off)) == ok) { |
199 | | |
200 | | /* append labels set by config */ |
201 | 0 | append_labels(ctx, cmt); |
202 | | |
203 | | /* add timestamp in the output format ? */ |
204 | 0 | if (ctx->add_timestamp) { |
205 | 0 | add_ts = CMT_TRUE; |
206 | 0 | } |
207 | 0 | else { |
208 | 0 | add_ts = CMT_FALSE; |
209 | 0 | } |
210 | | |
211 | | /* convert to text representation */ |
212 | 0 | tmp = cmt_encode_prometheus_create(cmt, add_ts); |
213 | 0 | if (!tmp) { |
214 | 0 | cmt_destroy(cmt); |
215 | 0 | flb_sds_destroy(text); |
216 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
217 | 0 | } |
218 | 0 | ret = flb_sds_cat_safe(&text, tmp, flb_sds_len(tmp)); |
219 | 0 | if (ret != 0) { |
220 | 0 | flb_plg_error(ctx->ins, "could not concatenate text representant coming from: %s", |
221 | 0 | flb_input_name(ins)); |
222 | 0 | cmt_encode_prometheus_destroy(tmp); |
223 | 0 | flb_sds_destroy(text); |
224 | 0 | cmt_destroy(cmt); |
225 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
226 | 0 | } |
227 | 0 | cmt_encode_prometheus_destroy(tmp); |
228 | 0 | cmt_destroy(cmt); |
229 | 0 | } |
230 | | |
231 | 0 | if (cfl_sds_len(text) == 0) { |
232 | 0 | flb_plg_debug(ctx->ins, "context without metrics (empty)"); |
233 | 0 | flb_sds_destroy(text); |
234 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
235 | 0 | } |
236 | | |
237 | | /* register payload of metrics / override previous one */ |
238 | 0 | ret = hash_store(ctx, ins, text); |
239 | 0 | if (ret == -1) { |
240 | 0 | flb_plg_error(ctx->ins, "could not store metrics coming from: %s", |
241 | 0 | flb_input_name(ins)); |
242 | 0 | flb_sds_destroy(text); |
243 | 0 | cmt_destroy(cmt); |
244 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
245 | 0 | } |
246 | 0 | flb_sds_destroy(text); |
247 | | |
248 | | /* retrieve a full copy of all metrics */ |
249 | 0 | metrics = hash_format_metrics(ctx); |
250 | 0 | if (!metrics) { |
251 | 0 | flb_plg_error(ctx->ins, "could not retrieve metrics"); |
252 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
253 | 0 | } |
254 | | |
255 | | /* push new (full) metrics payload */ |
256 | 0 | ret = prom_http_server_mq_push_metrics(ctx->http, |
257 | 0 | (char *) metrics, |
258 | 0 | flb_sds_len(metrics)); |
259 | 0 | flb_sds_destroy(metrics); |
260 | |
|
261 | 0 | if (ret != 0) { |
262 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
263 | 0 | } |
264 | | |
265 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
266 | 0 | } |
267 | | |
268 | | static int cb_prom_exit(void *data, struct flb_config *config) |
269 | 0 | { |
270 | 0 | struct prom_exporter *ctx = data; |
271 | |
|
272 | 0 | if (!ctx) { |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | 0 | if (ctx->ht_metrics) { |
277 | 0 | flb_hash_table_destroy(ctx->ht_metrics); |
278 | 0 | } |
279 | |
|
280 | 0 | flb_kv_release(&ctx->kv_labels); |
281 | 0 | prom_http_server_stop(ctx->http); |
282 | 0 | prom_http_server_destroy(ctx->http); |
283 | 0 | flb_free(ctx); |
284 | |
|
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | | /* Configuration properties map */ |
289 | | static struct flb_config_map config_map[] = { |
290 | | { |
291 | | FLB_CONFIG_MAP_BOOL, "add_timestamp", "false", |
292 | | 0, FLB_TRUE, offsetof(struct prom_exporter, add_timestamp), |
293 | | "Add timestamp to every metric honoring collection time." |
294 | | }, |
295 | | |
296 | | { |
297 | | FLB_CONFIG_MAP_SLIST_1, "add_label", NULL, |
298 | | FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct prom_exporter, add_labels), |
299 | | "TCP port for listening for HTTP connections." |
300 | | }, |
301 | | |
302 | | /* EOF */ |
303 | | {0} |
304 | | }; |
305 | | |
306 | | /* Plugin reference */ |
307 | | struct flb_output_plugin out_prometheus_exporter_plugin = { |
308 | | .name = "prometheus_exporter", |
309 | | .description = "Prometheus Exporter", |
310 | | .cb_init = cb_prom_init, |
311 | | .cb_flush = cb_prom_flush, |
312 | | .cb_exit = cb_prom_exit, |
313 | | .flags = FLB_OUTPUT_NET | FLB_OUTPUT_HTTP_SERVER, |
314 | | .event_type = FLB_OUTPUT_METRICS, |
315 | | .config_map = config_map, |
316 | | }; |