Skip to content

Commit 8fbb2fe

Browse files
committed
opentelemetry: adjust parser for updated proto fields
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 31cd8e5 commit 8fbb2fe

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

src/opentelemetry/flb_opentelemetry_logs.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ static int process_json_payload_log_records_entry(
5050
msgpack_object *observed_time_unix_nano = NULL;
5151
msgpack_object *severity_number = NULL;
5252
msgpack_object *severity_text = NULL;
53+
msgpack_object *dropped_attributes_count = NULL;
5354
msgpack_object *trace_id = NULL;
5455
msgpack_object *span_id = NULL;
56+
msgpack_object *trace_flags = NULL;
57+
msgpack_object *event_name = NULL;
5558
struct flb_time timestamp;
5659

5760
if (error_status) {
@@ -153,6 +156,12 @@ static int process_json_payload_log_records_entry(
153156
metadata_object = &log_records_entry->ptr[result].val;
154157
}
155158

159+
/* droppedAttributesCount */
160+
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "droppedAttributesCount", 0, FLB_TRUE);
161+
if (result >= 0) {
162+
dropped_attributes_count = &log_records_entry->ptr[result].val;
163+
}
164+
156165
/* traceId */
157166
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "traceId", 0, FLB_TRUE);
158167
if (result >= 0) {
@@ -229,6 +238,12 @@ static int process_json_payload_log_records_entry(
229238
}
230239
}
231240

241+
/* traceFlags */
242+
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "traceFlags", 0, FLB_TRUE);
243+
if (result >= 0) {
244+
trace_flags = &log_records_entry->ptr[result].val;
245+
}
246+
232247
/* body */
233248
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "body", 0, FLB_TRUE);
234249
if (result == -1) {
@@ -244,6 +259,12 @@ static int process_json_payload_log_records_entry(
244259
body_object = &log_records_entry->ptr[result].val;
245260
}
246261

262+
/* eventName */
263+
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "eventName", 0, FLB_TRUE);
264+
if (result >= 0) {
265+
event_name = &log_records_entry->ptr[result].val;
266+
}
267+
247268
result = flb_log_event_encoder_begin_record(encoder);
248269

249270
if (result == FLB_EVENT_ENCODER_SUCCESS) {
@@ -301,11 +322,20 @@ static int process_json_payload_log_records_entry(
301322
result = flb_otel_utils_json_payload_append_converted_kvlist(encoder, FLB_LOG_EVENT_METADATA, metadata_object);
302323
}
303324

325+
if (dropped_attributes_count != NULL &&
326+
(dropped_attributes_count->type == MSGPACK_OBJECT_POSITIVE_INTEGER ||
327+
dropped_attributes_count->type == MSGPACK_OBJECT_NEGATIVE_INTEGER)) {
328+
flb_log_event_encoder_append_metadata_values(encoder,
329+
FLB_LOG_EVENT_STRING_VALUE("dropped_attributes_count", 24),
330+
FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE(dropped_attributes_count));
331+
}
332+
304333
if (trace_id != NULL && trace_id->type == MSGPACK_OBJECT_STR && trace_id->via.str.size == 32) {
305334
if (flb_otel_utils_hex_to_id(trace_id->via.str.ptr, trace_id->via.str.size, tmp_id, 16) != 0) {
306335
if (error_status) {
307336
*error_status = FLB_OTEL_LOGS_ERR_INVALID_TRACE_ID;
308337
}
338+
flb_log_event_encoder_rollback_record(encoder);
309339
return -FLB_OTEL_LOGS_ERR_INVALID_TRACE_ID;
310340
}
311341
flb_log_event_encoder_append_metadata_values(encoder,
@@ -318,13 +348,58 @@ static int process_json_payload_log_records_entry(
318348
if (error_status) {
319349
*error_status = FLB_OTEL_LOGS_ERR_INVALID_SPAN_ID;
320350
}
351+
flb_log_event_encoder_rollback_record(encoder);
321352
return -FLB_OTEL_LOGS_ERR_INVALID_SPAN_ID;
322353
}
323354
flb_log_event_encoder_append_metadata_values(encoder,
324355
FLB_LOG_EVENT_STRING_VALUE("span_id", 7),
325356
FLB_LOG_EVENT_BINARY_VALUE(tmp_id, 8));
326357
}
327358

359+
if (trace_flags != NULL) {
360+
if (trace_flags->type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
361+
timestamp_uint64 = trace_flags->via.u64;
362+
363+
flb_log_event_encoder_append_metadata_values(encoder,
364+
FLB_LOG_EVENT_STRING_VALUE("trace_flags", 11),
365+
FLB_LOG_EVENT_UINT64_VALUE((uint8_t) (timestamp_uint64 & 0xff)));
366+
}
367+
else if (trace_flags->type == MSGPACK_OBJECT_STR) {
368+
memset(timestamp_str, 0, sizeof(timestamp_str));
369+
if (trace_flags->via.str.size < sizeof(timestamp_str)) {
370+
strncpy(timestamp_str,
371+
trace_flags->via.str.ptr,
372+
trace_flags->via.str.size);
373+
}
374+
else {
375+
strncpy(timestamp_str,
376+
trace_flags->via.str.ptr,
377+
sizeof(timestamp_str) - 1);
378+
}
379+
380+
for (i = 0; i < strlen(timestamp_str); i++) {
381+
if (!isdigit((unsigned char) timestamp_str[i])) {
382+
timestamp_str[0] = '\0';
383+
break;
384+
}
385+
}
386+
387+
if (strlen(timestamp_str) > 0) {
388+
timestamp_uint64 = strtoull(timestamp_str, NULL, 10);
389+
390+
flb_log_event_encoder_append_metadata_values(encoder,
391+
FLB_LOG_EVENT_STRING_VALUE("trace_flags", 11),
392+
FLB_LOG_EVENT_UINT64_VALUE((uint8_t) (timestamp_uint64 & 0xff)));
393+
}
394+
}
395+
}
396+
397+
if (event_name != NULL && event_name->type == MSGPACK_OBJECT_STR) {
398+
flb_log_event_encoder_append_metadata_values(encoder,
399+
FLB_LOG_EVENT_STRING_VALUE("event_name", 10),
400+
FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE(event_name));
401+
}
402+
328403
result = flb_log_event_encoder_commit_map(encoder, FLB_LOG_EVENT_METADATA);
329404

330405
if (result == FLB_EVENT_ENCODER_SUCCESS &&

0 commit comments

Comments
 (0)