use crate::http::StatusCode;
#[cfg(feature = "tracing")]
use crate::observability::OperationInfo;
use crate::operation::Operation;
#[cfg(feature = "tracing")]
macro_rules! debug {
($($arg:tt)*) => { tracing::debug!($($arg)*) };
}
#[cfg(not(feature = "tracing"))]
macro_rules! debug {
($($arg:tt)*) => {{}};
}
#[cfg(feature = "tracing")]
macro_rules! warning {
($($arg:tt)*) => { tracing::warn!($($arg)*) };
}
#[cfg(not(feature = "tracing"))]
macro_rules! warning {
($($arg:tt)*) => {{}};
}
pub(crate) use {debug, warning};
#[derive(Clone)]
pub(crate) struct OperationSpan {
#[cfg(feature = "tracing")]
span: tracing::Span,
}
impl OperationSpan {
pub(crate) fn new(operation: &Operation) -> OperationSpan {
#[cfg(not(feature = "tracing"))]
let _ = operation;
OperationSpan {
#[cfg(feature = "tracing")]
span: tracing::info_span!(
"hey.operation",
operation = label(operation),
service = %operation.info.service,
http.status = tracing::field::Empty,
request_id = tracing::field::Empty,
),
}
}
pub(crate) fn none() -> OperationSpan {
OperationSpan {
#[cfg(feature = "tracing")]
span: tracing::Span::none(),
}
}
#[cfg(feature = "tracing")]
pub(crate) fn wrap<F: Future>(&self, work: F) -> tracing::instrument::Instrumented<F> {
tracing::Instrument::instrument(work, self.span.clone())
}
#[cfg(not(feature = "tracing"))]
#[allow(clippy::unused_self)] pub(crate) fn wrap<F: Future>(&self, work: F) -> F {
work
}
#[allow(clippy::unused_self)] pub(crate) fn answered(&self, status: StatusCode, request_id: Option<&str>) {
#[cfg(feature = "tracing")]
{
self.span.record("http.status", status.as_u16());
if let Some(request_id) = request_id {
self.span.record("request_id", request_id);
}
}
#[cfg(not(feature = "tracing"))]
{
let _ = (status, request_id);
}
}
}
#[cfg(feature = "tracing")]
pub(crate) fn label(operation: &Operation) -> &str {
if is_raw(&operation.info) {
operation.method.as_str()
} else {
&operation.info.operation
}
}
#[cfg(feature = "tracing")]
fn is_raw(info: &OperationInfo) -> bool {
info.service == "Raw"
}
pub(crate) struct AttemptSpan {
#[cfg(feature = "tracing")]
span: tracing::Span,
}
impl AttemptSpan {
pub(crate) fn new(attempt: u32) -> AttemptSpan {
#[cfg(not(feature = "tracing"))]
let _ = attempt;
AttemptSpan {
#[cfg(feature = "tracing")]
span: tracing::debug_span!("hey.attempt", attempt, http.status = tracing::field::Empty),
}
}
#[cfg(feature = "tracing")]
pub(crate) fn wrap<F: Future>(&self, work: F) -> tracing::instrument::Instrumented<F> {
tracing::Instrument::instrument(work, self.span.clone())
}
#[cfg(not(feature = "tracing"))]
#[allow(clippy::unused_self)] pub(crate) fn wrap<F: Future>(&self, work: F) -> F {
work
}
#[allow(clippy::unused_self)] pub(crate) fn answered(&self, status: StatusCode) {
#[cfg(feature = "tracing")]
self.span.record("http.status", status.as_u16());
#[cfg(not(feature = "tracing"))]
let _ = status;
}
}