use crate::cost::usage::UsageCoverage;
use crate::model::{ModelId, Reach};
use std::future::Future;
#[cfg(feature = "tracing")]
pub(crate) type Span = tracing::Span;
#[cfg(not(feature = "tracing"))]
#[derive(Debug, Clone)]
pub(crate) struct Span;
#[cfg(feature = "tracing")]
pub(crate) fn routing(model: &ModelId) -> Span {
tracing::info_span!(
"llmr.route",
model = model.as_str(),
route = tracing::field::Empty,
reach = tracing::field::Empty,
usage_coverage = tracing::field::Empty,
attempts = tracing::field::Empty,
fell_through = tracing::field::Empty,
)
}
#[cfg(not(feature = "tracing"))]
pub(crate) fn routing(_model: &ModelId) -> Span {
Span
}
#[cfg(feature = "tracing")]
pub(crate) fn calling(provider: &str, model: &ModelId, reach: Reach) -> Span {
tracing::info_span!(
"llmr.call",
provider = provider,
model = model.as_str(),
reach = reach.as_str(),
usage_coverage = tracing::field::Empty,
)
}
#[cfg(not(feature = "tracing"))]
pub(crate) fn calling(_provider: &str, _model: &ModelId, _reach: Reach) -> Span {
Span
}
#[cfg(feature = "tracing")]
pub(crate) fn routed(
span: &Span,
route: &str,
coverage: UsageCoverage,
attempts: u32,
fell: usize,
) {
span.record("route", route);
span.record("usage_coverage", coverage.as_str());
span.record("attempts", attempts);
span.record("fell_through", fell);
if fell > 0 {
tracing::warn!(
route,
fell_through = fell,
attempts,
"answered, but not by the first route that was tried"
);
}
}
#[cfg(not(feature = "tracing"))]
pub(crate) fn routed(
_span: &Span,
_route: &str,
_coverage: UsageCoverage,
_attempts: u32,
_fell: usize,
) {
}
#[cfg(feature = "tracing")]
pub(crate) fn measured(span: &Span, coverage: UsageCoverage) {
span.record("usage_coverage", coverage.as_str());
if coverage == UsageCoverage::Absent {
tracing::debug!("the provider reported no usage for this call");
}
}
#[cfg(not(feature = "tracing"))]
pub(crate) fn measured(_span: &Span, _coverage: UsageCoverage) {}
pub(crate) async fn inside<F: Future>(span: Span, future: F) -> F::Output {
#[cfg(feature = "tracing")]
{
use tracing::Instrument;
future.instrument(span).await
}
#[cfg(not(feature = "tracing"))]
{
let Span = span;
future.await
}
}