Skip to content

Commit 2af7ee3

Browse files
jpnurmiclaude
andcommitted
extract sentry__ensure_user_id alongside sentry__ensure_event_id
Centralize the "user.id or installation_id" resolution so both the event and session call sites share the same helper. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 37391d0 commit 2af7ee3

4 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/sentry_core.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,17 @@ sentry__ensure_event_id(sentry_value_t event, sentry_uuid_t *uuid_out)
856856
return event_id;
857857
}
858858

859+
sentry_value_t
860+
sentry__ensure_user_id(sentry_value_t user, const char *installation_id)
861+
{
862+
sentry_value_t user_id = sentry_value_get_by_key(user, "id");
863+
if (sentry_value_is_null(user_id) && installation_id) {
864+
user_id = sentry_value_new_string(installation_id);
865+
sentry_value_set_by_key(user, "id", user_id);
866+
}
867+
return user_id;
868+
}
869+
859870
void
860871
sentry_set_user(sentry_value_t user)
861872
{

src/sentry_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ sentry_uuid_t sentry__new_event_id(void);
109109
sentry_value_t sentry__ensure_event_id(
110110
sentry_value_t event, sentry_uuid_t *uuid_out);
111111

112+
/**
113+
* This will ensure that the given `user` has an `id`, falling back to
114+
* `installation_id` when the user has no explicit id. Returns the id value
115+
* (existing or newly-set), or a null value if neither is available.
116+
*/
117+
sentry_value_t sentry__ensure_user_id(
118+
sentry_value_t user, const char *installation_id);
119+
112120
/**
113121
* This will return an owned reference to the global options.
114122
*/

src/sentry_scope.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,9 @@ sentry__scope_apply_to_event(const sentry_scope_t *scope,
360360
if (IS_NULL("user")) {
361361
SET("user", sentry__value_clone(scope->user));
362362
}
363-
// patch missing user ID with installation ID
364363
sentry_value_t user = sentry_value_get_by_key(event, "user");
365-
if (sentry_value_get_type(user) == SENTRY_VALUE_TYPE_OBJECT
366-
&& sentry_value_is_null(sentry_value_get_by_key(user, "id"))) {
367-
sentry_value_set_by_key(user, "id",
368-
sentry_value_new_string(options->run->installation_id));
364+
if (sentry_value_get_type(user) == SENTRY_VALUE_TYPE_OBJECT) {
365+
sentry__ensure_user_id(user, options->run->installation_id);
369366
}
370367
} else if (sentry_value_get_length(scope->user) > 0) {
371368
PLACE_VALUE("user", scope->user);

src/sentry_session.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "sentry_session.h"
22
#include "sentry_alloc.h"
3+
#include "sentry_core.h"
34
#include "sentry_database.h"
45
#include "sentry_envelope.h"
56
#include "sentry_json.h"
@@ -301,12 +302,8 @@ void
301302
sentry__session_sync_user(
302303
sentry_session_t *session, sentry_value_t user, const char *installation_id)
303304
{
304-
sentry_value_t did = sentry_value_get_by_key(user, "id");
305+
sentry_value_t did = sentry__ensure_user_id(user, installation_id);
305306
sentry_value_decref(session->distinct_id);
306-
if (sentry_value_is_null(did) && installation_id) {
307-
session->distinct_id = sentry_value_new_string(installation_id);
308-
} else {
309-
sentry_value_incref(did);
310-
session->distinct_id = did;
311-
}
307+
sentry_value_incref(did);
308+
session->distinct_id = did;
312309
}

0 commit comments

Comments
 (0)