From 083785777fe0fc770919136b60d1873cb6c0ae50 Mon Sep 17 00:00:00 2001 From: shuaichen Date: Mon, 22 Jul 2024 08:28:01 -0700 Subject: [PATCH] out_stackdriver: Add latency metric for write log entries HTTP request. Signed-off-by: shuaichen --- plugins/out_stackdriver/stackdriver.c | 11 +++++++++++ plugins/out_stackdriver/stackdriver.h | 3 +++ plugins/out_stackdriver/stackdriver_conf.c | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index 60b66c48ce4..0984e96da8b 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -2829,6 +2829,9 @@ static void cb_stackdriver_flush(struct flb_event_chunk *event_chunk, struct flb_connection *u_conn; struct flb_http_client *c; int compressed = FLB_FALSE; + uint64_t write_entries_start = 0; + uint64_t write_entries_end = 0; + float write_entries_latency = 0.0; #ifdef FLB_HAVE_METRICS char *name = (char *) flb_output_name(ctx->ins); uint64_t ts = cfl_time_now(); @@ -2923,9 +2926,14 @@ static void cb_stackdriver_flush(struct flb_event_chunk *event_chunk, flb_http_set_content_encoding_gzip(c); } + write_entries_start = cfl_time_now(); + /* Send HTTP request */ ret = flb_http_do(c, &b_sent); + write_entries_end = cfl_time_now(); + write_entries_latency = (float)(write_entries_end - write_entries_start) / 1000000000.0; + /* validate response */ if (ret != 0) { flb_plg_warn(ctx->ins, "http_do=%i", ret); @@ -2994,6 +3002,9 @@ static void cb_stackdriver_flush(struct flb_event_chunk *event_chunk, #ifdef FLB_HAVE_METRICS if (ret_code == FLB_OK) { cmt_counter_inc(ctx->cmt_successful_requests, ts, 1, (char *[]) {name}); + if (write_entries_latency > 0.0) { + cmt_histogram_observe(ctx->cmt_write_entries_latency, ts, write_entries_latency, 1, (char *[]) {name}); + } add_record_metrics(ctx, ts, (int) event_chunk->total_events, 200, 0); /* OLD api */ diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h index 76f5a7598ea..fa5cb2e2b10 100644 --- a/plugins/out_stackdriver/stackdriver.h +++ b/plugins/out_stackdriver/stackdriver.h @@ -27,6 +27,8 @@ #include #include +#include + /* refresh token every 50 minutes */ #define FLB_STD_TOKEN_REFRESH 3000 @@ -218,6 +220,7 @@ struct flb_stackdriver { struct cmt_counter *cmt_requests_total; struct cmt_counter *cmt_proc_records_total; struct cmt_counter *cmt_retried_records_total; + struct cmt_histogram *cmt_write_entries_latency; #endif /* plugin instance */ diff --git a/plugins/out_stackdriver/stackdriver_conf.c b/plugins/out_stackdriver/stackdriver_conf.c index b7de32a0a95..89582623d40 100644 --- a/plugins/out_stackdriver/stackdriver_conf.c +++ b/plugins/out_stackdriver/stackdriver_conf.c @@ -262,6 +262,7 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * const char *backwards_compatible_env_var; struct flb_stackdriver *ctx; size_t http_request_key_size; + struct cmt_histogram_buckets *buckets; /* Allocate config context */ ctx = flb_calloc(1, sizeof(struct flb_stackdriver)); @@ -559,6 +560,16 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * "Total number of retried records.", 2, (char *[]) {"status", "name"}); + buckets = cmt_histogram_buckets_create(7, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0); + ctx->cmt_write_entries_latency = cmt_histogram_create(ins->cmt, + "fluentbit", + "stackdriver", + "write_entries_latency", + "Latency of Cloud Logging WriteLogEntries HTTP request.", + buckets, + 1, (char *[]) {"name"}); + + /* OLD api */ flb_metrics_add(FLB_STACKDRIVER_SUCCESSFUL_REQUESTS, "stackdriver_successful_requests", ctx->ins->metrics); @@ -599,7 +610,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx) } flb_free(ctx->creds); } - + if (ctx->env) { if (ctx->env->creds_file) { flb_sds_destroy(ctx->env->creds_file); @@ -620,7 +631,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx) if (ctx->metadata_server) { flb_sds_destroy(ctx->metadata_server); } - + if (ctx->resource_type == RESOURCE_TYPE_K8S){ flb_sds_destroy(ctx->namespace_name); flb_sds_destroy(ctx->pod_name);