Skip to content

Commit b7c7081

Browse files
authored
out_stackdriver: add trace and Custom Log Name Support (#2683)
- Add trace to stackdriver output. This is necessary for stackdriver to correlate request logs with app logs. See https://medium.com/google-cloud/combining-correlated-log-lines-in-google-stackdriver-dd23284aeb29 - Added a default label for trace_key which is needed for the proper removal from the jsonPayload. Signed-off-by: Todor Petrov <[email protected]>
1 parent e1a0859 commit b7c7081

File tree

6 files changed

+388
-6
lines changed

6 files changed

+388
-6
lines changed

plugins/out_stackdriver/stackdriver.c

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,18 @@ static int get_msgpack_obj(msgpack_object * subobj, const msgpack_object * o,
994994
return -1;
995995
}
996996

997+
static int get_string(flb_sds_t * s, const msgpack_object * o, const flb_sds_t key)
998+
{
999+
msgpack_object tmp;
1000+
if (get_msgpack_obj(&tmp, o, key, flb_sds_len(key), MSGPACK_OBJECT_STR) == 0) {
1001+
*s = flb_sds_create_len(tmp.via.str.ptr, tmp.via.str.size);
1002+
return 0;
1003+
}
1004+
1005+
*s = 0;
1006+
return -1;
1007+
}
1008+
9971009
static int get_severity_level(severity_t * s, const msgpack_object * o,
9981010
const flb_sds_t key)
9991011
{
@@ -1006,6 +1018,7 @@ static int get_severity_level(severity_t * s, const msgpack_object * o,
10061018
return -1;
10071019
}
10081020

1021+
10091022
static int get_stream(msgpack_object_map map)
10101023
{
10111024
int i;
@@ -1104,8 +1117,13 @@ static int pack_json_payload(int insert_id_extracted,
11041117
monitored_resource_key,
11051118
local_resource_id_key,
11061119
ctx->labels_key,
1120+
ctx->severity_key,
1121+
ctx->trace_key,
1122+
ctx->log_name_key,
11071123
stream
1108-
/* more special fields are required to be added */
1124+
/* more special fields are required to be added, but, if this grows with more
1125+
than a few records, it might need to be converted to flb_hash
1126+
*/
11091127
};
11101128

11111129
if (insert_id_extracted == FLB_TRUE) {
@@ -1138,7 +1156,7 @@ static int pack_json_payload(int insert_id_extracted,
11381156
* check length of key to avoid partial matching
11391157
* e.g. labels key = labels && kv->key = labelss
11401158
*/
1141-
if (flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
1159+
if (removed && flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
11421160
to_remove += 1;
11431161
break;
11441162
}
@@ -1215,7 +1233,7 @@ static int pack_json_payload(int insert_id_extracted,
12151233
len = kv->key.via.str.size;
12161234
for (j = 0; j < len_to_be_removed; j++) {
12171235
removed = to_be_removed[j];
1218-
if (flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
1236+
if (removed && flb_sds_cmp(removed, kv->key.via.str.ptr, len) == 0) {
12191237
key_not_found = 0;
12201238
break;
12211239
}
@@ -1264,6 +1282,7 @@ static int stackdriver_format(struct flb_config *config,
12641282
char path[PATH_MAX];
12651283
char time_formatted[255];
12661284
const char *newtag;
1285+
const char *new_log_name;
12671286
msgpack_object *obj;
12681287
msgpack_object *labels_ptr;
12691288
msgpack_unpacked result;
@@ -1276,6 +1295,16 @@ static int stackdriver_format(struct flb_config *config,
12761295
int severity_extracted = FLB_FALSE;
12771296
severity_t severity;
12781297

1298+
/* Parameters for trace */
1299+
int trace_extracted = FLB_FALSE;
1300+
flb_sds_t trace;
1301+
char stackdriver_trace[PATH_MAX];
1302+
const char *new_trace;
1303+
1304+
/* Parameters for log name */
1305+
int log_name_extracted = FLB_FALSE;
1306+
flb_sds_t log_name;
1307+
12791308
/* Parameters for insertId */
12801309
msgpack_object insert_id_obj;
12811310
insert_id_status in_status;
@@ -1581,7 +1610,8 @@ static int stackdriver_format(struct flb_config *config,
15811610
* "labels": "...",
15821611
* "logName": "...",
15831612
* "jsonPayload": {...},
1584-
* "timestamp": "..."
1613+
* "timestamp": "...",
1614+
* "trace": "..."
15851615
* }
15861616
*/
15871617
entry_size = 3;
@@ -1594,6 +1624,21 @@ static int stackdriver_format(struct flb_config *config,
15941624
entry_size += 1;
15951625
}
15961626

1627+
/* Extract trace */
1628+
trace_extracted = FLB_FALSE;
1629+
if (ctx->trace_key
1630+
&& get_string(&trace, obj, ctx->trace_key) == 0) {
1631+
trace_extracted = FLB_TRUE;
1632+
entry_size += 1;
1633+
}
1634+
1635+
/* Extract log name */
1636+
log_name_extracted = FLB_FALSE;
1637+
if (ctx->log_name_key
1638+
&& get_string(&log_name, obj, ctx->log_name_key) == 0) {
1639+
log_name_extracted = FLB_TRUE;
1640+
}
1641+
15971642
/* Extract insertId */
15981643
in_status = validate_insert_id(&insert_id_obj, obj);
15991644
if (in_status == INSERTID_VALID) {
@@ -1668,6 +1713,26 @@ static int stackdriver_format(struct flb_config *config,
16681713
msgpack_pack_int(&mp_pck, severity);
16691714
}
16701715

1716+
/* Add trace into the log entry */
1717+
if (trace_extracted == FLB_TRUE) {
1718+
msgpack_pack_str(&mp_pck, 5);
1719+
msgpack_pack_str_body(&mp_pck, "trace", 5);
1720+
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+
1731+
msgpack_pack_str(&mp_pck, len);
1732+
msgpack_pack_str_body(&mp_pck, new_trace, len);
1733+
flb_sds_destroy(trace);
1734+
}
1735+
16711736
/* Add insertId field into the log entry */
16721737
if (insert_id_extracted == FLB_TRUE) {
16731738
msgpack_pack_str(&mp_pck, 8);
@@ -1729,9 +1794,21 @@ static int stackdriver_format(struct flb_config *config,
17291794
newtag = "stderr";
17301795
}
17311796
}
1797+
1798+
if (log_name_extracted == FLB_FALSE) {
1799+
new_log_name = newtag;
1800+
}
1801+
else {
1802+
new_log_name = log_name;
1803+
}
1804+
17321805
/* logName */
17331806
len = snprintf(path, sizeof(path) - 1,
1734-
"projects/%s/logs/%s", ctx->project_id, newtag);
1807+
"projects/%s/logs/%s", ctx->project_id, new_log_name);
1808+
1809+
if (log_name_extracted == FLB_TRUE) {
1810+
flb_sds_destroy(log_name);
1811+
}
17351812

17361813
msgpack_pack_str(&mp_pck, 7);
17371814
msgpack_pack_str_body(&mp_pck, "logName", 7);

plugins/out_stackdriver/stackdriver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#define MONITORED_RESOURCE_KEY "logging.googleapis.com/monitored_resource"
5151
#define LOCAL_RESOURCE_ID_KEY "logging.googleapis.com/local_resource_id"
5252
#define DEFAULT_LABELS_KEY "logging.googleapis.com/labels"
53+
#define DEFAULT_SEVERITY_KEY "logging.googleapis.com/severity"
54+
#define DEFAULT_TRACE_KEY "logging.googleapis.com/trace"
55+
#define DEFAULT_LOG_NAME_KEY "logging.googleapis.com/logName"
5356
#define DEFAULT_INSERT_ID_KEY "logging.googleapis.com/insertId"
5457
#define SOURCELOCATION_FIELD_IN_JSON "logging.googleapis.com/sourceLocation"
5558
#define HTTPREQUEST_FIELD_IN_JSON "logging.googleapis.com/http_request"
@@ -108,6 +111,9 @@ struct flb_stackdriver {
108111
/* other */
109112
flb_sds_t resource;
110113
flb_sds_t severity_key;
114+
flb_sds_t trace_key;
115+
flb_sds_t log_name_key;
116+
bool autoformat_stackdriver_trace;
111117

112118
/* oauth2 context */
113119
struct flb_oauth2 *o;

plugins/out_stackdriver/stackdriver_conf.c

Lines changed: 31 additions & 1 deletion
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

@@ -268,8 +269,35 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
268269
if (tmp) {
269270
ctx->severity_key = flb_sds_create(tmp);
270271
}
272+
else {
273+
ctx->severity_key = flb_sds_create(DEFAULT_SEVERITY_KEY);
274+
}
275+
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+
284+
tmp = flb_output_get_property("trace_key", ins);
285+
if (tmp) {
286+
ctx->trace_key = flb_sds_create(tmp);
287+
}
288+
else {
289+
ctx->trace_key = flb_sds_create(DEFAULT_TRACE_KEY);
290+
}
291+
292+
tmp = flb_output_get_property("log_name_key", ins);
293+
if (tmp) {
294+
ctx->log_name_key = flb_sds_create(tmp);
295+
}
296+
else {
297+
ctx->log_name_key = flb_sds_create(DEFAULT_LOG_NAME_KEY);
298+
}
271299

272-
if (flb_sds_cmp(ctx->resource, "k8s_container",
300+
if (flb_sds_cmp(ctx->resource, "k8s_container",
273301
flb_sds_len(ctx->resource)) == 0 ||
274302
flb_sds_cmp(ctx->resource, "k8s_node",
275303
flb_sds_len(ctx->resource)) == 0 ||
@@ -344,6 +372,8 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx)
344372
flb_sds_destroy(ctx->token_uri);
345373
flb_sds_destroy(ctx->resource);
346374
flb_sds_destroy(ctx->severity_key);
375+
flb_sds_destroy(ctx->trace_key);
376+
flb_sds_destroy(ctx->log_name_key);
347377
flb_sds_destroy(ctx->labels_key);
348378
flb_sds_destroy(ctx->tag_prefix);
349379

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define LOG_NAME_OVERRIDE "[" \
2+
"1591111124," \
3+
"{" \
4+
"\"custom_log_name_key\": \"custom_log_name\"" \
5+
"}]"
6+
7+
#define LOG_NAME_NO_OVERRIDE "[" \
8+
"1591111124," \
9+
"{" \
10+
"}]"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define TRACE_COMMON_CASE "[" \
2+
"1591111124," \
3+
"{" \
4+
"\"trace\": \"test-trace-id-xyz\"" \
5+
"}]"
6+

0 commit comments

Comments
 (0)