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 Jan 25, 2022
1 parent 4c12ad5 commit f800739
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions plugins/out_stackdriver/stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1465,6 +1466,10 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
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;
Expand Down Expand Up @@ -1817,6 +1822,7 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
* "jsonPayload": {...},
* "timestamp": "...",
* "trace": "..."
* "spanId": "..."
* }
*/
entry_size = 3;
Expand All @@ -1837,6 +1843,14 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
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
Expand Down Expand Up @@ -1941,6 +1955,16 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
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);
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 @@ -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"
Expand Down Expand Up @@ -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;
flb_sds_t http_request_key;
int http_request_key_size;
Expand Down
9 changes: 9 additions & 0 deletions plugins/out_stackdriver/stackdriver_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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);
Expand Down Expand Up @@ -525,6 +533,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx)
flb_sds_destroy(ctx->resource);
flb_sds_destroy(ctx->severity_key);
flb_sds_destroy(ctx->trace_key);
flb_sds_destroy(ctx->span_id_key);
flb_sds_destroy(ctx->log_name_key);
flb_sds_destroy(ctx->http_request_key);
flb_sds_destroy(ctx->labels_key);
Expand Down
3 changes: 2 additions & 1 deletion tests/runtime/data/stackdriver/stackdriver_test_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"1591111124," \
"{" \
"\"trace\": \"test-trace-id-xyz\"" \
"\"spanId\": \"test-span-id-abc\"" \
"}]"

0 comments on commit f800739

Please sign in to comment.