Skip to main content

Crate flareapp

Crate flareapp 

Source
Expand description

Sends errors, logs and traces from Rust applications to Flare.

§Getting started

// Reads the API key from the `FLARE_KEY` environment variable or `.env` file.
let _flare = flareapp::init(flareapp::config!().environment("production"));

// Your application runs here. When `_flare` is dropped, queued data is sent
// before the process exits.

You can also use #[flareapp::main]. The attribute starts the Flare client, and also reports an error that main returns:

#[flareapp::main(environment = "production")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Your application runs here.
    Ok(())
}

§Reporting errors

use flareapp::{Level, User};
use serde_json::json;

flareapp::configure_scope(|scope| scope.set_user(User::new("42").email("a@b.c")));
flareapp::glow("cart updated", Level::Info, json!({ "items": 3 }));

if let Err(error) = std::fs::read_to_string("orders.csv") {
    flareapp::report_error(&error);
}

flareapp::report_message("Stock low", Level::Warning);

When the panic feature is on, the Flare client also reports panics.

Keep the guard in a named variable like _flare. let _ = flareapp::init(…) drops the guard right away and stops the Flare client.

§How data is sent

The Flare client sends logs and traces as OTLP, the standard format of OpenTelemetry, encoded as protobuf. Config::otlp_protocol switches to OTLP/JSON. The worker thread, a background thread, collects the data and sends the data in batches, so your code never waits for the network. When a request fails, the worker thread tries the request again up to 3 times, and waits longer before each new try.

§Features

  • ureq (default): sends data with the ureq HTTP client. Without this feature, pass your own Transport to Config::transport.
  • backtrace (default): adds stack traces to reports. Without this feature, a report has only the line where the app made the report.
  • panic (default): reports panics. Turns on backtrace.
  • macros (default): adds the #[flareapp::main] attribute.
  • dotenv (default): reads the settings of Config::new from the .env file, when the environment does not set them.
  • tracing: sends tracing spans as traces, and events as logs and breadcrumbs. sqlx queries become query spans. See the flareapp::tracing module.
  • log: sends log records as logs and breadcrumbs. See the flareapp::log module. You need this feature only when your app does not use tracing.
  • tower: adds middleware that gives each request its own scope and root span. See the flareapp::tower module.
  • axum: turns on tower, and adds the route name (GET /users/{id}) for axum apps. The tower middleware also detects axum without this feature.
  • actix: adds actix-web middleware that gives each request its own scope and root span, and reports server errors. See the flareapp::actix module. Turns on backtrace.
  • reqwest: continues traces in outgoing reqwest calls. See the flareapp::reqwest module.
  • sqlx-postgres, sqlx-mysql, sqlx-sqlite: add a sqlx pool whose query spans name the database and show failed queries. See the flareapp::sqlx module.
  • anyhow: reports anyhow::Error. See the flareapp::anyhow module. Turns on backtrace.
  • opentelemetry: sends the spans of the OpenTelemetry SDK, for apps and frameworks that make their spans with tracing-opentelemetry. See the flareapp::opentelemetry module.
  • laravel: adds a service provider for Laravel for Rust, which sends the errors, logs and traces of the app. See the flareapp::laravel module. Turns on tracing and opentelemetry, and needs Rust 1.99.

Modules§

actix
Adds Flare to actix-web apps. See the flareapp-actix crate.
anyhow
Reports anyhow errors. See the flareapp-anyhow crate.
laravel
Connects Flare to Laravel for Rust. See the flareapp-laravel crate.
log
Sends log records to Flare. See the flareapp-log crate.
opentelemetry
Sends the spans of the OpenTelemetry SDK to Flare. See the flareapp-opentelemetry crate.
reqwest
Continues traces in outgoing reqwest calls. See the flareapp-reqwest crate.
sqlx
Sends sqlx queries as query spans, with the database and the errors. See the flareapp-sqlx crate.
tower
Adds Flare to tower servers like axum. See the flareapp-tower crate.
tracing
Sends tracing spans and events to Flare. See the flareapp-tracing crate.

Macros§

config
Creates a Config that already has the name and version of your crate.

Structs§

Config
Settings for the Flare client.
EntryPoint
What started the work that a report, log or trace belongs to: a web request, a command, or a queued job. Flare requires an entry point, and groups and filters by it.
Guard
A handle that keeps the Flare client running. init returns the guard.
Report
One error, panic or message, on its way to Flare.
ReportId
The id of one report, a random UUID. Flare calls it the tracking UUID.
Request
An HTTP POST request to Flare, ready to send.
Response
The parts of an HTTP response that the Flare client needs: the status, the retry time and the body.
Scope
The user, context, attributes, entry point, glows and log breadcrumbs that reports include.
TransportError
The error for a request that got no HTTP response, like a connection failure or a timeout.
User
The user that was logged in when an error happened.
WithScope
A future that runs with its own scope. Create it with ScopeFutureExt::with_flare_scope.

Enums§

Level
How serious a message or glow is. These are the levels Flare knows, the same as the PSR-3 log levels in PHP.
OtlpProtocol
How logs and traces are encoded before they are sent. Set it with Config::otlp_protocol.
Signal
The kind of data in a request. Each kind goes to its own endpoint.

Traits§

ReportableError
An error that the Flare client can report: an error type that does not borrow data, or a dyn Error.
ScopeFutureExt
Gives a future its own scope, so the user, context and breadcrumbs of one request stay with that request, across every .await.
Transport
Sends requests to Flare. Implement this trait to send data with your own HTTP client.

Functions§

configure_scope
Changes the current scope.
flush
Waits until all queued logs, spans and error reports are sent, or until timeout has passed. Returns true when nothing is left to send.
glow
Adds a glow: a breadcrumb that shows up on the timeline of the next reports.
init
Starts the Flare client and returns a guard that keeps the client running.
report_error
Reports an error that your code handled.
report_error_with
Reports an error, and lets you change the report before it is sent.
report_message
Reports a message, like a log line that needs attention.
with_scope
Runs work with an extra scope on the current thread, and removes that scope afterwards, also when work panics.

Attribute Macros§

main
Starts the Flare client at the start of main, and keeps the client running until main ends.