out_file: add support for metrics
#include <fluent-bit/flb_time.h>
#include <msgpack.h>
+#include <cmetrics/cmetrics.h>
+#include <cmetrics/cmt_encode_text.h>
+#include <cmetrics/cmt_decode_msgpack.h>
+
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -318,9 +322,34 @@ static int plain_output(FILE *fp, msgpack_object *obj, size_t alloc_size)
return 0;
}
+static void print_metrics_text(struct flb_output_instance *ins,
+ FILE *fp,
+ const void *data, size_t bytes)
+{
+ int ret;
+ cmt_sds_t text;
+ struct cmt *cmt = NULL;
+
+ /* get cmetrics context */
+ ret = cmt_decode_msgpack(&cmt, (char *) data, bytes);
+ if (ret != 0) {
+ flb_plg_error(ins, "could not process metrics payload");
+ return;
+ }
+
+ /* convert to text representation */
+ text = cmt_encode_text_create(cmt);
+
+ /* destroy cmt context */
+ cmt_destroy(cmt);
+
+ fprintf(fp, "%s", text);
+ cmt_encode_text_destroy(text);
+}
+
static void cb_file_flush(const void *data, size_t bytes,
const char *tag, int tag_len,
- struct flb_input_instance *i_ins,
+ struct flb_input_instance *ins,
void *out_context,
struct flb_config *config)
{
@@ -339,7 +368,6 @@ static void cb_file_flush(const void *data, size_t bytes,
msgpack_object *obj;
struct flb_file_conf *ctx = out_context;
struct flb_time tm;
- (void) i_ins;
(void) config;
/* Set the right output file */
@@ -376,6 +404,13 @@ static void cb_file_flush(const void *data, size_t bytes,
*/
file_pos = ftell(fp);
+ /* Check if the event type is metrics, handle the payload differently */
+ if (flb_input_event_type_is_metric(ins)) {
+ print_metrics_text(ctx->ins, fp, (char *) data, bytes);
+ fclose(fp);
+ FLB_OUTPUT_RETURN(FLB_OK);
+ }
+
tag_buf = flb_malloc(tag_len + 1);
if (!tag_buf) {
flb_errno();
@@ -385,6 +420,7 @@ static void cb_file_flush(const void *data, size_t bytes,
memcpy(tag_buf, tag, tag_len);
tag_buf[tag_len] = '\0';
+
/*
* Msgpack output format used to create unit tests files, useful for
* Fluent Bit developers.
@@ -535,6 +571,7 @@ struct flb_output_plugin out_file_plugin = {
.cb_init = cb_file_init,
.cb_flush = cb_file_flush,
.cb_exit = cb_file_exit,
- .config_map = config_map,
.flags = 0,
+ .event_type = FLB_OUTPUT_LOGS | FLB_OUTPUT_METRICS,
+ .config_map = config_map,
};
