-
Notifications
You must be signed in to change notification settings - Fork 1.9k
opentelemetry: logs: fix packaging length for trace_id and span_id #10767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Strengthen the test: enforce binary type and verify the exact bytes, not only the lengths.
Today the test passes if IDs show up as strings. Given the PR fixes packaging to 16/8-byte binaries, assert type == FLB_RA_BINARY and bytes match the hex source. Also prefer const for the JSON literal.
Apply this diff:
void test_trace_span_binary_sizes() { int ret; struct flb_log_event_encoder enc; struct flb_log_event_decoder dec; struct flb_log_event event; int32_t record_type; - char *input_json; + const char *input_json; int error_status = 0; int found_trace_id = 0; int found_span_id = 0; size_t trace_id_size = 0; size_t span_id_size = 0; struct flb_record_accessor *ra_trace_id; struct flb_record_accessor *ra_span_id; struct flb_ra_value *val_trace_id; struct flb_ra_value *val_span_id; - size_t len; + unsigned char expected_trace_id[16]; + unsigned char expected_span_id[8]; + const char *trace_hex = "5B8EFFF798038103D269B633813FC60C"; + const char *span_hex = "EEE19B7EC3C1B174"; /* Test input with trace_id and span_id */ input_json = "{\"resourceLogs\":[{\"scopeLogs\":[{\"logRecords\":[{\"timeUnixNano\":\"1640995200000000000\",\"traceId\":\"5B8EFFF798038103D269B633813FC60C\",\"spanId\":\"EEE19B7EC3C1B174\",\"body\":{\"stringValue\":\"test\"}}]}]}]}"; ret = flb_log_event_encoder_init(&enc, FLB_LOG_EVENT_FORMAT_FLUENT_BIT_V2); TEST_CHECK(ret == FLB_EVENT_ENCODER_SUCCESS); ret = flb_opentelemetry_logs_json_to_msgpack(&enc, input_json, strlen(input_json), NULL, &error_status); TEST_CHECK(ret == 0); + TEST_CHECK(error_status == 0); + + /* Prepare expected binary bytes from hex */ + ret = flb_otel_utils_hex_to_id(trace_hex, 32, expected_trace_id, sizeof(expected_trace_id)); + TEST_CHECK(ret == 0); + ret = flb_otel_utils_hex_to_id(span_hex, 16, expected_span_id, sizeof(expected_span_id)); + TEST_CHECK(ret == 0); /* Create record accessors for trace_id and span_id */ ra_trace_id = flb_ra_create("$otlp['trace_id']", FLB_FALSE); TEST_CHECK(ra_trace_id != NULL); ra_span_id = flb_ra_create("$otlp['span_id']", FLB_FALSE); TEST_CHECK(ra_span_id != NULL); @@ /* Use record accessor to get trace_id */ val_trace_id = flb_ra_get_value_object(ra_trace_id, *event.metadata); if (val_trace_id != NULL) { found_trace_id = 1; - if (val_trace_id->type == FLB_RA_BINARY) { + TEST_CHECK_(val_trace_id->type == FLB_RA_BINARY, "trace_id must be emitted as binary"); + if (val_trace_id->type == FLB_RA_BINARY) { trace_id_size = flb_sds_len(val_trace_id->val.binary); printf("Found trace_id with binary size: %zu\n", trace_id_size); /* trace_id should be 16 bytes (32 hex chars = 16 bytes) */ TEST_CHECK_(trace_id_size == 16, "trace_id binary size should be 16, got %zu", trace_id_size); + TEST_CHECK_(memcmp(expected_trace_id, val_trace_id->val.binary, 16) == 0, + "trace_id binary content mismatch"); } - else if (val_trace_id->type == FLB_RA_STRING) { - printf("Found trace_id as string: %s\n", val_trace_id->val.string); - } flb_ra_key_value_destroy(val_trace_id); } /* Use record accessor to get span_id */ val_span_id = flb_ra_get_value_object(ra_span_id, *event.metadata); if (val_span_id != NULL) { found_span_id = 1; - if (val_span_id->type == FLB_RA_BINARY) { + TEST_CHECK_(val_span_id->type == FLB_RA_BINARY, "span_id must be emitted as binary"); + if (val_span_id->type == FLB_RA_BINARY) { span_id_size = flb_sds_len(val_span_id->val.binary); printf("Found span_id with binary size: %zu\n", span_id_size); /* span_id should be 8 bytes (16 hex chars = 8 bytes) */ TEST_CHECK_(span_id_size == 8, "span_id binary size should be 8, got %zu", span_id_size); + TEST_CHECK_(memcmp(expected_span_id, val_span_id->val.binary, 8) == 0, + "span_id binary content mismatch"); } - else if (val_span_id->type == FLB_RA_STRING) { - printf("Found span_id as string: %s\n", val_span_id->val.string); - } flb_ra_key_value_destroy(val_span_id); } } }📝 Committable suggestion