diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index bddc8fd44bb..ef6cd4f4de8 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -1280,6 +1280,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 @@ -1458,6 +1459,10 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx, char stackdriver_trace[PATH_MAX]; const char *new_trace; + /* Parameters for spanID */ + int span_id_extracted = FLB_FALSE; + flb_sds_t span_id; + /* Parameters for log name */ int log_name_extracted = FLB_FALSE; flb_sds_t log_name = NULL; @@ -1892,6 +1897,7 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx, * "jsonPayload": {...}, * "timestamp": "...", * "trace": "..." + * "spanId": "..." * } */ entry_size = 3; @@ -1912,6 +1918,14 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx, entry_size += 1; } + /* Extract spanID */ + span_id_extracted = FLB_FALSE; + if (ctx->span_id_key + && get_string(&span_id, obj, ctx->span_id_key) == 0) { + span_id_extracted = FLB_TRUE; + entry_size += 1; + } + /* Extract log name */ log_name_extracted = FLB_FALSE; if (ctx->log_name_key @@ -2018,6 +2032,16 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx, flb_sds_destroy(trace); } + /* Add spanID into the log entry */ + if (span_id_extracted == FLB_TRUE) { + msgpack_pack_str(&mp_pck, 7); + msgpack_pack_str_body(&mp_pck, "span_id", 7); + len = flb_sds_len(span_id); + msgpack_pack_str(&mp_pck, len); + msgpack_pack_str_body(&mp_pck, span_id, len); + flb_sds_destroy(span_id); + } + /* Add insertId field into the log entry */ if (insert_id_extracted == FLB_TRUE) { msgpack_pack_str(&mp_pck, 8); @@ -2393,6 +2417,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_stackdriver, trace_key), "Set the trace key" }, + { + FLB_CONFIG_MAP_STR, "span_id_key", DEFAULT_SPAN_ID_KEY, + 0, FLB_TRUE, offsetof(struct flb_stackdriver, span_id_key), + "Set the span id key" + }, { FLB_CONFIG_MAP_STR, "log_name_key", DEFAULT_LOG_NAME_KEY, 0, FLB_TRUE, offsetof(struct flb_stackdriver, log_name_key), diff --git a/plugins/out_stackdriver/stackdriver.h b/plugins/out_stackdriver/stackdriver.h index 479e0859009..015a9071e59 100644 --- a/plugins/out_stackdriver/stackdriver.h +++ b/plugins/out_stackdriver/stackdriver.h @@ -53,6 +53,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_SPAN_ID_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" @@ -147,6 +148,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; flb_sds_t http_request_key; int http_request_key_size; diff --git a/tests/runtime/data/stackdriver/stackdriver_test_trace.h b/tests/runtime/data/stackdriver/stackdriver_test_trace.h index 15c7bc911ba..24ba9e0a05f 100644 --- a/tests/runtime/data/stackdriver/stackdriver_test_trace.h +++ b/tests/runtime/data/stackdriver/stackdriver_test_trace.h @@ -1,6 +1,6 @@ #define TRACE_COMMON_CASE "[" \ "1591111124," \ "{" \ - "\"trace\": \"test-trace-id-xyz\"" \ + "\"trace\": \"test-trace-id-xyz\"," \ + "\"spanId\": \"test-span-id-abc\"" \ "}]" - \ No newline at end of file