Use Rollbar to monitor the system in Rails

Tram Ho

Introduce

Rollbar is a service that supports developers, providing the ability to report exceptions in real time when the system is operational and monitor the process of deploying the system.

Rollbar’s operation is also quite simple, every time the system has an error it will send a notification to the Rollbar server. The rollbar server will collect errors for statistics and send notifications to the webmaster.

Rollbar provides SDK that is easily embedded into the system, and supports many languages ​​such as Go, Java, Ruby, iOS, macOS, Javascript (both browser and server – nodejs), python, PHP, ReactNative, .NET .. ..

In this article, we will learn about how to install Rollbar in ruby,

Not only does Rails support, Rollbar also supports all other ruby ​​integrations and frameworks such as Rails, Sinatra, Goalie, Rack, Grape, Ruby, etc.

In this article, we will learn about how to use Rollbar in Rails

Setting

To install in rails, we add in gemfile

then run bundle install or can install gem directly by command

Run the following command:

POST_SERVER_ITEM_ACCESS_TOKEN value will be taken from https://rollbar.com/ , after we create the account, this is a paid service, but we can use it for free for 14 days

The above command will generate config/initializers/rollbar.rb containing the values ​​to configure Rollbar at initialization and access_token.

If we don’t want to add access_token directly to the above command, we can use an environment variable

and install in config/initializers/rollbar.rb

Using:

The usage is very simple, just add the following rollbar commands for each specific need

For example, suppose we have a faulty function

We add an error text raise StandRrror, now it is expected that when running to the error line, it will run into rescue and send errors to the Rollbar server

However, there will be 2 cases to display the error:

  • Add the error message content to Rollbar.error similar to the above example

If you add that, then when you go to the Rollbar home page, you will see the error message for that error message

  • Add exception to the parameter of Rollbar.error :

We will then track the traceback of the error

In addition, in the paid version, we can track the parameter value of each file as well

Once you know the params, you can easily track the unreasonable values ​​of a function on the production environment that cause errors, because on the production environment, if data cannot be created, validate leads to errors. to Rollback, if we need more information, we can’t ask the user to run again to reproduce the error

Positions should set Rollbar to monitor the system

  • In rescue of important services: help get information when the service mishandled
  • In after_validation: if you need to get more information for complex models, you may have to validate failed
  • Track startup rails: to keep track of this process, we need to change a few config. Move the file from config/initializers/rollbar.rb to config/rollbar.rb and add the following code in config / environment.rb.

  • Monitoring deploy process: Assuming we use Capistrano 3 to deploy, we perform the following steps to monitor the deploy process

Install rollbar in Gemfile same as above

Add require 'rollbar/capistrano3' in the Capfile file

Add the following code in deploy.rb file

If you want to monitor the deployment process with other tools, let’s find out in the doc of rollbar

Share the news now

Source : Viblo