@@ -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+
9971009static 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+
10091022static 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 );
0 commit comments