Using highlight.io without a framework in Rust
Learn how to set up highlight.io without a framework.
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.
Install the Highlight Rust SDK.
Add Highlight to your Config.toml. You'll need to pick your features based on what kind of runtime your project uses. If everything is synchronous, you can use the default features. If you're using tokio
, turn off default features and use the feature tokio
. If you're using async-std
, turn off default features and use the feature async-std
.
[dependencies.highlightio]
version = "1"
default-features = ...
features = [...]
Initialize the Highlight Rust SDK.
highlightio::Highlight::init
initializes the SDK.
use highlightio::{Highlight, HighlightConfig};
// or async fn main()
// with #[tokio::main] if you're using tokio, etc.
fn main() {
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");
// ...
h.shutdown();
}
Capture errors.
Highlight::capture_error
can be used to explicitly capture any error.
fn do_something() -> Result<(), Error> {
// ...
}
fn main() {
// ...
match do_something() {
Ok(_) => {},
Err(e) => h.capture_error(&e),
};
}
Verify your errors are being recorded.
Now that you've set up the SDK, you can verify that the backend error handling works by sending an error in. Visit the highlight errors page and check that backend errors are coming in.
fn main() {
// ...
let e = std::io::Error::new(
std::io::ErrorKind::Other,
"This is a test error."
);
h.capture_error(&e);
}
Install the log crate.
Highlight works with the log crate to make logging easier.
[dependencies]
log = "0.4"
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 on 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};
// ...
trace!("This is a trace! log. {:?}", "hi!");
debug!("This is a debug! log. {}", 3 * 3);
info!("This is an info! log. {}", 2 + 2);
warn!("This is a warn! log.");
error!("This is an error! log.");
Verify your backend logs are being recorded.
Visit the highlight logs portal and check that backend logs are coming in.
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"
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");
Verify your backend traces are being recorded.
Visit the highlight traces portal and check that backend traces are coming in.