
Creating a span
When adding custom instrumentation, first import the OpenTelemetry trace module in the file you want to add instrumentation to.One offs and Serverless
When running one-off scripts or serverless functions, you need to manually initialize AppSignal and stop it at the end to ensure no data is lost. Thestop helper will gracefully kill the agent process and wait a while to ensure all data is sent to the AppSignal servers.
Helper methods
The following helper methods set AppSignal specific attributes on spans that help group spans and improve how they’re displayed in AppSignal. Other attributes are not supported on spans by AppSignal.set_category
The span category is the name that appears in the performance event timeline for traces. It’s also used to group spans together to create a breakdown per group on the trace detail page.
.) to express the hierarchical inheritance of the event, with the highest unit last. For more information on span categories, please see the event names guide.
Set a category on every span you instrument yourself. AppSignal reports a span it does not recognize under the category unknown, which groups it with every other unrecognized span.
set_name
More details can be added to the span that are visible when hovering over the event in the event timeline. The span name is used to provide more information about the event, such as “Fetch users”, the database from which they are fetched or the URL that was requested.
start_as_current_span was called.
set_body
The span’s body can include additional information about the event, like the HTTP request, the connected host, etc. Be sure to sanitize the information before adding it to the span so no Personal Identifiable Information is sent to AppSignal. This information will be visible for the span when hovering over the event timeline.
To store SQL queries in the span’s body, please use the set_sql_body helper instead.
set_sql_body
Set a SQL query as the body of the span as it appears in the performance event timeline in the incident trace detail view. This is similar to the set_body helper, but is specialized for SQL queries. Any SQL query set as the body with this attribute will be sanitized to avoid sending PII (Personal Identifiable Information) data to our servers.
See the set_body helper for more details on how the body attribute works.
When both the set_body and set_sql_body helpers are called on the same span, the set_sql_body helper’s value is leading and the set_body helper’s value will be ignored.
set_root_name
Every trace is grouped under an HTTP endpoint, background job worker name, or task name. We call this group the “action name”. To change this action name for the entire trace, use the set_root_name helper.
Use an action name generic enough to group all traces from this part of the app, while not reporting different names every time. Set GET /users/:id (where :id the URL parameter name) as the action name, instead of GET /users/123 (where 123 is the actual value made in the request). The latter would report a new incident for every unique request.
Example Use Case
Your application has an endpoint calledGET /coffee.
GET /coffee, but your endpoint handles multiple actions: coffee?action=buy, and coffee?action=sell.

GET /coffee endpoint, they are conceptually very different and so it would make sense for them to be grouped separately in AppSignal rather than in the same GET /coffee trace. To do this, you can use the set_root_name helper:
set_root_name will change the name of the root span, grouping the traces for the requests coffee?action=buy and coffee?action=sell into separate actions:

Other trace data
These helpers set data on the trace rather than on a single event:set_errorandsend_errorreport an error.set_request_payload,set_request_query_parametersandset_function_parametersreport the parameters of a request or of a background job.set_session_datareports a request’s session data.set_headerreports a request header.set_custom_datareports data that fits none of the others.set_tagtags the trace.set_namespacemoves the trace into another namespace.