Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_mqtt: add test code and support payload_key #7872

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion plugins/in_mqtt/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ static int in_mqtt_exit(void *data, struct flb_config *config)

/* Configuration properties map */
static struct flb_config_map config_map[] = {

{
FLB_CONFIG_MAP_STR, "payload_key", NULL,
0, FLB_TRUE, offsetof(struct flb_in_mqtt_config, payload_key),
"Key where the payload will be preserved"
},
/* EOF */
{0}
};
Expand Down
3 changes: 3 additions & 0 deletions plugins/in_mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef FLB_IN_MQTT_H
#define FLB_IN_MQTT_H

#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_log_event_encoder.h>

#define MQTT_MSGP_BUF_SIZE 8192
Expand All @@ -28,6 +29,8 @@ struct flb_in_mqtt_config {
char *listen; /* Listen interface */
char *tcp_port; /* TCP Port */

flb_sds_t payload_key; /* payload key */

int msgp_len; /* msgpack data length */
char msgp[MQTT_MSGP_BUF_SIZE]; /* msgpack static buffer */
struct flb_input_instance *ins; /* plugin input instance */
Expand Down
8 changes: 8 additions & 0 deletions plugins/in_mqtt/mqtt_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ struct flb_in_mqtt_config *mqtt_config_init(struct flb_input_instance *ins)
{
char tmp[16];
struct flb_in_mqtt_config *config;
int ret;

config = flb_calloc(1, sizeof(struct flb_in_mqtt_config));
if (!config) {
flb_errno();
return NULL;
}

ret = flb_input_config_map_set(ins, (void*) config);
if (ret == -1) {
flb_plg_error(ins, "could not initialize config map");
flb_free(config);
return NULL;
}

config->log_encoder = flb_log_event_encoder_create(
FLB_LOG_EVENT_FORMAT_DEFAULT);

Expand Down
11 changes: 11 additions & 0 deletions plugins/in_mqtt/mqtt_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ static int mqtt_data_append(char *topic, size_t topic_len,
FLB_LOG_EVENT_STRING_VALUE(topic, topic_len));
}

if (ctx->payload_key) {
flb_log_event_encoder_append_body_string_length(ctx->log_encoder, flb_sds_len(ctx->payload_key));
flb_log_event_encoder_append_body_string_body(ctx->log_encoder, ctx->payload_key,
flb_sds_len(ctx->payload_key));
flb_log_event_encoder_body_begin_map(ctx->log_encoder);
}

/* Re-pack original KVs */
for (i = 0;
i < root.via.map.size &&
Expand All @@ -182,6 +189,10 @@ static int mqtt_data_append(char *topic, size_t topic_len,
FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE(&root.via.map.ptr[i].val));
}

if (ctx->payload_key) {
flb_log_event_encoder_body_commit_map(ctx->log_encoder);
}

if (ret == FLB_EVENT_ENCODER_SUCCESS) {
ret = flb_log_event_encoder_commit_record(ctx->log_encoder);
}
Expand Down
1 change: 1 addition & 0 deletions tests/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if(FLB_OUT_LIB)
FLB_RT_TEST(FLB_IN_DUMMY "in_dummy.c")
FLB_RT_TEST(FLB_IN_HTTP "in_http.c")
FLB_RT_TEST(FLB_IN_ELASTICSEARCH "in_elasticsearch.c")
FLB_RT_TEST(FLB_IN_MQTT "in_mqtt.c")
FLB_RT_TEST(FLB_IN_OPENTELEMETRY "in_opentelemetry.c")
FLB_RT_TEST(FLB_IN_RANDOM "in_random.c")
FLB_RT_TEST(FLB_IN_STATSD "in_statsd.c")
Expand Down
Loading