Skip to content

Commit

Permalink
out_datadog: support config map (#3985)
Browse files Browse the repository at this point in the history
Signed-off-by: Takahiro Yamashita <[email protected]>
  • Loading branch information
nokute78 authored Dec 12, 2021
1 parent cc6ebaf commit 0e94b10
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 58 deletions.
74 changes: 74 additions & 0 deletions plugins/out_datadog/datadog.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_gzip.h>
#include <fluent-bit/flb_config_map.h>

#include <msgpack.h>

Expand Down Expand Up @@ -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",
Expand All @@ -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,
};
2 changes: 1 addition & 1 deletion plugins/out_datadog/datadog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
struct flb_out_datadog {

/* Proxy */
const char *proxy;
flb_sds_t proxy;
char *proxy_host;
int proxy_port;

Expand Down
66 changes: 9 additions & 57 deletions plugins/out_datadog/datadog_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_config_map.h>

#include "datadog.h"
#include "datadog_conf.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 0e94b10

Please sign in to comment.