From 0e94b10bdbe1e36dc5b5a3600fd140789a29490c Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 12 Dec 2021 12:45:51 +0900 Subject: [PATCH] out_datadog: support config map (#3985) Signed-off-by: Takahiro Yamashita --- plugins/out_datadog/datadog.c | 74 ++++++++++++++++++++++++++++++ plugins/out_datadog/datadog.h | 2 +- plugins/out_datadog/datadog_conf.c | 66 ++++---------------------- 3 files changed, 84 insertions(+), 58 deletions(-) diff --git a/plugins/out_datadog/datadog.c b/plugins/out_datadog/datadog.c index 804a3c1fbfd..08dc28b01c3 100644 --- a/plugins/out_datadog/datadog.c +++ b/plugins/out_datadog/datadog.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -422,6 +423,76 @@ static int cb_datadog_exit(void *data, struct flb_config *config) return 0; } +static struct flb_config_map config_map[] = { + { + FLB_CONFIG_MAP_STR, "compress", "false", + 0, FLB_FALSE, 0, + "compresses the payload in GZIP format, " + "Datadog supports and recommends setting this to 'gzip'." + }, + { + FLB_CONFIG_MAP_STR, "apikey", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, api_key), + "Datadog API key" + }, + { + FLB_CONFIG_MAP_STR, "dd_service", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, dd_service), + "The human readable name for your service generating the logs " + "- the name of your application or database." + }, + { + FLB_CONFIG_MAP_STR, "dd_source", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, dd_source), + "A human readable name for the underlying technology of your service. " + "For example, 'postgres' or 'nginx'." + }, + { + FLB_CONFIG_MAP_STR, "dd_tags", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, dd_tags), + "The tags you want to assign to your logs in Datadog." + }, + + { + FLB_CONFIG_MAP_STR, "proxy", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, proxy), + "Specify an HTTP Proxy. The expected format of this value is http://host:port. " + "Note that https is not supported yet." + }, + { + FLB_CONFIG_MAP_BOOL, "include_tag_key", "false", + 0, FLB_TRUE, offsetof(struct flb_out_datadog, include_tag_key), + "If enabled, tag is appended to output. " + "The key name is used 'tag_key' property." + }, + { + FLB_CONFIG_MAP_STR, "tag_key", FLB_DATADOG_DEFAULT_TAG_KEY, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, tag_key), + "The key name of tag. If 'include_tag_key' is false, " + "This property is ignored" + }, + { + FLB_CONFIG_MAP_STR, "dd_message_key", NULL, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, dd_message_key), + "By default, the plugin searches for the key 'log' " + "and remap the value to the key 'message'. " + "If the property is set, the plugin will search the property name key." + }, + { + FLB_CONFIG_MAP_STR, "provider", NULL, + 0, FLB_FALSE, 0, + "To activate the remapping, specify configuration flag provider with value 'ecs'" + }, + { + FLB_CONFIG_MAP_STR, "json_date_key", FLB_DATADOG_DEFAULT_TIME_KEY, + 0, FLB_TRUE, offsetof(struct flb_out_datadog, json_date_key), + "Date key name for output." + }, + + /* EOF */ + {0} +}; + struct flb_output_plugin out_datadog_plugin = { .name = "datadog", .description = "Send events to DataDog HTTP Event Collector", @@ -432,6 +503,9 @@ struct flb_output_plugin out_datadog_plugin = { /* Test */ .test_formatter.callback = datadog_format, + /* Config map */ + .config_map = config_map, + /* Plugin flags */ .flags = FLB_OUTPUT_NET | FLB_IO_OPT_TLS, }; diff --git a/plugins/out_datadog/datadog.h b/plugins/out_datadog/datadog.h index 1156b0c586d..f7752ebd52c 100644 --- a/plugins/out_datadog/datadog.h +++ b/plugins/out_datadog/datadog.h @@ -43,7 +43,7 @@ struct flb_out_datadog { /* Proxy */ - const char *proxy; + flb_sds_t proxy; char *proxy_host; int proxy_port; diff --git a/plugins/out_datadog/datadog_conf.c b/plugins/out_datadog/datadog_conf.c index dff85f191ca..b9bab8a4645 100644 --- a/plugins/out_datadog/datadog_conf.c +++ b/plugins/out_datadog/datadog_conf.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "datadog.h" #include "datadog_conf.h" @@ -49,6 +50,13 @@ struct flb_out_datadog *flb_datadog_conf_create(struct flb_output_instance *ins, ctx->ins = ins; ctx->nb_additional_entries = 0; + ret = flb_output_config_map_set(ins, (void *) ctx); + if (ret == -1) { + flb_plg_error(ins, "flb_output_config_map_set failed"); + flb_free(ctx); + return NULL; + } + tmp = flb_output_get_property("proxy", ins); if (tmp) { ret = flb_utils_url_split(tmp, &protocol, &host, &port, &uri); @@ -60,7 +68,6 @@ struct flb_out_datadog *flb_datadog_conf_create(struct flb_output_instance *ins, ctx->proxy_host = host; ctx->proxy_port = atoi(port); - ctx->proxy = tmp; flb_free(protocol); flb_free(port); flb_free(uri); @@ -79,57 +86,30 @@ struct flb_out_datadog *flb_datadog_conf_create(struct flb_output_instance *ins, /* configure URI */ api_key = flb_output_get_property("apikey", ins); - if (api_key) { - ctx->api_key = flb_sds_create(api_key); - } - else { + if (api_key == NULL) { flb_plg_error(ctx->ins, "no ApiKey configuration key defined"); flb_datadog_conf_destroy(ctx); return NULL; } - /* Include Tag key */ - tmp = flb_output_get_property("include_tag_key", ins); - if (tmp) { - ctx->include_tag_key = flb_utils_bool(tmp); - } - else { - ctx->include_tag_key = FLB_FALSE; - } - /* Tag Key */ if (ctx->include_tag_key == FLB_TRUE) { ctx->nb_additional_entries++; - tmp = flb_output_get_property("tag_key", ins); - if (tmp) { - ctx->tag_key = flb_sds_create(tmp); - } - else { - ctx->tag_key = flb_sds_create(FLB_DATADOG_DEFAULT_TAG_KEY); - } } tmp = flb_output_get_property("dd_source", ins); if (tmp) { ctx->nb_additional_entries++; - ctx->dd_source = flb_sds_create(tmp); } tmp = flb_output_get_property("dd_service", ins); if (tmp) { ctx->nb_additional_entries++; - ctx->dd_service = flb_sds_create(tmp); } tmp = flb_output_get_property("dd_tags", ins); if (tmp) { ctx->nb_additional_entries++; - ctx->dd_tags = flb_sds_create(tmp); - } - - tmp = flb_output_get_property("dd_message_key", ins); - if (tmp) { - ctx->dd_message_key = flb_sds_create(tmp); } tmp = flb_output_get_property("provider", ins); @@ -167,13 +147,6 @@ struct flb_out_datadog *flb_datadog_conf_create(struct flb_output_instance *ins, flb_plg_debug(ctx->ins, "port: %i", ctx->port); /* Date tag for JSON output */ - tmp = flb_output_get_property("json_date_key", ins); - if (tmp) { - ctx->json_date_key = flb_sds_create(tmp); - } - else { - ctx->json_date_key = flb_sds_create(FLB_DATADOG_DEFAULT_TIME_KEY); - } ctx->nb_additional_entries++; flb_plg_debug(ctx->ins, "json_date_key: %s", ctx->json_date_key); @@ -230,27 +203,6 @@ int flb_datadog_conf_destroy(struct flb_out_datadog *ctx) if (ctx->uri) { flb_sds_destroy(ctx->uri); } - if (ctx->api_key) { - flb_sds_destroy(ctx->api_key); - } - if (ctx->tag_key) { - flb_sds_destroy(ctx->tag_key); - } - if (ctx->json_date_key) { - flb_sds_destroy(ctx->json_date_key); - } - if (ctx->dd_source) { - flb_sds_destroy(ctx->dd_source); - } - if (ctx->dd_service) { - flb_sds_destroy(ctx->dd_service); - } - if (ctx->dd_tags) { - flb_sds_destroy(ctx->dd_tags); - } - if (ctx->dd_message_key) { - flb_sds_destroy(ctx->dd_message_key); - } if (ctx->upstream) { flb_upstream_destroy(ctx->upstream); }