From ea5b9376077be1b65c729252f0591c1ec8dd678a Mon Sep 17 00:00:00 2001 From: 0Delta <0deltast@gmail.com> Date: Mon, 25 Oct 2021 22:24:10 +0900 Subject: [PATCH] out_stackdriver: support stackdriver trace span (#4223) logentry will be able to bind to span in the Google Cloud Web UI. Specifically Cloud Trace (Stackdriver), the `logging.googleapis.com/traceId` field is moved to the top of the log entry and renames it to `traceId`. Signed-off-by: 0Delta <0deltast@gmail.com> --- plugins/out_stackdriver/stackdriver.c | 24 +++++++++++++++++++ plugins/out_stackdriver/stackdriver.h | 2 ++ plugins/out_stackdriver/stackdriver_conf.c | 8 +++++++ .../data/stackdriver/stackdriver_test_trace.h | 3 ++- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index f3a6f33fbb7..a68ab63b2d9 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -1288,6 +1288,7 @@ static int pack_json_payload(int insert_id_extracted, ctx->labels_key, ctx->severity_key, ctx->trace_key, + ctx->span_id_key, ctx->log_name_key, stream /* more special fields are required to be added, but, if this grows with more @@ -1469,6 +1470,10 @@ static int stackdriver_format(struct flb_config *config, char stackdriver_trace[PATH_MAX]; const char *new_trace; + /* Parameters for spanID */ + int spanid_extracted = FLB_FALSE; + flb_sds_t spanid; + /* Parameters for log name */ int log_name_extracted = FLB_FALSE; flb_sds_t log_name = NULL; @@ -1822,6 +1827,7 @@ static int stackdriver_format(struct flb_config *config, * "jsonPayload": {...}, * "timestamp": "...", * "trace": "..." + * "spanId": "..." * } */ entry_size = 3; @@ -1842,6 +1848,14 @@ static int stackdriver_format(struct flb_config *config, entry_size += 1; } + /* Extract spanid */ + spanid_extracted = FLB_FALSE; + if (ctx->span_id_key + && get_string(&spanid, obj, ctx->span_id_key) == 0) { + spanid_extracted = FLB_TRUE; + entry_size += 1; + } + /* Extract log name */ log_name_extracted = FLB_FALSE; if (ctx->log_name_key @@ -1946,6 +1960,16 @@ static int stackdriver_format(struct flb_config *config, flb_sds_destroy(trace); } + /* Add spanId into the log entry */ + if (spanid_extracted == FLB_TRUE) { + msgpack_pack_str(&mp_pck, 6); + msgpack_pack_str_body(&mp_pck, "spanId", 6); + len = flb_sds_len(spanid); + msgpack_pack_str(&mp_pck, len); + msgpack_pack_str_body(&mp_pck, spanid, len); + flb_sds_destroy(spanid); + } + /* Add insertId field into the log entry */ if (insert_id_extracted == FLB_TRUE) { msgpack_pack_str(&mp_pck, 8); diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h index ee4b462e3c7..5aafc6c1186 100644 --- a/plugins/out_stackdriver/stackdriver.h +++ b/plugins/out_stackdriver/stackdriver.h @@ -54,6 +54,7 @@ #define DEFAULT_LABELS_KEY "logging.googleapis.com/labels" #define DEFAULT_SEVERITY_KEY "logging.googleapis.com/severity" #define DEFAULT_TRACE_KEY "logging.googleapis.com/trace" +#define DEFAULT_SPANID_KEY "logging.googleapis.com/spanId" #define DEFAULT_LOG_NAME_KEY "logging.googleapis.com/logName" #define DEFAULT_INSERT_ID_KEY "logging.googleapis.com/insertId" #define SOURCELOCATION_FIELD_IN_JSON "logging.googleapis.com/sourceLocation" @@ -130,6 +131,7 @@ struct flb_stackdriver { flb_sds_t resource; flb_sds_t severity_key; flb_sds_t trace_key; + flb_sds_t span_id_key; flb_sds_t log_name_key; bool autoformat_stackdriver_trace; diff --git a/plugins/out_stackdriver/stackdriver_conf.c b/plugins/out_stackdriver/stackdriver_conf.c index 15e9b9916d8..610788e65a6 100644 --- a/plugins/out_stackdriver/stackdriver_conf.c +++ b/plugins/out_stackdriver/stackdriver_conf.c @@ -311,6 +311,14 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance * ctx->trace_key = flb_sds_create(DEFAULT_TRACE_KEY); } + tmp = flb_output_get_property("span_id_key", ins); + if (tmp) { + ctx->span_id_key = flb_sds_create(tmp); + } + else { + ctx->span_id_key = flb_sds_create(DEFAULT_SPANID_KEY); + } + tmp = flb_output_get_property("log_name_key", ins); if (tmp) { ctx->log_name_key = flb_sds_create(tmp); diff --git a/tests/runtime/data/stackdriver/stackdriver_test_trace.h b/tests/runtime/data/stackdriver/stackdriver_test_trace.h index 15c7bc911ba..44cf011398d 100644 --- a/tests/runtime/data/stackdriver/stackdriver_test_trace.h +++ b/tests/runtime/data/stackdriver/stackdriver_test_trace.h @@ -2,5 +2,6 @@ "1591111124," \ "{" \ "\"trace\": \"test-trace-id-xyz\"" \ + "\"spanId\": \"test-span-id-abc\"" \ "}]" - \ No newline at end of file +