Skip to content

Commit 54acd76

Browse files
committed
tests: internal: opentelemetry: add unit test for msgpack logs ids
Signed-off-by: Eduardo Silva <[email protected]>
1 parent b5f7b11 commit 54acd76

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tests/internal/opentelemetry.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <fluent-bit/flb_pack.h>
2323
#include <fluent-bit/flb_sds.h>
2424
#include <fluent-bit/flb_mem.h>
25+
#include <fluent-bit/flb_record_accessor.h>
26+
#include <fluent-bit/flb_ra_key.h>
2527

2628
// #include "../../plugins/in_opentelemetry/opentelemetry.h"
2729
#include <fluent-bit/flb_opentelemetry.h>
@@ -735,13 +737,103 @@ void test_opentelemetry_cases()
735737
flb_free(cases_json);
736738
}
737739

740+
void test_trace_span_binary_sizes()
741+
{
742+
int ret;
743+
struct flb_log_event_encoder enc;
744+
struct flb_log_event_decoder dec;
745+
struct flb_log_event event;
746+
int32_t record_type;
747+
char *input_json;
748+
int error_status = 0;
749+
int found_trace_id = 0;
750+
int found_span_id = 0;
751+
size_t trace_id_size = 0;
752+
size_t span_id_size = 0;
753+
struct flb_record_accessor *ra_trace_id;
754+
struct flb_record_accessor *ra_span_id;
755+
struct flb_ra_value *val_trace_id;
756+
struct flb_ra_value *val_span_id;
757+
size_t len;
758+
759+
/* Test input with trace_id and span_id */
760+
input_json = "{\"resourceLogs\":[{\"scopeLogs\":[{\"logRecords\":[{\"timeUnixNano\":\"1640995200000000000\",\"traceId\":\"5B8EFFF798038103D269B633813FC60C\",\"spanId\":\"EEE19B7EC3C1B174\",\"body\":{\"stringValue\":\"test\"}}]}]}]}";
761+
762+
ret = flb_log_event_encoder_init(&enc, FLB_LOG_EVENT_FORMAT_FLUENT_BIT_V2);
763+
TEST_CHECK(ret == FLB_EVENT_ENCODER_SUCCESS);
764+
765+
ret = flb_opentelemetry_logs_json_to_msgpack(&enc, input_json, strlen(input_json), NULL, &error_status);
766+
TEST_CHECK(ret == 0);
767+
768+
/* Create record accessors for trace_id and span_id */
769+
ra_trace_id = flb_ra_create("$otlp['trace_id']", FLB_FALSE);
770+
TEST_CHECK(ra_trace_id != NULL);
771+
772+
ra_span_id = flb_ra_create("$otlp['span_id']", FLB_FALSE);
773+
TEST_CHECK(ra_span_id != NULL);
774+
775+
/* Decode the output to check binary sizes */
776+
ret = flb_log_event_decoder_init(&dec, enc.output_buffer, enc.output_length);
777+
TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS);
778+
779+
flb_log_event_decoder_read_groups(&dec, FLB_TRUE);
780+
781+
while ((ret = flb_log_event_decoder_next(&dec, &event)) == FLB_EVENT_DECODER_SUCCESS) {
782+
ret = flb_log_event_decoder_get_record_type(&event, &record_type);
783+
TEST_CHECK(ret == 0);
784+
785+
if (record_type == FLB_LOG_EVENT_NORMAL) {
786+
/* Use record accessor to get trace_id */
787+
val_trace_id = flb_ra_get_value_object(ra_trace_id, *event.metadata);
788+
if (val_trace_id != NULL) {
789+
found_trace_id = 1;
790+
if (val_trace_id->type == FLB_RA_BINARY) {
791+
trace_id_size = flb_sds_len(val_trace_id->val.binary);
792+
printf("Found trace_id with binary size: %zu\n", trace_id_size);
793+
/* trace_id should be 16 bytes (32 hex chars = 16 bytes) */
794+
TEST_CHECK_(trace_id_size == 16, "trace_id binary size should be 16, got %zu", trace_id_size);
795+
}
796+
else if (val_trace_id->type == FLB_RA_STRING) {
797+
printf("Found trace_id as string: %s\n", val_trace_id->val.string);
798+
}
799+
flb_ra_key_value_destroy(val_trace_id);
800+
}
801+
802+
/* Use record accessor to get span_id */
803+
val_span_id = flb_ra_get_value_object(ra_span_id, *event.metadata);
804+
if (val_span_id != NULL) {
805+
found_span_id = 1;
806+
if (val_span_id->type == FLB_RA_BINARY) {
807+
span_id_size = flb_sds_len(val_span_id->val.binary);
808+
printf("Found span_id with binary size: %zu\n", span_id_size);
809+
/* span_id should be 8 bytes (16 hex chars = 8 bytes) */
810+
TEST_CHECK_(span_id_size == 8, "span_id binary size should be 8, got %zu", span_id_size);
811+
}
812+
else if (val_span_id->type == FLB_RA_STRING) {
813+
printf("Found span_id as string: %s\n", val_span_id->val.string);
814+
}
815+
flb_ra_key_value_destroy(val_span_id);
816+
}
817+
}
818+
}
819+
820+
flb_log_event_decoder_destroy(&dec);
821+
flb_log_event_encoder_destroy(&enc);
822+
flb_ra_destroy(ra_trace_id);
823+
flb_ra_destroy(ra_span_id);
824+
825+
TEST_CHECK(found_trace_id == 1);
826+
TEST_CHECK(found_span_id == 1);
827+
}
828+
738829
/* Test list */
739830
TEST_LIST = {
740831
{ "hex_to_id", test_hex_to_id },
741832
{ "convert_string_number_to_u64", test_convert_string_number_to_u64 },
742833
{ "find_map_entry_by_key", test_find_map_entry_by_key },
743834
{ "json_payload_get_wrapped_value", test_json_payload_get_wrapped_value },
744835
{ "opentelemetry_cases", test_opentelemetry_cases },
836+
{ "trace_span_binary_sizes", test_trace_span_binary_sizes },
745837
{ 0 }
746838
};
747839

0 commit comments

Comments
 (0)