Skip to content

feat(sdk): Add JS Core metrics #3590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ This release contains upgrade of `sentry-android` dependency to major version 7.
### Features

- Add Android profiles to React Native Profiling ([#3397](https://github.com/getsentry/sentry-react-native/pull/3397))
- Add `Sentry.metrics` ([#3590](https://github.com/getsentry/sentry-react-native/pull/3590))

To learn more, see the [Set Up Metrics](https://docs.sentry.io/platforms/react-native/metrics/) guide.

```javascript
import * as Sentry from '@sentry/react-native';

Sentry.init({
dsn: '___DSN___',
integrations: [
Sentry.metrics.metricsAggregatorIntegration(),
],
});

Sentry.metrics.increment("button_click", 1, {
tags: { system: "iOS", app_version: "1.0.0" },
});
```

### Fixes

Expand Down
23 changes: 23 additions & 0 deletions samples/expo/app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Text, View } from '@/components/Themed';
import { SENTRY_INTERNAL_DSN } from '@/utils/dsn';
import { HttpClient } from '@sentry/integrations';
import { setScopeProperties } from '@/utils/setScopeProperties';
import { timestampInSeconds } from '@sentry/utils';
import React from 'react';

const isRunningInExpoGo = Constants.appOwnership === 'expo'

Expand Down Expand Up @@ -38,6 +40,7 @@ Sentry.init({
// default: [/.*/]
failedRequestTargets: [/.*/],
}),
Sentry.metrics.metricsAggregatorIntegration(),
);
return integrations.filter(i => i.name !== 'Dedupe');
},
Expand Down Expand Up @@ -66,6 +69,25 @@ Sentry.init({
});

export default function TabOneScreen() {
const [componentMountStartTimestamp] = React.useState<number>(() => {
return timestampInSeconds();
});

React.useEffect(() => {
if (componentMountStartTimestamp) {
// Distributions help you get the most insights from your data by allowing you to obtain aggregations such as p90, min, max, and avg.
Sentry.metrics.distribution(
'tab_one_mount_time',
timestampInSeconds() - componentMountStartTimestamp,
{
unit: "seconds",
},
);
}
// We only want this to run once.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<View style={styles.container}>
<Text>Welcome to Sentry Expo Sample App!</Text>
Expand All @@ -78,6 +100,7 @@ export default function TabOneScreen() {
<Button
title="Capture exception"
onPress={() => {
Sentry.metrics.increment('tab_one.capture_exception_button_press', 1);
Sentry.captureException(new Error('Captured exception'));
}}
/>
Expand Down
1 change: 1 addition & 0 deletions samples/react-native/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Sentry.init({
// default: [/.*/]
failedRequestTargets: [/.*/],
}),
Sentry.metrics.metricsAggregatorIntegration(),
);
return integrations.filter(i => i.name !== 'Dedupe');
},
Expand Down
20 changes: 20 additions & 0 deletions samples/react-native/src/Screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { CommonActions } from '@react-navigation/native';
import { UserFeedbackModal } from '../components/UserFeedbackModal';
import { FallbackRender } from '@sentry/react';
import NativeSampleModule from '../../tm/NativeSampleModule';
import { timestampInSeconds } from '@sentry/utils';

const { AssetsModule, CppModule, CrashModule } = NativeModules;

Expand All @@ -27,6 +28,25 @@ interface Props {
}

const HomeScreen = (props: Props) => {
const [componentMountStartTimestamp] = React.useState<number>(() => {
return timestampInSeconds();
});

React.useEffect(() => {
if (componentMountStartTimestamp) {
// Distributions help you get the most insights from your data by allowing you to obtain aggregations such as p90, min, max, and avg.
Sentry.metrics.distribution(
'home_mount_time',
timestampInSeconds() - componentMountStartTimestamp,
{
unit: 'seconds',
},
);
}
// We only want this to run once.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Show bad code inside error boundary to trigger it.
const [showBadCode, setShowBadCode] = React.useState(false);
const [isFeedbackVisible, setFeedbackVisible] = React.useState(false);
Expand Down
13 changes: 12 additions & 1 deletion samples/react-native/src/Screens/TrackerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const TrackerScreen = () => {
});
};

const onRefreshButtonPress = () => {
Sentry.metrics.increment('tracker_screen.refresh_button_press', 1, {
tags: { graph: 'none', public_data: true },
});
loadData();
};

React.useEffect(() => {
loadData();
}, []);
Expand Down Expand Up @@ -71,7 +78,11 @@ const TrackerScreen = () => {
<ActivityIndicator size="small" color="#F6F6F8" />
)}
</View>
<Button sentry-label="refresh" title="Refresh" onPress={loadData} />
<Button
sentry-label="refresh"
title="Refresh"
onPress={onRefreshButtonPress}
/>
</View>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export {
getClient,
setCurrentClient,
addEventProcessor,
metrics,
} from '@sentry/core';

import { _addTracingExtensions } from './tracing/addTracingExtensions';
Expand Down