Sentry Pro Tips

4 minute read

Here are some quick tips to get the most out of Sentry when using it to monitor your frontend.

Log server errors

If you just setup Sentry with a library specific to the framework you are using you get warned of all exceptions that happen in your application. What you might also like to do is to log server errors because it gives you better visibility of problems affecting the frontend. You could get information on these server errors from the backend log manager, but it requires less context switching to just do it all in Sentry if it relates to the frontend.

Here is how you could do it with angular (you can do something similar with other frameworks).

Register an interceptor for HTTP requests:

( function () { ' use strict ' ; angular . module ( ' myNiceWebApp ' , [ ' ngRaven ' , // ... ]) // ... . config ([ ' $httpProvider ' , function ( $httpProvider ) { // Add an HTTP interceptor $httpProvider . interceptors . push ( ' interceptHttpRequestsService ' ); }]) // ... . run ([ ' runAppService ' , function ( runAppService ) { runAppService . runApp (); } ]); }());

Manually capture a message on error responses:

( function () { ' use strict ' ; angular . module ( ' myNiceWebApp.interceptHttpRequestsService ' , []) . factory ( ' interceptHttpRequestsService ' , [ ' $q ' , interceptHttpRequestsService ]); function interceptHttpRequestsService ( $q ) { var service = { responseError : responseError }; return service ; function responseError ( response ) { // we usually don't want to log UNAUTHORIZED // requests otherwise we get an alert // every time someone's // session expires. if ( response . status !== 401 ) { // This example assumes the server // is returning an error code in the // message `response.data.errorCode` // the details of this will be different // depending on the server response. Raven . captureMessage ( ' Server error ' + response . status + ' ' + response . data . errorCode , { level : ' error ' , extra : response }); } return $q . reject ( response ); } } }());

Filter sensitive information

Out of the box Sentry filters out sensitive information (e.g. if you send a password to Sentry it will filter it).

When you look at the data in the Sentry dashboard it will be stripped of sensitive information. For example:

{ // ... withCredentials : [ Filtered ] }

Yet raven - the frontend library that sends the logs to Sentry - is still sending this information to Sentry. If you are not comfortable with that you can just filter it also on the client side. It could look something like this:

// Example of sensitive data you might want to filter out var propertiesToMask = [ ' password ' , ' secret ' , ' credentials ' ]; function isSensitiveProperty ( key ) { return _ . find ( propertiesToMask , function ( propertyToMask ) { return _ . includes ( key . toUpperCase (), propertyToMask . toUpperCase ()); }); } function maskSensitiveData ( obj ) { var key ; if ( obj instanceof Object ) { for ( key in obj ) { if ( obj . hasOwnProperty ( key )) { if ( isSensitiveProperty ( key )) { obj [ k ] = ' [masked] ' } else { // Recursive call to scan property maskSensitiveData ( obj [ key ]); } } } } } Raven . config ( ' https://xxxx@sentry.io/xxx ' , { // A function that allows mutation of the data payload // right before being sent to Sentry. dataCallback : function ( data ) { return maskSensitiveData ( data ); } }) . addPlugin ( Raven . Plugins . Angular ) . install ();

Add client-side context to your logs

When a user logs in set this information for all requests.

Raven . setUserContext ({ email : ' user@example.org ' });

You can also save the webapp version they are using and which environment it is running on.

Raven . config ( ' https://xxx@sentry.io/xxx ' , { release : 8.15 . 0 . dev0_example_version , environment : ' LIVE ' }) . addPlugin ( Raven . Plugins . Angular ) . install ();

Filter localhost logs

Usually you won’t want to get emails informing you of exceptions when developing the application or running automated tests. You can configure Sentry to ignore localhost logs on the Sentry dashboard.

Or on the client side config, that would look something like this:

Raven . config ( ' https://xxx@sentry.io/xxx ' , { ignoreUrls : [ /.*0 \. 0 \. 0 \. */ , /.*localhost.*/ ] }) . addPlugin ( Raven . Plugins . Angular ) . install ();

A hacky alternative is to check window.location.hostname and, if it is running locally, initialize raven with an invalid key. This way raven is initialized and available to the rest of the application but the logs do not reach Sentry. You should avoid this option if you can.

Turn on source maps

You should be combining and minifying your JavaScript before deploying your webapp. However, when you do this your client-side code is no longer readable or debbugable in the production environment. This is a really big set back when using an error tracking tool like Sentry because you won’t be able to know exactly where an error happens… unless you use source maps :)

Source Maps - Basically it's a way to map a combined/minified file back to an unbuilt state. When you build for production, along with minifying and combining your JavaScript files, you generate a source map which holds information about your original files. When you query a certain line and column number in your generated JavaScript you can do a lookup in the source map which returns the original location. Ryan Seddon, Introduction to JavaScript Source Maps, html5rocks.

You’ll need to setup your build system to produce source maps. There is no next step, since Sentry picks up source maps automatically when they are available. As soon as you have source maps Sentry will be able to tell you the exact line in your code where an error occurred.

Set alerts

Sentry provides a way to set alerts based on rules. The example bellow is the simplest, get an email for all events. The rules you set will depend on the kind of application you have and on the number of users.

Extra tip - slack integration

If you are using slack there is an integration for Sentry that improves the visibility of the frontend errors for the entire team.