Skip to content

Commit 3464ac1

Browse files
Merge 57a0525 into 000da7a
2 parents 000da7a + 57a0525 commit 3464ac1

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

packages/core/src/js/integrations/logEnricherIntegration.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const logEnricherIntegration = (): Integration => {
1414
cacheLogContext().then(
1515
() => {
1616
client.on('beforeCaptureLog', (log: Log) => {
17-
processLog(log);
17+
processLog(log, client);
1818
});
1919
},
2020
reason => {
@@ -28,6 +28,25 @@ export const logEnricherIntegration = (): Integration => {
2828

2929
let NativeCache: Record<string, unknown> | undefined = undefined;
3030

31+
/**
32+
* Sets a log attribute if the value exists and the attribute key is not already present.
33+
*
34+
* @param logAttributes - The log attributes object to modify.
35+
* @param key - The attribute key to set.
36+
* @param value - The value to set (only sets if truthy and key not present).
37+
* @param setEvenIfPresent - Whether to set the attribute if it is present. Defaults to true.
38+
*/
39+
function setLogAttribute(
40+
logAttributes: Record<string, unknown>,
41+
key: string,
42+
value: unknown,
43+
setEvenIfPresent = true,
44+
): void {
45+
if (value && (!logAttributes[key] || setEvenIfPresent)) {
46+
logAttributes[key] = value;
47+
}
48+
}
49+
3150
async function cacheLogContext(): Promise<void> {
3251
try {
3352
const response = await NATIVE.fetchNativeLogAttributes();
@@ -52,16 +71,26 @@ async function cacheLogContext(): Promise<void> {
5271
return Promise.resolve();
5372
}
5473

55-
function processLog(log: Log): void {
74+
function processLog(log: Log, client: ReactNativeClient): void {
5675
if (NativeCache === undefined) {
5776
return;
5877
}
5978

60-
log.attributes = log.attributes ?? {};
61-
NativeCache.brand && (log.attributes['device.brand'] = NativeCache.brand);
62-
NativeCache.model && (log.attributes['device.model'] = NativeCache.model);
63-
NativeCache.family && (log.attributes['device.family'] = NativeCache.family);
64-
NativeCache.os && (log.attributes['os.name'] = NativeCache.os);
65-
NativeCache.version && (log.attributes['os.version'] = NativeCache.version);
66-
NativeCache.release && (log.attributes['sentry.release'] = NativeCache.release);
79+
// Save log.attributes to a new variable
80+
const logAttributes = log.attributes ?? {};
81+
82+
// Use setLogAttribute with the variable instead of direct assignment
83+
setLogAttribute(logAttributes, 'device.brand', NativeCache.brand);
84+
setLogAttribute(logAttributes, 'device.model', NativeCache.model);
85+
setLogAttribute(logAttributes, 'device.family', NativeCache.family);
86+
setLogAttribute(logAttributes, 'os.name', NativeCache.os);
87+
setLogAttribute(logAttributes, 'os.version', NativeCache.version);
88+
setLogAttribute(logAttributes, 'sentry.release', NativeCache.release);
89+
90+
const replay = client.getIntegrationByName<Integration & { getReplayId: () => string | null }>('MobileReplay');
91+
setLogAttribute(logAttributes, 'sentry.replay_id', replay?.getReplayId());
92+
93+
94+
// Set log.attributes to the variable
95+
log.attributes = logAttributes;
6796
}

packages/core/src/js/replay/mobilereplay.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function mergeOptions(initOptions: Partial<MobileReplayOptions>): Required<Mobil
9595

9696
type MobileReplayIntegration = Integration & {
9797
options: Required<MobileReplayOptions>;
98+
getReplayId: () => string | null;
9899
};
99100

100101
/**
@@ -173,19 +174,26 @@ export const mobileReplayIntegration = (initOptions: MobileReplayOptions = defau
173174
client.on('beforeAddBreadcrumb', enrichXhrBreadcrumbsForMobileReplay);
174175
}
175176

177+
function getReplayId(): string | null {
178+
return NATIVE.getCurrentReplayId();
179+
;
180+
}
181+
176182
// TODO: When adding manual API, ensure overlap with the web replay so users can use the same API interchangeably
177183
// https://github.com/getsentry/sentry-javascript/blob/develop/packages/replay-internal/src/integration.ts#L45
178184
return {
179185
name: MOBILE_REPLAY_INTEGRATION_NAME,
180186
setup,
181187
processEvent,
182188
options: options,
189+
getReplayId: getReplayId
183190
};
184191
};
185192

186193
const mobileReplayIntegrationNoop = (): MobileReplayIntegration => {
187194
return {
188195
name: MOBILE_REPLAY_INTEGRATION_NAME,
189196
options: defaultOptions,
197+
getReplayId: () => null, // Mock implementation for noop version
190198
};
191199
};

0 commit comments

Comments
 (0)