Skip to content

Commit 4831443

Browse files
authored
Fix for missing replay_id from metrics (#5483)
* Fix for missing `replay_id` from metrics * Changelog entry
1 parent 8d0a325 commit 4831443

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
99
## Unreleased
1010

11+
### Fixes
12+
- Fix for missing `replay_id` from metrics ([#5483](https://github.com/getsentry/sentry-react-native/pull/5483))
13+
1114
### Dependencies
1215

1316
- Bump JavaScript SDK from v10.30.0 to v10.31.0 ([#5480](https://github.com/getsentry/sentry-react-native/pull/5480))

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Client, DynamicSamplingContext, Event, EventHint, Integration } from '@sentry/core';
1+
import type { Client, DynamicSamplingContext, Event, EventHint, Integration, Metric } from '@sentry/core';
22
import { debug } from '@sentry/core';
33
import { isHardCrash } from '../misc';
44
import { hasHooks } from '../utils/clientutils';
@@ -253,6 +253,15 @@ export const mobileReplayIntegration = (initOptions: MobileReplayOptions = defau
253253
}
254254
});
255255

256+
client.on('processMetric', (metric: Metric) => {
257+
// Add replay_id to metric attributes to link metrics to replays
258+
const currentReplayId = getCachedReplayId();
259+
if (currentReplayId) {
260+
metric.attributes = metric.attributes || {};
261+
metric.attributes.replay_id = currentReplayId;
262+
}
263+
});
264+
256265
client.on('beforeAddBreadcrumb', enrichXhrBreadcrumbsForMobileReplay);
257266
}
258267

packages/core/test/metrics.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getClient, metrics, setCurrentClient } from '@sentry/core';
22
import { ReactNativeClient } from '../src/js';
3+
import { mobileReplayIntegration } from '../src/js/replay/mobilereplay';
34
import { getDefaultTestClientOptions } from './mocks/client';
45
import { NATIVE } from './mockWrapper';
56

@@ -176,5 +177,61 @@ describe('Metrics', () => {
176177
const sentMetric = beforeSendMetric.mock.calls[0]?.[0];
177178
expect(sentMetric).toBeDefined();
178179
});
180+
181+
it('metrics include replay_id when mobile replay integration is active', async () => {
182+
const mockReplayId = 'test-replay-id-123';
183+
(NATIVE.getCurrentReplayId as jest.Mock).mockReturnValue(mockReplayId);
184+
185+
const beforeSendMetric = jest.fn(metric => metric);
186+
187+
const client = new ReactNativeClient({
188+
...getDefaultTestClientOptions({
189+
dsn: EXAMPLE_DSN,
190+
enableMetrics: true,
191+
beforeSendMetric,
192+
integrations: [mobileReplayIntegration()],
193+
replaysSessionSampleRate: 1.0,
194+
}),
195+
});
196+
197+
setCurrentClient(client);
198+
client.init();
199+
200+
// Send a metric
201+
metrics.count('test_metric', 1);
202+
203+
jest.advanceTimersByTime(10000);
204+
expect(beforeSendMetric).toHaveBeenCalled();
205+
const sentMetric = beforeSendMetric.mock.calls[0]?.[0];
206+
expect(sentMetric).toBeDefined();
207+
expect(sentMetric.attributes).toBeDefined();
208+
expect(sentMetric.attributes?.replay_id).toBe(mockReplayId);
209+
});
210+
211+
it('metrics do not include replay_id when replay integration is not active', async () => {
212+
(NATIVE.getCurrentReplayId as jest.Mock).mockReturnValue(null);
213+
214+
const beforeSendMetric = jest.fn(metric => metric);
215+
216+
const client = new ReactNativeClient({
217+
...getDefaultTestClientOptions({
218+
dsn: EXAMPLE_DSN,
219+
enableMetrics: true,
220+
beforeSendMetric,
221+
}),
222+
});
223+
224+
setCurrentClient(client);
225+
client.init();
226+
227+
// Send a metric
228+
metrics.count('test_metric', 1);
229+
230+
jest.advanceTimersByTime(10000);
231+
expect(beforeSendMetric).toHaveBeenCalled();
232+
const sentMetric = beforeSendMetric.mock.calls[0]?.[0];
233+
expect(sentMetric).toBeDefined();
234+
expect(sentMetric.attributes?.replay_id).toBeUndefined();
235+
});
179236
});
180237
});

0 commit comments

Comments
 (0)