mod main_error;
#[cfg(feature = "ureq")]
mod ureq;
use std::panic::Location;
pub use flareapp_core::{
Config, EntryPoint, Guard, Level, OtlpProtocol, Report, ReportId, ReportableError, Request,
Response, Scope, ScopeFutureExt, Signal, Transport, TransportError, User, WithScope,
configure_scope, flush, glow, with_scope,
};
#[cfg(feature = "macros")]
pub use flareapp_macros::main;
#[doc(hidden)]
pub mod __private {
pub use flareapp_core::workspace_root;
pub use crate::main_error::{MainError, ReportMainError};
pub fn first_set(values: &[Option<&'static str>]) -> &'static str {
values
.iter()
.flatten()
.map(|value| value.trim())
.find(|value| !value.is_empty())
.unwrap_or("")
}
}
#[cfg(feature = "tracing")]
pub mod tracing {
pub use flareapp_tracing::{FlareLayer, SpanExt, current_traceparent, layer};
}
#[cfg(feature = "log")]
pub mod log {
pub use flareapp_log::Logger;
}
#[cfg(feature = "tower")]
pub mod tower {
pub use flareapp_tower::{RequestLayer, RequestService, ResponseFuture, layer};
}
#[cfg(feature = "actix")]
pub mod actix {
pub use flareapp_actix::{Middleware, MiddlewareService, ResponseFuture, middleware};
}
#[cfg(feature = "reqwest")]
pub mod reqwest {
pub use flareapp_reqwest::TracePropagation;
}
#[cfg(any(
feature = "sqlx-postgres",
feature = "sqlx-mysql",
feature = "sqlx-sqlite"
))]
pub mod sqlx {
pub use flareapp_sqlx::{Pool, PoolConnection, TracedDatabase, Transaction};
}
#[cfg(feature = "anyhow")]
pub mod anyhow {
pub use flareapp_anyhow::{report, report_with};
}
pub fn init(config: Config) -> Guard {
#[cfg(feature = "ureq")]
let config = config.default_transport(ureq::UreqTransport::new);
let guard = flareapp_core::init(config);
#[cfg(feature = "panic")]
flareapp_panic::install();
guard
}
#[track_caller]
pub fn report_error<E: ReportableError + ?Sized>(error: &E) -> Option<ReportId> {
report_error_with(error, |_| {})
}
#[track_caller]
pub fn report_error_with<E, F>(error: &E, configure: F) -> Option<ReportId>
where
E: ReportableError + ?Sized,
F: FnOnce(&mut Report),
{
capture_and_send(|location| Report::from_error(error, location), configure)
}
#[track_caller]
pub fn report_message<M: Into<String>>(message: M, level: Level) -> Option<ReportId> {
capture_and_send(
|location| Report::from_message(message, level, location),
|_| {},
)
}
#[track_caller]
fn capture_and_send<M, C>(make_report: M, configure: C) -> Option<ReportId>
where
M: FnOnce(&'static Location<'static>) -> Report,
C: FnOnce(&mut Report),
{
let client = flareapp_core::current()?;
let location = Location::caller();
let mut report = make_report(location);
#[cfg(feature = "backtrace")]
report.set_frames(flareapp_backtrace::capture(&client));
configure(&mut report);
client.send_report(report)
}
#[macro_export]
macro_rules! config {
() => {
$crate::Config::new()
.service_name(::core::env!("CARGO_PKG_NAME"))
.service_version(::core::env!("CARGO_PKG_VERSION"))
.app_crate(::core::env!("CARGO_CRATE_NAME"))
.application_path(::core::env!("CARGO_MANIFEST_DIR"))
.workspace_root($crate::__private::workspace_root(
::core::env!("CARGO_MANIFEST_DIR"),
::core::file!(),
))
.git_hash($crate::__private::first_set(&[
::core::option_env!("FLARE_GIT_HASH"),
::core::option_env!("GITHUB_SHA"),
::core::option_env!("CI_COMMIT_SHA"),
::core::option_env!("CIRCLE_SHA1"),
::core::option_env!("BITBUCKET_COMMIT"),
]))
};
}
#[cfg(test)]
mod config_tests {
use std::path::Path;
#[test]
fn config_finds_the_workspace_root() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(Path::parent)
.unwrap();
let found = crate::__private::workspace_root(env!("CARGO_MANIFEST_DIR"), file!());
assert_eq!(Path::new(&found), root);
}
#[test]
fn first_set_skips_missing_and_blank_values() {
let first_set = crate::__private::first_set;
assert_eq!(
first_set(&[None, Some(" "), Some("abc"), Some("def")]),
"abc"
);
}
#[test]
fn first_set_is_empty_when_no_value_is_set() {
let first_set = crate::__private::first_set;
assert_eq!(first_set(&[None, Some("")]), "");
}
}
#[cfg(test)]
#[cfg(feature = "ureq")]
mod tests {
use std::time::Duration;
use flareapp_core::{Client, StartError};
use flareapp_otlp::{LogRecord, Severity};
use super::*;
#[test]
#[ignore = "sends a real log to Flare; needs FLARE_KEY"]
fn sends_a_log_to_flare() {
let config = Config::new()
.debug(true)
.default_transport(ureq::UreqTransport::new);
let client = match Client::start(config) {
Ok(client) => client,
Err(StartError::NoApiKey) => panic!("set FLARE_KEY to run this test"),
Err(error) => panic!("Flare did not start: {error}"),
};
client.send_log(
module_path!(),
LogRecord::new(
Severity::Info,
"Hello from the flareapp Rust client test suite".to_owned(),
),
);
assert!(client.flush(Duration::from_secs(15)));
}
}