Skip to content

Commit

Permalink
out_stackdriver: support stackdriver trace span (fluent#4223)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
0Delta committed Mar 17, 2022
1 parent da871ff commit 74fe1e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
29 changes: 29 additions & 0 deletions plugins/out_stackdriver/stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1892,6 +1897,7 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
* "jsonPayload": {...},
* "timestamp": "...",
* "trace": "..."
* "spanId": "..."
* }
*/
entry_size = 3;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions plugins/out_stackdriver/stackdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/data/stackdriver/stackdriver_test_trace.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define TRACE_COMMON_CASE "[" \
"1591111124," \
"{" \
"\"trace\": \"test-trace-id-xyz\"" \
"\"trace\": \"test-trace-id-xyz\"," \
"\"spanId\": \"test-span-id-abc\"" \
"}]"

0 comments on commit 74fe1e0

Please sign in to comment.