Skip to content

Commit 96b153e

Browse files
committed
Metrics for React Native SDK
1 parent 9b50d32 commit 96b153e

File tree

10 files changed

+54
-5
lines changed

10 files changed

+54
-5
lines changed
0 Bytes
Binary file not shown.

packages/core/src/js/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export {
4646
setCurrentClient,
4747
addEventProcessor,
4848
lastEventId,
49+
metrics,
4950
} from '@sentry/core';
5051

5152
export {

packages/core/src/js/wrapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ export const NATIVE: SentryNativeWrapper = {
281281
beforeSend,
282282
beforeBreadcrumb,
283283
beforeSendTransaction,
284+
beforeSendMetric,
284285
integrations,
285286
ignoreErrors,
286287
logsOrigin,

packages/core/test/wrapper.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ describe('Tests Native Wrapper', () => {
194194
expect(NATIVE.enableNative).toBe(true);
195195
});
196196

197+
test('filter beforeSendMetric when initializing Native SDK', async () => {
198+
await NATIVE.initNativeSdk({
199+
dsn: 'test',
200+
enableNative: true,
201+
autoInitializeNativeSdk: true,
202+
beforeSendMetric: jest.fn(),
203+
devServerUrl: undefined,
204+
defaultSidecarUrl: undefined,
205+
mobileReplayOptions: undefined,
206+
});
207+
208+
expect(RNSentry.initNativeSdk).toHaveBeenCalled();
209+
// @ts-expect-error mock value
210+
const initParameter = RNSentry.initNativeSdk.mock.calls[0][0];
211+
expect(initParameter).not.toHaveProperty('beforeSendMetric');
212+
expect(NATIVE.enableNative).toBe(true);
213+
});
214+
197215
test('filter integrations when initializing Native SDK', async () => {
198216
await NATIVE.initNativeSdk({
199217
dsn: 'test',

samples/expo/app/(tabs)/index.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ export default function TabOneScreen() {
3939
}}
4040
/>
4141
</View>
42+
<View style={styles.buttonWrapper}>
43+
<Button
44+
title="Metrics count: Increase"
45+
onPress={() => {
46+
Sentry.metrics.count('sentry_rn_test_metric', 1);
47+
}}
48+
/>
49+
</View>
50+
<View style={styles.buttonWrapper}>
51+
<Button
52+
title="Metrics count: Increase by 100"
53+
onPress={() => {
54+
Sentry.metrics.count('sentry_rn_test_metric', 100);
55+
}}
56+
/>
57+
</View>
4258
<View style={styles.buttonWrapper}>
4359
<Button
4460
title="Capture exception"
@@ -202,7 +218,7 @@ export default function TabOneScreen() {
202218
Sentry.logger.warn('expo warn log');
203219
Sentry.logger.error('expo error log');
204220

205-
Sentry.logger.info('expo info log with data', { database: 'admin', number: 123, obj: { password: 'admin'} });
221+
Sentry.logger.info('expo info log with data', { database: 'admin', number: 123, obj: { password: 'admin' } });
206222
}}
207223
/>
208224
</View>

samples/expo/app/_layout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const navigationIntegration = Sentry.reactNavigationIntegration({
2828

2929
Sentry.init({
3030
// Replace the example DSN below with your own DSN:
31-
dsn: SENTRY_INTERNAL_DSN,
31+
dsn: 'https://c96d5b6136315b7a6cef6230a38b4842@o4509786567475200.ingest.de.sentry.io/4510182962298960',
3232
debug: true,
3333
environment: 'dev',
3434
enableLogs: true,
@@ -40,6 +40,11 @@ Sentry.init({
4040
console.log('Transaction beforeSend:', event.event_id);
4141
return event;
4242
},
43+
beforeSendMetric: (metric: Sentry.Metric) => {
44+
console.log('Metric beforeSend:', metric.name, metric.value);
45+
return metric;
46+
},
47+
enableMetrics: false,
4348
// This will be called with a boolean `didCallNativeInit` when the native SDK has been contacted.
4449
onReady: ({ didCallNativeInit }) => {
4550
console.log('onReady called with didCallNativeInit:', didCallNativeInit);

samples/expo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"eas-build-development-android": "eas build --profile development --platform android"
2121
},
2222
"dependencies": {
23-
"@sentry/react-native": "7.6.0",
23+
"@sentry/react-native": "workspace:^",
2424
"@types/react": "~19.0.10",
2525
"expo": "^53.0.0",
2626
"expo-constants": "~17.1.5",

samples/react-native-macos/src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Sentry.init({
5454
logWithoutTracing('Transaction beforeSend:', event.event_id);
5555
return event;
5656
},
57+
beforeSendMetric(metric: Sentry.Metric) {
58+
logWithoutTracing('Metric beforeSend:', metric.name, metric.value);
59+
return metric;
60+
},
5761
// This will be called with a boolean `didCallNativeInit` when the native SDK has been contacted.
5862
onReady: ({ didCallNativeInit }) => {
5963
logWithoutTracing(

samples/react-native/src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Sentry.init({
5757
logWithoutTracing('Transaction beforeSend:', event.event_id);
5858
return event;
5959
},
60+
beforeSendMetric(metric: Sentry.Metric) {
61+
logWithoutTracing('Metric beforeSend:', metric.name, metric.value);
62+
return metric;
63+
},
6064
// This will be called with a boolean `didCallNativeInit` when the native SDK has been contacted.
6165
onReady: ({ didCallNativeInit }) => {
6266
logWithoutTracing(

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9766,7 +9766,7 @@ __metadata:
97669766
languageName: node
97679767
linkType: hard
97689768

9769-
"@sentry/[email protected], @sentry/react-native@workspace:packages/core":
9769+
"@sentry/[email protected], @sentry/react-native@workspace:^, @sentry/react-native@workspace:packages/core":
97709770
version: 0.0.0-use.local
97719771
resolution: "@sentry/react-native@workspace:packages/core"
97729772
dependencies:
@@ -27606,7 +27606,7 @@ __metadata:
2760627606
"@babel/core": ^7.26.0
2760727607
"@babel/preset-env": ^7.26.0
2760827608
"@sentry/babel-plugin-component-annotate": 4.6.0
27609-
"@sentry/react-native": 7.6.0
27609+
"@sentry/react-native": "workspace:^"
2761027610
"@types/node": 20.10.4
2761127611
"@types/react": ~19.0.10
2761227612
expo: ^53.0.0

0 commit comments

Comments
 (0)