Skip to content

Commit e016ccf

Browse files
committed
- Added default logname key logging.googleapis.com/logName
- Added support for `autoformat_stackdriver_trace` to format the trace as `projects/[PROJECT-ID]/traces/[TRACE-ID]` when enabled
1 parent 0a2c653 commit e016ccf

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed

plugins/out_stackdriver/stackdriver.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ static int stackdriver_format(struct flb_config *config,
12981298
/* Parameters for trace */
12991299
int trace_extracted = FLB_FALSE;
13001300
flb_sds_t trace;
1301+
char stackdriver_trace[PATH_MAX];
1302+
const char *new_trace;
13011303

13021304
/* Parameters for log name */
13031305
int log_name_extracted = FLB_FALSE;
@@ -1716,9 +1718,18 @@ static int stackdriver_format(struct flb_config *config,
17161718
msgpack_pack_str(&mp_pck, 5);
17171719
msgpack_pack_str_body(&mp_pck, "trace", 5);
17181720

1719-
len = flb_sds_len(trace);
1721+
if (ctx->autoformat_stackdriver_trace) {
1722+
len = snprintf(stackdriver_trace, sizeof(stackdriver_trace) - 1,
1723+
"projects/%s/traces/%s", ctx->project_id, trace);
1724+
new_trace = stackdriver_trace;
1725+
}
1726+
else {
1727+
len = flb_sds_len(trace);
1728+
new_trace = trace;
1729+
}
1730+
17201731
msgpack_pack_str(&mp_pck, len);
1721-
msgpack_pack_str_body(&mp_pck, trace, len);
1732+
msgpack_pack_str_body(&mp_pck, new_trace, len);
17221733
flb_sds_destroy(trace);
17231734
}
17241735

plugins/out_stackdriver/stackdriver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define DEFAULT_LABELS_KEY "logging.googleapis.com/labels"
5353
#define DEFAULT_SEVERITY_KEY "logging.googleapis.com/severity"
5454
#define DEFAULT_TRACE_KEY "logging.googleapis.com/trace"
55+
#define DEFAULT_LOG_NAME_KEY "logging.googleapis.com/logName"
5556
#define DEFAULT_INSERT_ID_KEY "logging.googleapis.com/insertId"
5657
#define SOURCELOCATION_FIELD_IN_JSON "logging.googleapis.com/sourceLocation"
5758
#define HTTPREQUEST_FIELD_IN_JSON "logging.googleapis.com/http_request"
@@ -112,6 +113,7 @@ struct flb_stackdriver {
112113
flb_sds_t severity_key;
113114
flb_sds_t trace_key;
114115
flb_sds_t log_name_key;
116+
bool autoformat_stackdriver_trace;
115117

116118
/* oauth2 context */
117119
struct flb_oauth2 *o;

plugins/out_stackdriver/stackdriver_conf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <fluent-bit/flb_compat.h>
2323
#include <fluent-bit/flb_info.h>
2424
#include <fluent-bit/flb_unescape.h>
25+
#include <fluent-bit/flb_utils.h>
2526

2627
#include <jsmn/jsmn.h>
2728

@@ -272,6 +273,14 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
272273
ctx->severity_key = flb_sds_create(DEFAULT_SEVERITY_KEY);
273274
}
274275

276+
tmp = flb_output_get_property("autoformat_stackdriver_trace", ins);
277+
if (tmp) {
278+
ctx->autoformat_stackdriver_trace = flb_utils_bool(tmp);
279+
}
280+
else {
281+
ctx->autoformat_stackdriver_trace = FLB_FALSE;
282+
}
283+
275284
tmp = flb_output_get_property("trace_key", ins);
276285
if (tmp) {
277286
ctx->trace_key = flb_sds_create(tmp);
@@ -284,6 +293,9 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
284293
if (tmp) {
285294
ctx->log_name_key = flb_sds_create(tmp);
286295
}
296+
else {
297+
ctx->log_name_key = flb_sds_create(DEFAULT_LOG_NAME_KEY);
298+
}
287299

288300
if (flb_sds_cmp(ctx->resource, "k8s_container",
289301
flb_sds_len(ctx->resource)) == 0 ||

tests/runtime/out_stackdriver.c

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,9 @@ static void cb_check_k8s_container_resource_default_regex(void *ctx, int ffd,
572572
flb_sds_destroy(res_data);
573573
}
574574

575-
static void cb_check_trace_common_case(void *ctx, int ffd,
576-
int res_ret, void *res_data, size_t res_size,
577-
void *data)
575+
static void cb_check_trace_no_autoformat(void *ctx, int ffd,
576+
int res_ret, void *res_data, size_t res_size,
577+
void *data)
578578
{
579579
int ret;
580580

@@ -589,6 +589,27 @@ static void cb_check_trace_common_case(void *ctx, int ffd,
589589
flb_sds_destroy(res_data);
590590
}
591591

592+
static void cb_check_trace_stackdriver_autoformat(void *ctx, int ffd,
593+
int res_ret, void *res_data, size_t res_size,
594+
void *data)
595+
{
596+
int ret;
597+
598+
/* trace in the entries */
599+
ret = mp_kv_cmp(
600+
res_data,
601+
res_size,
602+
"$entries[0]['trace']",
603+
"projects/fluent-bit-test/traces/test-trace-id-xyz");
604+
TEST_CHECK(ret == FLB_TRUE);
605+
606+
/* trace has been removed from jsonPayload */
607+
ret = mp_kv_exists(res_data, res_size, "$entries[0]['jsonPayload']['trace']");
608+
TEST_CHECK(ret == FLB_FALSE);
609+
610+
flb_sds_destroy(res_data);
611+
}
612+
592613
static void cb_check_log_name_override(void *ctx, int ffd,
593614
int res_ret, void *res_data, size_t res_size,
594615
void *data)
@@ -1913,7 +1934,48 @@ void flb_test_resource_global()
19131934
flb_destroy(ctx);
19141935
}
19151936

1916-
void flb_test_trace_common_case()
1937+
void flb_test_trace_no_autoformat()
1938+
{
1939+
int ret;
1940+
int size = sizeof(TRACE_COMMON_CASE) - 1;
1941+
flb_ctx_t *ctx;
1942+
int in_ffd;
1943+
int out_ffd;
1944+
1945+
/* Create context, flush every second (some checks omitted here) */
1946+
ctx = flb_create();
1947+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
1948+
1949+
/* Lib input mode */
1950+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
1951+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
1952+
1953+
/* Stackdriver output */
1954+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
1955+
flb_output_set(ctx, out_ffd,
1956+
"match", "test",
1957+
"resource", "gce_instance",
1958+
"trace_key", "trace",
1959+
NULL);
1960+
1961+
/* Enable test mode */
1962+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1963+
cb_check_trace_no_autoformat,
1964+
NULL, NULL);
1965+
1966+
/* Start */
1967+
ret = flb_start(ctx);
1968+
TEST_CHECK(ret == 0);
1969+
1970+
/* Ingest data sample */
1971+
flb_lib_push(ctx, in_ffd, (char *) TRACE_COMMON_CASE, size);
1972+
1973+
sleep(2);
1974+
flb_stop(ctx);
1975+
flb_destroy(ctx);
1976+
}
1977+
1978+
void flb_test_trace_stackdriver_autoformat()
19171979
{
19181980
int ret;
19191981
int size = sizeof(TRACE_COMMON_CASE) - 1;
@@ -1935,11 +1997,12 @@ void flb_test_trace_common_case()
19351997
"match", "test",
19361998
"resource", "gce_instance",
19371999
"trace_key", "trace",
2000+
"autoformat_stackdriver_trace", "true",
19382001
NULL);
19392002

19402003
/* Enable test mode */
19412004
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1942-
cb_check_trace_common_case,
2005+
cb_check_trace_stackdriver_autoformat,
19432006
NULL, NULL);
19442007

19452008
/* Start */
@@ -4108,7 +4171,8 @@ TEST_LIST = {
41084171
{"resource_gce_instance", flb_test_resource_gce_instance },
41094172

41104173
/* test trace */
4111-
{"trace_common_case", flb_test_trace_common_case},
4174+
{"trace_no_autoformat", flb_test_trace_no_autoformat},
4175+
{"trace_stackdriver_autoformat", flb_test_trace_stackdriver_autoformat},
41124176

41134177
/* test log name */
41144178
{"log_name_override", flb_test_log_name_override},

0 commit comments

Comments
 (0)