Adding custom context
Every report already tells you a lot: the stack trace, the environment, the version of your crate, the host, and during a request the method, URL, route and headers. The full list is on reporting errors.
What Flare can't know is what matters in your app: the tenant, the order that was being processed, the feature flag that was on. That's what custom context is for. It shows up under custom context on the report in Flare.
Adding context to every report
Use configure_scope and set_context:
Rust
use serde_json::json;
flareapp::configure_scope(|scope| {
scope.set_context("tenant", json!({ "id": 12, "plan": "pro" }));
});
The value can be anything that converts into a serde_json::Value: a string, a number, a bool, or a whole object built with json!. Setting the same key again replaces the old value, and remove_context takes it off again:
Rust
flareapp::configure_scope(|scope| {
scope.remove_context("tenant");
});
Where the context ends up depends on where you call configure_scope. Called at startup, it lands on every report your app sends. Called inside a request, with the axum or Actix Web middleware in place, it only lands on the reports of that request, so the tenant of one customer never shows up on the error of another. Scopes explains how that works, and how to get the same for jobs and background tasks.
Adding context to one report
When the context only makes sense for a single error, pass it along with the report itself:
Rust
use serde_json::json;
flareapp::report_error_with(&error, |report| {
report.set_context("payment", json!({ "provider": "mollie", "order_id": 7 }));
});
This works with flareapp::anyhow::report_with as well. When a key is set both on the scope and on the report, the report wins.
Adding context right before sending
To add or change context on every report in one place, including panics you never see coming, use the before_send hook:
Rust
fn flare_config() -> flareapp::Config {
flareapp::config!().before_send(|report| {
report.set_context("region", "eu-west-1");
true
})
}
#[flareapp::main(config = flare_config)]
fn main() {}
The same hook can blank out context you'd rather not send, by setting its key to Value::Null. See removing sensitive data.
Context, users and glows
Context is one of the things a scope holds. The others have their own page:
- Identifying users: who ran into the error.
- Adding glows: what your app did right before the error.