Etiqueta: sentry

Configure sentry on a Ionic Angular app

Sentry is a great tool tool to track errors in our applications, when an error occurs, it is sent to the server, storing it’s trackback and you will receive a notification email. In this article we will learn how to configure sentry on an Ionic Angular Application. On other articles we have seen how to use it with Python.

To explain this, we will start by creating a simple Ionic Application

Sentry with Ionic
Sentry logo

First we create the example application:

ionic start example --type angular tags

Configuration

After this we need to install the npm package

npm install --save @sentry/angular @sentry/tracing

On the main.ts we configure the sentry DSN like this:

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

import * as Sentry from '@sentry/angular';
import { Integrations } from '@sentry/tracing';


Sentry.init({
  dsn: 'your sentry dsn',
  integrations: [
    new Integrations.BrowserTracing({
      tracingOrigins: ['localhost'],
      routingInstrumentation: Sentry.routingInstrumentation,
    }),
  ],

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,
});


if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch(err => console.error(err));

Add the ErrorHandler on the providers, on app.module.ts

{
    provide: ErrorHandler,
    useValue: Sentry.createErrorHandler({
        showDialog: false, //You can enable/dissable the report dialog 
    }),
},
{
    provide: Sentry.TraceService,
    deps: [Router],
},
{
    provide: APP_INITIALIZER,
    useFactory: () => () => {},
    deps: [Sentry.TraceService],
    multi: true,
},

Now, with this your application, it is ready to report to the Sentry server. You can test it with calling an undefined function:

undefinedFunction();

If you want to check extra information about this you can check the official documentation

Sentry

Sentry is another service you don’t know that you need it until you try it.Basically Sentry is a service that catches the exceptions in your code and sends it to a web service to be analyzed, also sends you an email to notify you.

Sentry server is an opensource server that can be installed on your own server, also sentry can work as a SaS and you can use it at https://sentry.io/welcome/

Sentry can work with multiple languages from Python, to JS or Go, you can check all the supported languages here https://sentry.io/platforms/

Configuring sentry with python

First of all you need to create a project like this:

If you use some framework you can specify

Once you created the project you will get an screen like this

After this you will need to install the sentry package with this command:

pip install --upgrade sentry-sdk

You will need to add this on the package, if you use a setyp.py add it on the install_requires key or if you use it on a requirements.txt file you need to add it.

Now we need to initialize the sentry code. We do it adding this code on the entry point of the code.

import sentry_sdk
sentry_sdk.init(
    "!Place here your project DSN"
    traces_sample_rate=1.0
)

With this we have our script configured to catch all the exceptions with Sentry.

Extra usage

We can make our code notify an exception manually with sentry with this code:

from sentry_sdk import capture_exception
try:
    a=1/0
catch Exception as e:
    capture_exception(e)

Also can notify suspicious data without sending an exception with this code:

from sentry_sdk import capture_message
capture_message("Somthing strange")

© 2024 Another dev

Tema de Anders NorenAmunt ↑