From 4bae30be213ddad0df62188dceda93bddd3d7a5c Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:38:30 -0500 Subject: [PATCH] stackdriver: add cloud_logging_base_url (#9779) Add the `Cloud_Logging_Base_Url` configuration option to the `out_stackdriver` plugin. Signed-off-by: braydonk --- plugins/out_stackdriver/stackdriver.c | 7 +++- plugins/out_stackdriver/stackdriver.h | 5 +++ plugins/out_stackdriver/stackdriver_conf.c | 47 +++++++++++++++++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index 99ef657a0b4..4e1395f2d36 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -1243,7 +1243,7 @@ static int cb_stackdriver_init(struct flb_output_instance *ins, pthread_mutex_init(&ctx->token_mutex, NULL); /* Create Upstream context for Stackdriver Logging (no oauth2 service) */ - ctx->u = flb_upstream_create_url(config, FLB_STD_WRITE_URL, + ctx->u = flb_upstream_create_url(config, ctx->cloud_logging_write_url, io_flags, ins->tls); ctx->metadata_u = flb_upstream_create_url(config, ctx->metadata_server, FLB_IO_TCP, NULL); @@ -3241,6 +3241,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_stackdriver, test_log_entry_format), "Test log entry format" }, + { + FLB_CONFIG_MAP_STR, "cloud_logging_base_url", (char *)NULL, + 0, FLB_TRUE, offsetof(struct flb_stackdriver, cloud_logging_base_url), + "The base Cloud Logging API URL to use for the /v2/entries:write API request. Default: https://logging.googleapis.com" + }, /* EOF */ {0} }; diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h index 66462cf4fab..c6f18d932ea 100644 --- a/plugins/out_stackdriver/stackdriver.h +++ b/plugins/out_stackdriver/stackdriver.h @@ -40,6 +40,7 @@ /* Stackdriver Logging 'write' end-point */ #define FLB_STD_WRITE_URI "/v2/entries:write" +#define FLB_STD_WRITE_URI_SIZE 17 #define FLB_STD_WRITE_URL "https://logging.googleapis.com" FLB_STD_WRITE_URI /* Timestamp format */ @@ -213,6 +214,10 @@ struct flb_stackdriver { /* the key to extract unstructured text payload from */ flb_sds_t text_payload_key; + /* config key to allow an alternate Cloud Logging URL */ + flb_sds_t cloud_logging_base_url; + flb_sds_t cloud_logging_write_url; + #ifdef FLB_HAVE_METRICS /* metrics */ struct cmt_counter *cmt_successful_requests; diff --git a/plugins/out_stackdriver/stackdriver_conf.c b/plugins/out_stackdriver/stackdriver_conf.c index 89582623d40..9c0dd8b17b9 100644 --- a/plugins/out_stackdriver/stackdriver_conf.c +++ b/plugins/out_stackdriver/stackdriver_conf.c @@ -263,6 +263,8 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * struct flb_stackdriver *ctx; size_t http_request_key_size; struct cmt_histogram_buckets *buckets; + flb_sds_t cloud_logging_base_url_str; + size_t cloud_logging_base_url_size, cloud_logging_write_url_size; /* Allocate config context */ ctx = flb_calloc(1, sizeof(struct flb_stackdriver)); @@ -272,7 +274,7 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * } ctx->ins = ins; ctx->config = config; - + ret = flb_output_config_map_set(ins, (void *)ctx); if (ret == -1) { flb_plg_error(ins, "unable to load configuration"); @@ -460,6 +462,36 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * } } + if (ctx->cloud_logging_base_url) { + /* + * An alternate base URL was specified in the config. To avoid the confusion of a user + * not knowing whether the trailing `/` should be present, check whether the user + * provided it and remove it if it is. + */ + cloud_logging_base_url_size = flb_sds_len(ctx->cloud_logging_base_url); + if (FLB_SDS_HEADER( + ctx->cloud_logging_base_url + )->buf[cloud_logging_base_url_size-1] == '/') { + cloud_logging_base_url_size -= 1; + } + cloud_logging_base_url_str = flb_sds_create_size(cloud_logging_base_url_size); + + /* Note: The size calculated from `flb_sds_len` does not include the null terminator character, + * `size` argument for `flb_sds_snprintf` needs to be the size including the null terminator. + * Hence the +1 added to each size argument here. + */ + flb_sds_snprintf(&cloud_logging_base_url_str, cloud_logging_base_url_size+1, + "%s", ctx->cloud_logging_base_url); + cloud_logging_write_url_size = cloud_logging_base_url_size + FLB_STD_WRITE_URI_SIZE; + ctx->cloud_logging_write_url = flb_sds_create_size(cloud_logging_write_url_size); + flb_sds_snprintf(&ctx->cloud_logging_write_url, cloud_logging_write_url_size+1, + "%s%s", cloud_logging_base_url_str, FLB_STD_WRITE_URI); + + flb_sds_destroy(cloud_logging_base_url_str); + } else { + ctx->cloud_logging_write_url = flb_sds_create(FLB_STD_WRITE_URL); + } + set_resource_type(ctx); if (resource_api_has_required_labels(ctx) == FLB_FALSE) { @@ -473,7 +505,7 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * } } - else if (ctx->resource_type == RESOURCE_TYPE_GENERIC_NODE + else if (ctx->resource_type == RESOURCE_TYPE_GENERIC_NODE || ctx->resource_type == RESOURCE_TYPE_GENERIC_TASK) { if (ctx->location == NULL) { @@ -513,7 +545,6 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * } } - if (ctx->tag_prefix == NULL && ctx->resource_type == RESOURCE_TYPE_K8S) { /* allocate the flb_sds_t to tag_prefix_k8s so we can safely deallocate it */ ctx->tag_prefix_k8s = flb_sds_create(ctx->resource); @@ -639,7 +670,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx) flb_sds_destroy(ctx->node_name); flb_sds_destroy(ctx->local_resource_id); } - + if (ctx->metadata_server_auth) { flb_sds_destroy(ctx->zone); flb_sds_destroy(ctx->instance_id); @@ -660,15 +691,19 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx) if (ctx->regex) { flb_regex_destroy(ctx->regex); } - + if (ctx->project_id) { flb_sds_destroy(ctx->project_id); } - + if (ctx->tag_prefix_k8s) { flb_sds_destroy(ctx->tag_prefix_k8s); } + if (ctx->cloud_logging_write_url) { + flb_sds_destroy(ctx->cloud_logging_write_url); + } + flb_kv_release(&ctx->config_labels); flb_kv_release(&ctx->resource_labels_kvs); flb_free(ctx);