Skip to content

Commit 856203b

Browse files
jpnurmiclaude
andcommitted
ref: parse event_id from raw envelopes
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9e8a954 commit 856203b

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/sentry_envelope.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,15 @@ sentry_uuid_t
230230
sentry__envelope_get_event_id(const sentry_envelope_t *envelope)
231231
{
232232
if (envelope->is_raw) {
233-
return sentry_uuid_nil();
233+
const char *payload = envelope->contents.raw.payload;
234+
size_t payload_len = envelope->contents.raw.payload_len;
235+
const char *newline = memchr(payload, '\n', payload_len);
236+
size_t header_len = newline ? (size_t)(newline - payload) : payload_len;
237+
sentry_value_t header = sentry__value_from_json(payload, header_len);
238+
sentry_uuid_t event_id = sentry_uuid_from_string(sentry_value_as_string(
239+
sentry_value_get_by_key(header, "event_id")));
240+
sentry_value_decref(header);
241+
return event_id;
234242
}
235243
return sentry_uuid_from_string(sentry_value_as_string(
236244
sentry_value_get_by_key(envelope->contents.items.headers, "event_id")));

src/sentry_envelope.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ sentry_envelope_t *sentry__envelope_from_path(const sentry_path_t *path);
2727

2828
/**
2929
* This returns the UUID of the event associated with this envelope.
30-
* If there is no event inside this envelope, or the envelope was previously
31-
* loaded from disk, the empty nil UUID will be returned.
30+
* If there is no event inside this envelope, the empty nil UUID will be
31+
* returned.
3232
*/
3333
sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope);
3434

tests/unit/test_envelopes.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,65 @@ SENTRY_TEST(write_raw_envelope_to_file)
377377
sentry_close();
378378
}
379379

380+
SENTRY_TEST(raw_envelope_event_id)
381+
{
382+
sentry_envelope_t *envelope = create_test_envelope();
383+
const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope";
384+
sentry_path_t *test_file_path = sentry__path_from_str(test_file_str);
385+
TEST_CHECK_INT_EQUAL(
386+
sentry_envelope_write_to_file(envelope, test_file_str), 0);
387+
388+
sentry_envelope_t *raw_envelope
389+
= sentry__envelope_from_path(test_file_path);
390+
TEST_CHECK(!!raw_envelope);
391+
392+
sentry_uuid_t event_id = sentry__envelope_get_event_id(raw_envelope);
393+
char event_id_str[37];
394+
sentry_uuid_as_string(&event_id, event_id_str);
395+
TEST_CHECK_STRING_EQUAL(
396+
event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d");
397+
398+
sentry__path_remove(test_file_path);
399+
sentry__path_free(test_file_path);
400+
sentry_envelope_free(envelope);
401+
sentry_envelope_free(raw_envelope);
402+
403+
// missing event_id
404+
const char header_no_event_id[]
405+
= "{\"dsn\":\"https://foo@sentry.invalid/42\"}\n{}";
406+
test_file_path = sentry__path_from_str(test_file_str);
407+
TEST_CHECK_INT_EQUAL(
408+
sentry__path_write_buffer(
409+
test_file_path, header_no_event_id, sizeof(header_no_event_id) - 1),
410+
0);
411+
raw_envelope = sentry__envelope_from_path(test_file_path);
412+
TEST_CHECK(!!raw_envelope);
413+
event_id = sentry__envelope_get_event_id(raw_envelope);
414+
TEST_CHECK(sentry_uuid_is_nil(&event_id));
415+
sentry__path_remove(test_file_path);
416+
sentry__path_free(test_file_path);
417+
sentry_envelope_free(raw_envelope);
418+
419+
// missing newline
420+
const char header_no_newline[]
421+
= "{\"event_id\":\"c993afb6-b4ac-48a6-b61b-2558e601d65d\"}";
422+
test_file_path = sentry__path_from_str(test_file_str);
423+
TEST_CHECK_INT_EQUAL(sentry__path_write_buffer(test_file_path,
424+
header_no_newline, sizeof(header_no_newline) - 1),
425+
0);
426+
raw_envelope = sentry__envelope_from_path(test_file_path);
427+
TEST_CHECK(!!raw_envelope);
428+
event_id = sentry__envelope_get_event_id(raw_envelope);
429+
sentry_uuid_as_string(&event_id, event_id_str);
430+
TEST_CHECK_STRING_EQUAL(
431+
event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d");
432+
sentry__path_remove(test_file_path);
433+
sentry__path_free(test_file_path);
434+
sentry_envelope_free(raw_envelope);
435+
436+
sentry_close();
437+
}
438+
380439
SENTRY_TEST(read_envelope_from_file)
381440
{
382441
const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope";

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ XX(procmaps_parser)
146146
XX(propagation_context_init)
147147
XX(query_consent_requirement)
148148
XX(rate_limit_parsing)
149+
XX(raw_envelope_event_id)
149150
XX(read_envelope_from_file)
150151
XX(read_write_envelope_to_file_null)
151152
XX(read_write_envelope_to_invalid_path)

0 commit comments

Comments
 (0)