Grouping with Namespaces
Namespaces are a way to group error incidents, performance incidents and metrics from actions. AppSignal uses three namespaces by default:
web
for web requestsfrontend
for Front-end JavaScriptbackground
for background jobs
Applications can have any number of namespaces, for example, you could have a separate namespace for your application's administration panel, API or scheduled tasks. This guide will go through the steps needed to report actions in custom namespaces.
Configuring Namespaces
Namespaces are defined by your application's code. With small helpers, you can customize the namespace of an action. This guide will teach you how to split requests up into different namespaces. For this guide, we'll create an "admin" and a "critical" namespace for background jobs.
Configurations per Language
Ruby
In Rails we can use the before_action
callback to call a method before a request is handled. In the code example below, we've created a before_action
that calls the set_appsignal_namespace
method. In this method we call the Appsignal.set_namespace
helper method to configure "admin" as the namespace for this request. This also works if other controllers are subclasses of the AdminController
.
Next we'll define the namespace of code executed by our application's Sidekiq background workers. To correctly namespace errors triggered in our background worker, we must define the namespace before any other code runs. In the example Sidekiq worker code below, we do this immediately in the perform method on line 7.
It's also possible to configure the namespace when creating a transaction. The second argument of Appsignal::Transaction.new
accepts the namespace as a value (line 4).
For transactions that have already been created, you can use the set_namespace
method to configure the transaction's namespace, like in the example below:
A note on helper location
The set_namespace
helpers used in this guide can be called in any action that is part of an AppSignal sample. We recommend calling this as early in the request or background job as possible, so the transaction is configured with the given namespace before any error occurs. Otherwise, if an error occurs, or anything else that stops the process, the transaction is sent to AppSignal before the set_namespace
helper is called, and the sample is reported under the default namespace instead.
Read More
Elixir
In Phoenix controllers we use a plug
to call a function before the request is handled by Phoenix. In the code example below we use plug
to call the set_appsignal_namespace
function (line 5). In this function we call the Appsignal.Span.set_namespace
helper to configure the namespace for this request (line 10).
Next we'll configure the namespace for a background job. To catch any errors from within the job within the desired namespace, Appsignal.Span.set_namespace
must be called at the beginning of the function before any other code, like on line 4 of the below code example:
It's also possible to configure the namespace when creating a transaction. Please see the documentation for decorators or instrumentation helpers for more information on how to configure namespaces.
A note on helper location
The set_namespace
helpers used in this guide can be called in any action that starts an AppSignal transaction. We recommend calling this as early in the request or background job as possible, so the transaction is configured with the given namespace before any error occurs. Otherwise, if an error occurs—or anything else that stops the process—the transaction is sent to AppSignal before the set_namespace
code is called and it is reported under the default namespace instead.
Read More
Appsignal.Span.set_namespace
Helper Method Documentation- Elixir Namespace Decorator
- Elixir Namespace Instrumentation Helper
Node.js
In Node.js applications, AppSignal works with OpenTelemetry, which uses spans to track metadata, such as which namespace a span belongs to. Use the setNamespace
helper function as sampled below to set the namespace for the active span's trace.
Read more about how spans work in Node.js.
Python
In Python applications, AppSignal works with OpenTelemetry, which uses spans to track metadata, such as which namespace a span belongs to. Use the set_namespace
helper function as sampled below to set the namespace for the active span's trace.
Read more about how spans work in Python.
Front-end JavaScript
In Front-end JavaScript applications AppSignal works with spans to track metadata such as a span's namespace. The namespace can only be set on the "root span", or the first span that is created without a parent. All children of this span will inherit the namespace of the parent span. When creating this root span, pass in the namespace
option as a key with a String
value that is the desired namespace name, as demonstrated in the below example:
Read more about how spans work in Front-End JavaScript.
Deploy
When your application is configured with its new namespace helpers, commit your changes and deploy the app. When the app starts/restarts, AppSignal will report actions under their new namespace 🥳!
Are namespaces not being reported correctly? Don't panic, give us a shout and we will help you out!