Scopes
The user, the context, the glows and the log breadcrumbs that Flare adds to a report all live in a scope. In a program that does one thing at a time, you never have to think about that. In a server that handles a hundred requests at once, you do: the user of one request must never end up on the error of another.
That's the problem scopes solve. Each request, job or task can have its own scope on top of the app-wide one, and configure_scope always changes the scope of the work that's running right now.
The app-wide scope
Outside of any request or job, configure_scope changes the app-wide scope, and every report gets what you put there:
Rust
use serde_json::json;
#[flareapp::main]
fn main() {
flareapp::configure_scope(|scope| {
scope.set_context("deployment", json!({ "region": "eu-west-1" }));
});
// Every report from here on carries the deployment context.
}
That's the right place for things that are the same for the whole process, like the region it runs in or the name of the worker.
Requests
The axum and Actix Web middleware give every request its own scope. Inside a handler, configure_scope only changes the scope of that request:
Rust
use flareapp::User;
async fn checkout(user: AuthenticatedUser) -> String {
flareapp::configure_scope(|scope| {
scope.set_user(User::new(user.id.to_string()));
});
// A panic or report from here on knows the user, other requests don't.
// ...
}
A request scope starts out with everything from the app-wide scope, and adds its own values on top. When the request ends, its scope goes away with it. The log breadcrumbs work the same way: an error shows the log lines of its own request, plus the ones written outside any request or job, like at startup.
Middleware order
The request scope only exists inside the Flare middleware. Code that runs before it, like an authentication middleware that wraps the Flare middleware, has no request scope yet. A configure_scope call there changes the app-wide scope, and the user ends up on the errors of every other request.
In axum and Actix Web, the middleware you add last runs first. So add the Flare middleware after your own middleware:
Rust
use axum::{middleware::from_fn, routing::get, Router};
let app = Router::new()
.route("/orders", get(list_orders))
.layer(from_fn(authenticate))
.layer(flareapp::tower::layer());
Rust
use actix_web::{middleware::from_fn, web, App};
let app = App::new()
.wrap(from_fn(authenticate))
.wrap(flareapp::actix::middleware())
.route("/orders", web::get().to(list_orders));
Now the Flare middleware runs first, and authenticate can set the user on the scope of the request. With a tower::ServiceBuilder, it's the other way around: the layer you add first runs first, so put the Flare layer at the top.
Jobs and other async work
Work that doesn't come in through the middleware, like a job from a queue or a message from a stream, needs its own scope too. Build one, and attach it to the future with with_flare_scope:
Rust
use flareapp::{Scope, ScopeFutureExt, User};
let mut scope = Scope::default();
scope.set_user(User::new(job.user_id.to_string()));
handle_job(job).with_flare_scope(scope).await;
The scope travels with the future across every .await, also when tokio moves the future to another thread. Inside handle_job, configure_scope changes this scope, so you can keep adding to it as the job learns more.
Flare knows that this is a job from the consumer span of handle_job. When your app doesn't use the Flare tracing layer, tell Flare on the scope instead, with scope.set_entry_point(EntryPoint::queue("send-invoice")). See setting the entry point yourself.
Spawned tasks
A task you start with tokio::spawn runs on its own, so it doesn't pick up the scope of the request or job that started it. When the task should report with the same user and context, give it a scope of its own:
Rust
use flareapp::{Scope, ScopeFutureExt, User};
let mut scope = Scope::default();
scope.set_user(User::new(user_id.to_string()));
tokio::spawn(send_welcome_email(user_id).with_flare_scope(scope));
Sync code
For code without async, with_scope runs a closure with its own scope. The first closure sets up the scope, the second does the work:
Rust
use flareapp::User;
flareapp::with_scope(
|scope| scope.set_user(User::new("42")),
|| import_orders(),
);
The scope is removed again when the closure returns, also when it panics. with_scope returns whatever the closure returns, so it fits around existing code without much rewriting.
What a scope holds
| Part | Set with | Read more |
|---|---|---|
| The user | scope.set_user(...) |
Identifying users |
| Custom context | scope.set_context(...) |
Adding custom context |
| The entry point of the work | scope.set_entry_point(...) |
Entry points |
| Glows | flareapp::glow(...) |
Adding glows |
| Log breadcrumbs | the logging integrations | Breadcrumbs |
A scope keeps the last 30 glows and the last 100 log breadcrumbs. Older ones make room for new ones.