Sentry Rust SDK

repository·master·Indexed 20 days ago

https://github.com/getsentry/sentry-rust

The Sentry Rust SDK (version 0.49.1) provides error tracking and observability for Rust applications. It includes the high-level sentry crate for general use and the low-level sentry-core for integration authors. The SDK offers specialized integrations including sentry-actix for web middleware, sentry-anyhow for capturing anyhow::Error chains, sentry-log for the log crate, sentry-opentelemetry for distributed tracing, and sentry-contexts for automatic device and OS metadata.

Tokens
51.9K
Snippets
184
Records
244
Agent score
72%

What's inside sentry-rust

  1. Overview of sentry-backtrace

    master
    The sentry-backtrace crate provides integration and utilities for handling stacktraces within the Sentry Rust SDK. It exposes functions to capture, process, and convert or parse stacktraces. Additionally, it provides integrations that allow Sentry to automatically process stacktraces attached to events.
  2. Overview of sentry-types

    master
    The sentry-types crate provides common types for interacting with the Sentry protocol and the Sentry server. It is used by both the Sentry Relay infrastructure and the Rust Sentry client. It includes types for DSNs, Project IDs, authentication headers, and the Sentry event protocol (currently supporting v7).
  3. Use the Sentry Panic handler integration

    master

    The sentry-panic crate provides a panic handler that automatically dispatches errors caused by a Rust panic! to Sentry.

    If you are using the main sentry crate, the PanicIntegration is enabled by default. When a panic occurs, the integration will:

    1. Dispatch the error to Sentry.
    2. Forward the panic to the previously registered panic hook (allowing other handlers to still function).

    If you are using sentry directly, you don't need to perform additional setup to capture panics.

  4. When to use sentry-core vs sentry

    master

    The sentry-core crate is a low-level library intended for integration authors and third-party library authors who want to instrument their code for Sentry.

    Regular users who want to integrate Sentry into an application should use the [sentry] crate instead. The sentry crate provides a default transport and a wide range of pre-built integrations for various third-party libraries.

  5. Integrate Sentry with OpenTelemetry

    master

    The sentry-opentelemetry integration allows you to capture spans from an existing OpenTelemetry setup and send them to Sentry, supporting distributed tracing.

    Important Constraint: You must use the OpenTelemetry tracing API to start, end, and modify spans. Do not mix OpenTelemetry spans with the Sentry tracing API (e.g., sentry_core::start_transaction), as they will not nest properly. However, you can still use sentry::capture_event (or sentry::capture_message) to send events to Sentry with the correct trace and span association.

  6. How Client, Hub, and Scope work together

    master

    The sentry-core crate is built around the [Unified API] guidelines and centers on three primary concepts:

    • Client: Manages the lifecycle and configuration of the SDK.
    • Hub: The main concurrency primitive that coordinates between the Client and the current execution context.
    • Scope: Manages the context (tags, breadcrumbs, etc.) for the current execution.

    Additionally, the crate provides extension points via the Integration, Transport, and TransportFactory traits.

  7. Capture data using the per-request Hub

    master

    The sentry-actix middleware automatically creates a new per-request Hub derived from the main Hub and updates the current thread's Hub instance.

    This allows you to use standard Sentry functions like sentry::capture_message inside your request handlers or subsequent middleware, and the data will be correctly attached to the specific request's context.

    // Inside an actix-web handler or middleware:
    sentry::capture_message("Something is not well", sentry::Level::Warning);
  8. Use the Minimal API for library instrumentation

    master
    The sentry crate is a fully-featured SDK intended for application developers. If you are building a library and want to instrument it for Sentry usage, or if you need to implement a custom Integration or Transport, you should use the sentry-core crate instead of the main sentry crate.
  9. How sentry-anyhow integrates with Sentry

    master

    The sentry-anyhow integration acts as an event source. It does not require explicit configuration during the sentry::init call; it only requires the anyhow cargo feature to be enabled in your dependency configuration.

    It provides a standalone function capture_anyhow and can also expose error capturing as a method on the sentry::Hub via the AnyhowHubExt trait.

  10. How sentry-contexts works and its default behavior

    master

    The sentry-contexts crate adds structured Contexts to Sentry Events. When using the main sentry crate, this integration is enabled by default. It automatically attaches the following contexts to your events:

    • device
    • os
    • rust

    Additionally, it will set the server_name if it has not already been defined in your configuration.

  11. How `sentry-tower` manages Sentry Hubs per request

    master

    The sentry-tower crate provides Tower layers that automatically bind a unique Sentry Hub to each incoming request. This isolation ensures that breadcrumbs and other context collected during request handling are scoped to that specific request and do not leak or mix across different concurrent requests.

    You can achieve this using NewSentryLayer to create a new hub from the top-level hub for every request, or SentryLayer to provide a custom hub or a logic-based hub selection.

    use sentry_tower::NewSentryLayer;
    
    // Each request gets its own Sentry hub
    let service = ServiceBuilder::new()
        .layer(NewSentryLayer::<Request>::new_from_top())
        .service(your_service);
  12. How to use sentry-types protocol models

    master

    Most types in the protocol module are designed to be serializable to JSON. To simplify object creation, many types implement the Default trait, allowing you to instantiate a struct with specific fields while filling in the rest with default values using the ..Default::default() syntax.

    use sentry_types::protocol::v7;
    
    let event = v7::Event {
        message: Some("Hello World!".to_string()),
        culprit: Some("foo in bar".to_string()),
        level: v7::Level::Info,
        ..Default::default()
    };