Sentry Rust SDK: sentry-tracing
Support for automatic breadcrumb, event, and trace capturing from tracing
events.
The tracing
crate is supported in three ways. First, events can be captured as breadcrumbs for
later. Secondly, error events can be captured as events to Sentry. Finally, spans can be
recorded as structured transaction events. By default, events above Info
are recorded as
breadcrumbs, events above Error
are captured as error events, and spans above Info
are
recorded as transactions.
Configuration
To fully enable the tracing integration, set the traces sample rate and add a layer to the tracing subscriber:
use *;
let _guard = init;
// Register the Sentry tracing layer to capture breadcrumbs, events, and spans:
registry
.with
.with
.init;
It is also possible to set an explicit filter, to customize which log events are captured by Sentry:
use EventFilter;
use *;
let sentry_layer = layer.event_filter;
registry
.with
.with
.init;
Logging Messages
Tracing events automatically create breadcrumbs that are attached to the current scope in Sentry. They show up on errors and transactions captured within this scope as shown in the examples below.
Fields passed to the event macro are automatically tracked as structured data in Sentry. For breadcrumbs, they are shown directly with the breadcrumb message. For other types of data, read below.
for i in 0..10
Tracking Errors
The easiest way to emit errors is by logging an event with ERROR
level. This will create a
grouped issue in Sentry. To add custom information, prepend the message with fields. It is also
possible to add Sentry tags if a field is prefixed with "tags."
!;
error
To track error structs, assign a reference to error trait object as field
in one of the logging macros. By convention, it is recommended to use the ERROR
level and
assign it to a field called error
, although the integration will also work with all other
levels and field names.
All other fields passed to the macro are captured in a custom "Tracing Fields" context in Sentry.
use Error;
use io;
let custom_error = new;
error!;
It is also possible to combine error messages with error structs. In Sentry, this creates issues grouped by the message and location of the error log, and adds the passed error as nested source.
use Error;
use io;
let custom_error = new;
error!;
Tracing Spans
The integration automatically tracks tracing
spans as spans in Sentry. A convenient way to do
this is with the #[instrument]
attribute macro, which creates a transaction for the function
in Sentry.
Function arguments are added as context fields automatically, which can be configured through attribute arguments. Refer to documentation of the macro for more information.
use Duration;
use *;
// Functions instrumented by tracing automatically report
// their span as transactions.
async
// This creates spans inside the outer transaction, unless called directly.
async
Resources
License: Apache-2.0
- Discord server for project discussions.
- Follow @getsentry on Twitter for updates