Skip to content

Commit 652b3fb

Browse files
committed
out_vivo_exporter: change API endpoints to /api/v1/
Signed-off-by: Eduardo Silva <[email protected]>
1 parent f5597d8 commit 652b3fb

File tree

3 files changed

+170
-74
lines changed

3 files changed

+170
-74
lines changed

plugins/out_vivo_exporter/vivo.c

Lines changed: 94 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@
2020
#include <fluent-bit/flb_output_plugin.h>
2121
#include <fluent-bit/flb_kv.h>
2222
#include <fluent-bit/flb_pack.h>
23+
#include <fluent-bit/flb_mp.h>
2324
#include <fluent-bit/flb_log_event_decoder.h>
2425
#include <fluent-bit/flb_log_event_encoder.h>
2526

2627
#include "vivo.h"
2728
#include "vivo_http.h"
2829
#include "vivo_stream.h"
2930

30-
static flb_sds_t format_logs(struct flb_event_chunk *event_chunk, struct flb_config *config)
31+
static flb_sds_t format_logs(struct flb_input_instance *src_ins,
32+
struct flb_event_chunk *event_chunk, struct flb_config *config)
3133
{
32-
struct flb_log_event_decoder log_decoder;
33-
struct flb_log_event log_event;
34+
int len;
3435
int result;
35-
int i;
36+
char *name;
3637
flb_sds_t out_js;
3738
flb_sds_t out_buf = NULL;
3839
msgpack_sbuffer tmp_sbuf;
3940
msgpack_packer tmp_pck;
41+
struct flb_log_event log_event;
42+
struct flb_log_event_decoder log_decoder;
43+
struct flb_mp_map_header mh;
4044

4145
result = flb_log_event_decoder_init(&log_decoder,
4246
(char *) event_chunk->data,
@@ -56,90 +60,118 @@ static flb_sds_t format_logs(struct flb_event_chunk *event_chunk, struct flb_con
5660
msgpack_sbuffer_init(&tmp_sbuf);
5761
msgpack_packer_init(&tmp_pck, &tmp_sbuf, msgpack_sbuffer_write);
5862

63+
/*
64+
* Here is an example of the packaging done for Logs
65+
*
66+
* {
67+
* "source_type": "forward",
68+
* "source_name": "forward.0",
69+
* "tag": "dummy.0",
70+
* "records": [
71+
* {
72+
* "timestamp": 1759591426808913765,
73+
* "metadata": {
74+
* "level": "info"
75+
* },
76+
* "message": "dummy"
77+
* },
78+
* {
79+
* "timestamp": 1759591426908563348,
80+
* "metadata": {
81+
* "level": "debug",
82+
* "service": "auth"
83+
* },
84+
* "message": "dummy"
85+
* }
86+
* ]
87+
* }
88+
*/
89+
90+
msgpack_pack_map(&tmp_pck, 4);
91+
92+
/* source_type: internal type of the plugin */
93+
name = src_ins->p->name;
94+
len = strlen(name);
95+
96+
msgpack_pack_str(&tmp_pck, 11);
97+
msgpack_pack_str_body(&tmp_pck, "source_type", 11);
98+
msgpack_pack_str(&tmp_pck, len);
99+
msgpack_pack_str_body(&tmp_pck, name, len);
100+
101+
/* source_name: internal name or alias set by the user */
102+
name = (char *) flb_input_name(src_ins);
103+
len = strlen(name);
104+
msgpack_pack_str(&tmp_pck, 11);
105+
msgpack_pack_str_body(&tmp_pck, "source_name", 11);
106+
msgpack_pack_str(&tmp_pck, len);
107+
msgpack_pack_str_body(&tmp_pck, name, len);
108+
109+
/* tag */
110+
msgpack_pack_str(&tmp_pck, 3);
111+
msgpack_pack_str_body(&tmp_pck, "tag", 3);
112+
msgpack_pack_str(&tmp_pck, flb_sds_len(event_chunk->tag));
113+
msgpack_pack_str_body(&tmp_pck, event_chunk->tag, flb_sds_len(event_chunk->tag));
114+
115+
/* records */
116+
msgpack_pack_str(&tmp_pck, 7);
117+
msgpack_pack_str_body(&tmp_pck, "records", 7);
118+
119+
flb_mp_array_header_init(&mh, &tmp_pck);
120+
59121
while ((result = flb_log_event_decoder_next(
60122
&log_decoder,
61123
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
124+
125+
flb_mp_array_header_append(&mh);
126+
62127
/*
63-
* If the caller specified FLB_PACK_JSON_DATE_FLUENT, we format the data
64-
* by using the following structure:
65-
*
66-
* [[TIMESTAMP, {"_tag": "...", ...MORE_METADATA}], {RECORD CONTENT}]
128+
* [[TIMESTAMP, {"....": "...", ...MORE_METADATA}], {RECORD CONTENT}]
67129
*/
68130
msgpack_pack_array(&tmp_pck, 2);
69131
msgpack_pack_array(&tmp_pck, 2);
70132
msgpack_pack_uint64(&tmp_pck, flb_time_to_nanosec(&log_event.timestamp));
71133

72-
/* add tag only */
73-
msgpack_pack_map(&tmp_pck, 1 + log_event.metadata->via.map.size);
74-
75-
msgpack_pack_str(&tmp_pck, 4);
76-
msgpack_pack_str_body(&tmp_pck, "_tag", 4);
77-
78-
msgpack_pack_str(&tmp_pck, flb_sds_len(event_chunk->tag));
79-
msgpack_pack_str_body(&tmp_pck, event_chunk->tag, flb_sds_len(event_chunk->tag));
80-
81-
/* Append remaining keys/values */
82-
for (i = 0;
83-
i < log_event.metadata->via.map.size;
84-
i++) {
85-
msgpack_pack_object(&tmp_pck,
86-
log_event.metadata->via.map.ptr[i].key);
87-
msgpack_pack_object(&tmp_pck,
88-
log_event.metadata->via.map.ptr[i].val);
89-
}
134+
/* pack metadata */
135+
msgpack_pack_object(&tmp_pck, *log_event.metadata);
90136

91137
/* pack the remaining content */
92-
msgpack_pack_map(&tmp_pck, log_event.body->via.map.size);
93-
94-
/* Append remaining keys/values */
95-
for (i = 0;
96-
i < log_event.body->via.map.size;
97-
i++) {
98-
msgpack_pack_object(&tmp_pck,
99-
log_event.body->via.map.ptr[i].key);
100-
msgpack_pack_object(&tmp_pck,
101-
log_event.body->via.map.ptr[i].val);
102-
}
103-
104-
/* Concatenate by using break lines */
105-
out_js = flb_msgpack_raw_to_json_sds(tmp_sbuf.data, tmp_sbuf.size,
106-
config->json_escape_unicode);
107-
if (!out_js) {
108-
flb_sds_destroy(out_buf);
109-
msgpack_sbuffer_destroy(&tmp_sbuf);
110-
flb_log_event_decoder_destroy(&log_decoder);
111-
return NULL;
112-
}
113-
114-
/*
115-
* One map record has been converted, now append it to the
116-
* outgoing out_buf sds variable.
117-
*/
118-
flb_sds_cat_safe(&out_buf, out_js, flb_sds_len(out_js));
119-
flb_sds_cat_safe(&out_buf, "\n", 1);
120-
121-
flb_sds_destroy(out_js);
122-
msgpack_sbuffer_clear(&tmp_sbuf);
138+
msgpack_pack_object(&tmp_pck, *log_event.body);
123139
}
124140

141+
flb_mp_array_header_end(&mh);
142+
125143
/* Release the unpacker */
126144
flb_log_event_decoder_destroy(&log_decoder);
127145

146+
/* Convert the complete msgpack structure to JSON */
147+
out_js = flb_msgpack_raw_to_json_sds(tmp_sbuf.data, tmp_sbuf.size,
148+
config->json_escape_unicode);
149+
128150
msgpack_sbuffer_destroy(&tmp_sbuf);
129151

130-
return out_buf;
152+
/* append a newline */
153+
flb_sds_cat_safe(&out_js, "\n", 1);
154+
155+
if (!out_js) {
156+
flb_sds_destroy(out_buf);
157+
return NULL;
158+
}
159+
160+
/* Replace out_buf with the complete JSON */
161+
flb_sds_destroy(out_buf);
162+
return out_js;
131163
}
132164

133165
static int logs_event_chunk_append(struct vivo_exporter *ctx,
166+
struct flb_input_instance *src_ins,
134167
struct flb_event_chunk *event_chunk,
135168
struct flb_config *config)
136169
{
137170
size_t len;
138171
flb_sds_t json;
139172
struct vivo_stream_entry *entry;
140173

141-
142-
json = format_logs(event_chunk, config);
174+
json = format_logs(src_ins, event_chunk, config);
143175
if (!json) {
144176
flb_plg_error(ctx->ins, "cannot convert logs chunk to JSON");
145177
return -1;
@@ -207,6 +239,7 @@ static int cb_vivo_init(struct flb_output_instance *ins,
207239
return -1;
208240
}
209241
ctx->ins = ins;
242+
ctx->config = config;
210243

211244
ret = flb_output_config_map_set(ins, (void *) ctx);
212245
if (ret == -1) {
@@ -272,7 +305,7 @@ static void cb_vivo_flush(struct flb_event_chunk *event_chunk,
272305
}
273306
#endif
274307
if (event_chunk->type == FLB_EVENT_TYPE_LOGS) {
275-
ret = logs_event_chunk_append(ctx, event_chunk, config);
308+
ret = logs_event_chunk_append(ctx, ins, event_chunk, config);
276309
}
277310
else if (event_chunk->type == FLB_EVENT_TYPE_TRACES) {
278311
ret = metrics_traces_event_chunk_append(ctx, ctx->stream_traces, event_chunk, config);

plugins/out_vivo_exporter/vivo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <fluent-bit/flb_output_plugin.h>
2424
#include <fluent-bit/flb_ring_buffer.h>
2525

26+
struct flb_config;
27+
2628
#define VIVO_RING_BUFFER_SIZE 10
2729

2830
/* Plugin context */
@@ -40,6 +42,7 @@ struct vivo_exporter {
4042

4143
/* instance context */
4244
struct flb_output_instance *ins;
45+
struct flb_config *config;
4346
};
4447

4548
#endif

plugins/out_vivo_exporter/vivo_http.c

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
#include <fluent-bit/flb_output_plugin.h>
2121
#include <fluent-bit/flb_http_server.h>
22+
#include <fluent-bit/flb_config.h>
23+
#include <fluent-bit/flb_pack.h>
24+
#include <fluent-bit/flb_metrics_exporter.h>
25+
26+
#include <cmetrics/cmt_encode_msgpack.h>
2227

2328
#include "vivo.h"
2429
#include "vivo_http.h"
@@ -64,14 +69,8 @@ static int stream_get_uri_properties(mk_request_t *request,
6469
return 0;
6570
}
6671

67-
static void headers_set(mk_request_t *request, struct vivo_stream *vs)
72+
static void headers_set_common(struct vivo_exporter *ctx, mk_request_t *request)
6873
{
69-
struct vivo_exporter *ctx;
70-
71-
72-
/* parent context */
73-
ctx = vs->parent;
74-
7574
/* content type */
7675
mk_http_header(request,
7776
VIVO_CONTENT_TYPE, sizeof(VIVO_CONTENT_TYPE) - 1,
@@ -90,13 +89,25 @@ static void headers_set(mk_request_t *request, struct vivo_stream *vs)
9089
sizeof("Access-Control-Allow-Headers") - 1,
9190
"Origin, X-Requested-With, Content-Type, Accept",
9291
sizeof("Origin, X-Requested-With, Content-Type, Accept") - 1);
92+
}
93+
}
9394

95+
static void headers_set(mk_request_t *request, struct vivo_stream *vs)
96+
{
97+
struct vivo_exporter *ctx;
98+
99+
100+
/* parent context */
101+
ctx = vs->parent;
102+
103+
headers_set_common(ctx, request);
104+
105+
if (ctx->http_cors_allow_origin) {
94106
mk_http_header(request,
95107
"Access-Control-Expose-Headers",
96108
sizeof("Access-Control-Expose-Headers") - 1,
97109
"vivo-stream-start-id, vivo-stream-end-id",
98110
sizeof("vivo-stream-start-id, vivo-stream-end-id") - 1);
99-
100111
}
101112
}
102113

@@ -159,7 +170,7 @@ static void serve_content(mk_request_t *request, struct vivo_stream *vs)
159170
flb_sds_destroy(str_end);
160171
}
161172

162-
/* HTTP endpoint: /logs */
173+
/* HTTP endpoint: /api/v1/logs */
163174
static void cb_logs(mk_request_t *request, void *data)
164175
{
165176
struct vivo_exporter *ctx;
@@ -170,7 +181,7 @@ static void cb_logs(mk_request_t *request, void *data)
170181
mk_http_done(request);
171182
}
172183

173-
/* HTTP endpoint: /metrics */
184+
/* HTTP endpoint: /api/v1/metrics */
174185
static void cb_metrics(mk_request_t *request, void *data)
175186
{
176187
struct vivo_exporter *ctx;
@@ -181,6 +192,7 @@ static void cb_metrics(mk_request_t *request, void *data)
181192
mk_http_done(request);
182193
}
183194

195+
/* HTTP endpoint: /api/v1/traces */
184196
static void cb_traces(mk_request_t *request, void *data)
185197
{
186198
struct vivo_exporter *ctx;
@@ -191,6 +203,53 @@ static void cb_traces(mk_request_t *request, void *data)
191203
mk_http_done(request);
192204
}
193205

206+
/* HTTP endpoint: /api/v1/internal/metrics */
207+
static void cb_internal_metrics(mk_request_t *request, void *data)
208+
{
209+
int ret;
210+
char *mp_buf = NULL;
211+
size_t mp_size = 0;
212+
flb_sds_t json = NULL;
213+
struct cmt *cmt = NULL;
214+
struct vivo_exporter *ctx;
215+
216+
ctx = (struct vivo_exporter *) data;
217+
218+
cmt = flb_me_get_cmetrics(ctx->config);
219+
if (!cmt) {
220+
mk_http_status(request, 500);
221+
mk_http_done(request);
222+
return;
223+
}
224+
225+
ret = cmt_encode_msgpack_create(cmt, &mp_buf, &mp_size);
226+
if (ret != 0) {
227+
cmt_destroy(cmt);
228+
mk_http_status(request, 500);
229+
mk_http_done(request);
230+
return;
231+
}
232+
233+
json = flb_msgpack_raw_to_json_sds(mp_buf, mp_size,
234+
ctx->config->json_escape_unicode);
235+
236+
cmt_encode_msgpack_destroy(mp_buf);
237+
cmt_destroy(cmt);
238+
239+
if (!json) {
240+
mk_http_status(request, 500);
241+
mk_http_done(request);
242+
return;
243+
}
244+
245+
mk_http_status(request, 200);
246+
headers_set_common(ctx, request);
247+
mk_http_send(request, json, flb_sds_len(json), NULL);
248+
mk_http_done(request);
249+
250+
flb_sds_destroy(json);
251+
}
252+
194253
/* HTTP endpoint: / (root) */
195254
static void cb_root(mk_request_t *request, void *data)
196255
{
@@ -236,9 +295,10 @@ struct vivo_http *vivo_http_server_create(struct vivo_exporter *ctx,
236295
ph->vid = vid;
237296

238297
/* Set HTTP URI callbacks */
239-
mk_vhost_handler(ph->ctx, vid, "/logs", cb_logs, ctx);
240-
mk_vhost_handler(ph->ctx, vid, "/metrics", cb_metrics, ctx);
241-
mk_vhost_handler(ph->ctx, vid, "/traces", cb_traces, ctx);
298+
mk_vhost_handler(ph->ctx, vid, "/api/v1/logs", cb_logs, ctx);
299+
mk_vhost_handler(ph->ctx, vid, "/api/v1/metrics", cb_metrics, ctx);
300+
mk_vhost_handler(ph->ctx, vid, "/api/v1/traces", cb_traces, ctx);
301+
mk_vhost_handler(ph->ctx, vid, "/api/v1/internal/metrics", cb_internal_metrics, ctx);
242302
mk_vhost_handler(ph->ctx, vid, "/", cb_root, NULL);
243303

244304
return ph;

0 commit comments

Comments
 (0)