Skip to content

Commit

Permalink
stackdriver: add cloud_logging_base_url
Browse files Browse the repository at this point in the history
Add the `Cloud_Logging_Base_Url` configuration option to the
`out_stackdriver` plugin.

Signed-off-by: braydonk <[email protected]>
  • Loading branch information
braydonk committed Dec 30, 2024
1 parent 44a2d0d commit 34d7c0c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion plugins/out_stackdriver/stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}
};
Expand Down
5 changes: 5 additions & 0 deletions plugins/out_stackdriver/stackdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 41 additions & 6 deletions plugins/out_stackdriver/stackdriver_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 34d7c0c

Please sign in to comment.