Skip to content

Commit

Permalink
out_stackdriver: support stackdriver trace span (#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 Dec 21, 2021
1 parent 3f99107 commit ea5b937
Show file tree
Hide file tree
Showing 4 changed files with 36 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 @@ -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;
Expand Down Expand Up @@ -1822,6 +1827,7 @@ static int stackdriver_format(struct flb_config *config,
* "jsonPayload": {...},
* "timestamp": "...",
* "trace": "..."
* "spanId": "..."
* }
*/
entry_size = 3;
Expand All @@ -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
Expand Down Expand Up @@ -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);
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;
bool autoformat_stackdriver_trace;

Expand Down
8 changes: 8 additions & 0 deletions plugins/out_stackdriver/stackdriver_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 ea5b937

Please sign in to comment.