Appsignal.instrument helper. Event formatters allow the data to be passed to the ActiveSupport::Notifications.instrument method call to be formatted for AppSignal events.
The metadata for the events formatted by the event formatters will be visible on trace detail pages in the event timeline. Hover over a specific event and the on mouse hover pop-up will show details like the exact database being queried or the query that was executed.
Creating an event formatter
An AppSignal event formatter is a class with one instance method,format. This format method receives the event payload Hash and needs to return an Array with three values.
It’s possible to add event formatter for libraries that use ActiveSupport::Notifications instrumentation, but look out that there’s not already an event formatter registered for it.
It’s also possible to create an event formatter for your own events. When adding your own event names, please mind the event naming guidelines.
Each event formatter receives an event metadata “payload” Hash from which the event formatter can format the metadata for the event in AppSignal. This AppSignal event metadata needs to be returned by the event formatter in this order in an Array:
- An event title (
String)- A more descriptive title of an event, such as
"Fetch current user"or"Fetch blog post comments". It will appear next to the event name in the event tree on the performance trace page to provide a little more context on what’s happening.
- A more descriptive title of an event, such as
- An event body (
String)- More details such as the database query that was used by the event.
- An event body format (
Integer)- Body format supports formatters to scrub the given data in the
bodyargument to remove any sensitive data from the value. There are currently two supported values for thebody_formatargument.Appsignal::EventFormatter::DEFAULT- This default value will indicate to AppSignal to leave the value intact and not scrub any data from it.
Appsignal::EventFormatter::SQL_BODY_FORMAT- The
SQL_BODY_FORMATvalue will indicate to AppSignal to run your data through the SQL sanitizer and scrub any values in SQL queries.
- The
- Body format supports formatters to scrub the given data in the
Example event formatter
Formatting Dry::Monitor events
Event formatters also format events instrumented through Dry::Monitor notifications. Register the formatter against the Dry::Monitor event ID followed by.dry, so an event instrumented as sql is formatted by the formatter registered for sql.dry.
The first value the formatter returns means something different for these events. For an ActiveSupport::Notifications event it is the event’s title. For a Dry::Monitor event it is the event’s name, and the event is recorded in the timeline without a title.
Unregistering an event formatter
Unregister a formatter to stop AppSignal from using it for an event name.Appsignal::EventFormatter.registered?("event.custom") to check whether an event name has a formatter registered.
Changes in gem 2.5
In AppSignal for Ruby gem version2.5.2 some changes were made in how event formatters are registered. The old method of registering event formatters was deprecated in this release and will be removed in version 3.0 of the Ruby gem.
The new method of registering EventFormatters will allow custom formatters to be registered after AppSignal has loaded. This allows EventFormatters to be registered in Rails initializers.
In gem version 2.5.1 and older, it is possible to register an event formatter like the following example, calling the register method in the class itself.
Appsignal::EventFormatter class.