Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_stackdriver: process spanId special field #7345

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion plugins/out_stackdriver/stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,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 @@ -1651,6 +1652,10 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
char stackdriver_trace[PATH_MAX];
const char *new_trace;

/* Parameters for span id */
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 @@ -2113,6 +2118,7 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
* "logName": "...",
* "jsonPayload": {...},
* "timestamp": "...",
* "spanId": "...",
* "trace": "..."
* }
*/
Expand All @@ -2134,6 +2140,14 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
entry_size += 1;
}

/* Extract span id */
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 @@ -2249,6 +2263,14 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
flb_sds_destroy(trace);
}

/* Add spanId field into the log entry */
if (span_id_extracted == FLB_TRUE) {
msgpack_pack_str_with_body(&mp_pck, "spanId", 6);
len = flb_sds_len(span_id);
msgpack_pack_str_with_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 @@ -2658,13 +2680,18 @@ static struct flb_config_map config_map[] = {
{
FLB_CONFIG_MAP_BOOL, "autoformat_stackdriver_trace", "false",
0, FLB_TRUE, offsetof(struct flb_stackdriver, autoformat_stackdriver_trace),
"Autoformat the stacrdriver trace"
"Autoformat the stackdriver trace"
},
{
FLB_CONFIG_MAP_STR, "trace_key", DEFAULT_TRACE_KEY,
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 @@ -51,6 +51,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 @@ -163,6 +164,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
6 changes: 6 additions & 0 deletions tests/runtime/data/stackdriver/stackdriver_test_span_id.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#define SPAN_ID_COMMON_CASE "[" \
"1591111124," \
"{" \
"\"logging.googleapis.com/spanId\": \"000000000000004a\"" \
"}]"

63 changes: 63 additions & 0 deletions tests/runtime/out_stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "data/stackdriver/stackdriver_test_k8s_resource.h"
#include "data/stackdriver/stackdriver_test_labels.h"
#include "data/stackdriver/stackdriver_test_trace.h"
#include "data/stackdriver/stackdriver_test_span_id.h"
#include "data/stackdriver/stackdriver_test_log_name.h"
#include "data/stackdriver/stackdriver_test_resource_labels.h"
#include "data/stackdriver/stackdriver_test_insert_id.h"
Expand Down Expand Up @@ -753,6 +754,24 @@ static void cb_check_trace_stackdriver_autoformat(void *ctx, int ffd,
flb_sds_destroy(res_data);
}

static void cb_check_span_id(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
{
int ret;

/* span id in the entries */
ret = mp_kv_cmp(res_data, res_size, "$entries[0]['spanId']", "000000000000004a");
TEST_CHECK(ret == FLB_TRUE);

/* span id has been removed from jsonPayload */
ret = mp_kv_exists(res_data, res_size,
"$entries[0]['jsonPayload']['logging.googleapis.com/spanId']");
TEST_CHECK(ret == FLB_FALSE);

flb_sds_destroy(res_data);
}

static void cb_check_log_name_override(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
Expand Down Expand Up @@ -2439,6 +2458,47 @@ void flb_test_trace_stackdriver_autoformat()
flb_destroy(ctx);
}


void flb_test_span_id()
{
int ret;
int size = sizeof(SPAN_ID_COMMON_CASE) - 1;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

/* Create context, flush every second (some checks omitted here) */
ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

/* Lib input mode */
in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

/* Stackdriver output */
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
"resource", "gce_instance",
NULL);

/* Enable test mode */
ret = flb_output_set_test(ctx, out_ffd, "formatter",
cb_check_span_id,
NULL, NULL);

/* Start */
ret = flb_start(ctx);
TEST_CHECK(ret == 0);

/* Ingest data sample */
flb_lib_push(ctx, in_ffd, (char *) SPAN_ID_COMMON_CASE, size);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}

void flb_test_set_metadata_server()
{
int ret;
Expand Down Expand Up @@ -5964,6 +6024,9 @@ TEST_LIST = {
{"trace_no_autoformat", flb_test_trace_no_autoformat},
{"trace_stackdriver_autoformat", flb_test_trace_stackdriver_autoformat},

/* test span id */
{"span_id", flb_test_span_id},

/* test metadata server */
{"set_metadata_server", flb_test_set_metadata_server},

Expand Down