Star us on GitHub
Star
Menu

Using highlight.io with actix-web

Learn how to set up highlight.io with the actix-web framework.

1

Configure client-side Highlight. (optional)

If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping guide.

2

Install the Highlight Rust actix SDK.

Add Highlight to your Config.toml.

[dependencies] highlightio-actix = "1"
Copy
3

Initialize the Highlight Rust SDK and actix Middleware.

highlightio_actix::highlight::Highlight::init initializes the SDK, and adding the highlightio_actix::HighlightActix middleware will start tracing actix.

use actix_web::{App, Error, HttpServer}; use highlightio_actix::{highlight::{Highlight, HighlightConfig}, HighlightActix}; // ...your services... #[actix_web::main] async fn main() -> Result<(), Error> { let h = Highlight::init(HighlightConfig { project_id: "<YOUR_PROJECT_ID>".to_string(), service_name: "my-rust-app".to_string(), service_version: "git-sha".to_string(), ..Default::default() }).expect("Failed to initialize Highlight.io"); let _h = h.clone(); HttpServer::new(move || { App::new() .wrap(HighlightActix::new(&_h)) // ... }) .bind("127.0.0.1:8080")? .run() .await?; h.shutdown(); Ok(()) }
Copy
4

Verify your errors are being recorded.

Now that you've set everything up, you can verify that the backend error handling works by throwing an error in a service. Visit the highlight errors page and check that backend errors are coming in.

// ... #[get("/error")] async fn error() -> Result<impl Responder, std::io::Error> { Err(std::io::Error::new( std::io::ErrorKind::Other, "Test error" ))?; Ok(format!("You shouldn't be able to see this.")) } // ... #[actix_web::main] async fn main() -> Result<(), Error> { // ... HttpServer::new(move || { App::new() .wrap(HighlightActix::new(&h)) .service(error) // add this }) // ... }
Copy
5

Install the log crate.

Highlight works with the log crate to make logging easier.

[dependencies] log = "0.4"
Copy
6

Call the logging facades.

Highlight::init automatically installs a logging backend, so you can call any of the log crate's macros to emit logs. NOTE: env_logger only logs errors to the console out by default, so to see your logs, run your project with the RUST_LOG=<crate name> environment variable, or RUST_LOG=trace to see everything.

use log::{trace, debug, info, warn, error}; // ... #[get("/")] async fn index() -> impl Responder { info!("Hello, world! Greet endpoint called."); format!("Hello, world!") }
Copy
7

Verify your backend logs are being recorded.

Visit the highlight logs portal and check that backend logs are coming in.

8

Add the tracing crate to your project.

The tracing crate allows you and your dependencies to record traces that will be automatically captured by the highlight.io SDK.

[dependencies] tracing = "0.1"
Copy
9

Record a trace.

Use the tracing crate to create spans and events. You can read more about this on the docs.rs page of the tracing crate.

use tracing::{event, span, Level}; // ... let span = span!(Level::INFO, "my_span"); let _guard = span.enter(); event!(Level::DEBUG, "something happened inside my_span");
Copy
10

Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.