Skip to content

Commit b0b655d

Browse files
committed
metrics_exporter: push internal metrics to new queue for /api/v2
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 6aae15e commit b0b655d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

include/fluent-bit/flb_http_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#ifndef FLB_HTTP_SERVER_H
2424
#define FLB_HTTP_SERVER_H
25+
2526
#include "http_server/flb_hs.h"
2627
#include "http_server/flb_hs_utils.h"
2728
#include "http_server/flb_hs_endpoints.h"

include/fluent-bit/http_server/flb_hs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
struct flb_hs_buf {
3333
int users;
3434
flb_sds_t data;
35-
char *raw_data;
35+
void *raw_data;
3636
size_t raw_size;
3737
struct mk_list _head;
3838
};
@@ -41,6 +41,7 @@ struct flb_hs {
4141
mk_ctx_t *ctx; /* Monkey HTTP Context */
4242
int vid; /* Virtual Host ID */
4343
int qid_metrics; /* Metrics Message Queue ID */
44+
int qid_metrics_v2; /* Metrics Message Queue ID for /api/v2 */
4445
int qid_storage; /* Storage Message Queue ID */
4546
int qid_health; /* health Message Queue ID */
4647

@@ -56,6 +57,7 @@ struct flb_hs *flb_hs_create(const char *listen, const char *tcp_port,
5657
struct flb_config *config);
5758
int flb_hs_push_health_metrics(struct flb_hs *hs, void *data, size_t size);
5859
int flb_hs_push_pipeline_metrics(struct flb_hs *hs, void *data, size_t size);
60+
int flb_hs_push_metrics(struct flb_hs *hs, void *data, size_t size);
5961
int flb_hs_push_storage_metrics(struct flb_hs *hs, void *data, size_t size);
6062

6163
int flb_hs_destroy(struct flb_hs *ctx);

src/flb_metrics_exporter.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,17 @@ static int collect_outputs(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
148148

149149
static int collect_metrics(struct flb_me *me)
150150
{
151+
int ret;
151152
int keys;
153+
char *buf_data;
154+
size_t buf_size;
152155
struct flb_config *ctx = me->config;
156+
struct cmt *cmt;
153157

158+
/*
159+
* msgpack buffer for old-style /v1/metrics
160+
* ----------------------------------------
161+
*/
154162
msgpack_sbuffer mp_sbuf;
155163
msgpack_packer mp_pck;
156164

@@ -166,15 +174,36 @@ static int collect_metrics(struct flb_me *me)
166174
collect_filters(&mp_sbuf, &mp_pck, me->config);
167175
collect_outputs(&mp_sbuf, &mp_pck, me->config);
168176

177+
/*
178+
* If the built-in HTTP server is enabled, push metrics and health checks
179+
* ---------------------------------------------------------------------
180+
*/
181+
169182
#ifdef FLB_HAVE_HTTP_SERVER
170183
if (ctx->http_server == FLB_TRUE) {
171-
/* v1 metrics (old) */
184+
/* /v1/metrics (old) */
172185
flb_hs_push_pipeline_metrics(ctx->http_ctx, mp_sbuf.data, mp_sbuf.size);
186+
187+
/* /v1/health */
173188
if (ctx->health_check == FLB_TRUE) {
174189
flb_hs_push_health_metrics(ctx->http_ctx, mp_sbuf.data, mp_sbuf.size);
175190
}
191+
192+
/* /v2/metrics: retrieve a CMetrics context with internal metrics */
193+
cmt = flb_me_get_cmetrics(ctx);
194+
if (cmt) {
195+
/* encode context to msgpack */
196+
ret = cmt_encode_msgpack_create(cmt, &buf_data, &buf_size);
197+
if (ret == 0) {
198+
flb_hs_push_metrics(ctx->http_ctx, buf_data, buf_size);
199+
cmt_encode_msgpack_destroy(buf_data);
200+
}
201+
cmt_destroy(cmt);
202+
}
176203
}
177204
#endif
205+
206+
/* destroy msgpack buffer for old-style /v1/metrics */
178207
msgpack_sbuffer_destroy(&mp_sbuf);
179208

180209

0 commit comments

Comments
 (0)