Simply put, front-end monitoring is the set of processes and tools used to track the performance of a website or app.
Front-end monitoring primarily focuses on the parts that the end user sees. These include issues such as:
As websites are becoming more powerful and complex, the maintenance of its performance becomes increasingly difficult.
Front-end performance is a part of user experience. The perception of a business’ quality is often what the user first sees and experiences through its website. Any inconsistencies, downtime or errors on the client can lead to a loss of trust and credibility of a website. Therefore, front-end monitoring is an essential part in developing strong and robust websites and apps.
Fortunately, there are currently some powerful tools such as Sentry to track, record and monitor front-end performance. It is an open-source error tracing tool that supports various languages and frameworks such as Java, PHP, Ruby, React, Rust, Unity, etc.
In this tutorial, let’s set up and start monitoring a React app with Sentry.
Create react app
If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.
Create a free Sentry account at sentry.io. After creating an account, click the Create project button.
Now, select React as the platform of our project and enter a project name. Click Create Project to finish setting up a new Sentry project.
In a React app, we can integrate Sentry by installing its SDK with the following command:
Import the installed packages in your React’s app index.js file like so:
In order for Sentry to connect to our React app, we need to configure our SDK with our client key, also known as the Sentry DSN (Data Source Name) value.
To get our client key, simply navigate to Settings > Projects > {Your Project Name}, as shown in the screenshot below.
Then, click on Client Keys (DSN) and copy the DSN value.
Back in your App’s index.js file, add the Sentry.init() method below the import statements to connect the app to your Sentry project. Your index.js file should look something like:
While testing, it is okay to keep the tracesSampleRate as 1.0. This means that every action performed in the browser will be sent as a transaction to Sentry.
In production, this value should be lowered to collect a uniform sample data size without reaching Sentry’s transaction quota. Alternatively, to collect sample data dynamically, tracesSampler can be used to filter these transactions.
Once we’ve configured our app, we may test whether our integration is successful with a simple button:
If we run our app, we would get the following error:
Now, let’s check our Sentry dashboard to see if the error has been properly traced. As seen in the image below, the ReferenceError is there.
Besides capturing errors from React, Sentry can capture errors that are specified within the app too.
For example, in React app, we add some function. First is ButtonError.
And then, we simply add a try-catch statement when calling this function. We need to use Sentry.captureException() so it will be captured as a transaction and sent to Sentry.
Don't forget to import the package to use Sentry in our App.js file:
Now, if we click button Expected.
In our Sentry Dashboard, under Issues, we can see the custom error we have captured.
In addition to error tracking, we can enable performance monitoring in our Sentry dashboard by wrapping Sentry.withProfiler() in our App component in its export statement.
Navigate to the Performance tab to monitor and measure important metrics such as the FCP (First Contentful Paint), latency or downtime of any API requests, etc.
OpenReplay is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data.
Just add some configuration to capture session.
Sentry also support to bring pop-up feedback when error occur. Add some configuration on initiate Sentry.
When the error occur. Let's click the Expected button again.
If user submit the feedback, we can see it on User Feedbacks
Without a doubt, front-end monitoring has gradually become prevalent in web development practices today. Powerful tools such as Sentry can provide useful insights and error management to enrich the user experience.
What’s even more powerful is the fact that OpenReplay integrates with Sentry, which allows for replayed activities to be sent for faster and easier debugging. To learn more about how to integrate OpenReplay with Sentry, please visit this link.
Thank you for reading. I hope this article has been helpful in getting you started with front-end monitoring and Sentry.