#[cfg(feature = "tracing")]
use std::fmt::Write as _;
#[cfg(feature = "tracing")]
pub(crate) fn format_path(path: &[crate::PathSegment]) -> String {
let mut out = String::new();
for segment in path {
match segment {
crate::PathSegment::Field(name) => {
if !out.is_empty() {
out.push('.');
}
out.push_str(name);
}
crate::PathSegment::Index(index) => {
let _ = write!(out, "[{index}]");
}
}
}
out
}
#[cfg(feature = "tracing")]
pub(crate) fn field_type_label(field: Option<&crate::types::Field>) -> String {
field.map_or_else(
|| String::from("<unresolved>"),
|field| field.ty.to_string(),
)
}
#[cfg(feature = "tracing")]
macro_rules! field_span {
($parent:expr, $field:expr, $path:expr, $ty:expr, $subfields:expr, $resolver:expr, $fast:expr) => {{
if ::tracing::enabled!(target: "gql::field", ::tracing::Level::DEBUG) {
::tracing::debug_span!(
target: "gql::field",
"gql.field",
parent_type = $parent,
field = $field,
path = %$crate::telemetry::format_path($path),
return_type = %$ty,
subfields = $subfields,
resolver = $resolver,
fast_path = $fast,
errors = ::tracing::field::Empty,
)
} else {
::tracing::Span::none()
}
}};
}
#[cfg(feature = "tracing")]
pub(crate) use field_span;
#[cfg(not(feature = "tracing"))]
macro_rules! field_span {
($parent:expr, $field:expr, $path:expr, $ty:expr, $subfields:expr, $resolver:expr, $fast:expr) => {
()
};
}
#[cfg(not(feature = "tracing"))]
pub(crate) use field_span;
#[cfg(feature = "tracing")]
macro_rules! instrument_span_if_enabled {
($span:expr, $future:expr) => {{
let span = $span;
if span.is_disabled() {
$future.await
} else {
::tracing::Instrument::instrument($future, span).await
}
}};
}
#[cfg(feature = "tracing")]
pub(crate) use instrument_span_if_enabled;
#[cfg(not(feature = "tracing"))]
macro_rules! instrument_span_if_enabled {
($span:expr, $future:expr) => {
$future.await
};
}
#[cfg(not(feature = "tracing"))]
pub(crate) use instrument_span_if_enabled;
#[cfg(feature = "tracing")]
macro_rules! record_field_outcome {
($span:expr, $outcome:expr) => {{
let span = &$span;
if !span.is_disabled() {
match &$outcome {
Ok(_) => {
span.record("errors", 0usize);
}
Err(error) => {
span.record("errors", 1usize);
if ::tracing::enabled!(target: "gql::field", ::tracing::Level::DEBUG) {
::tracing::event!(
target: "gql::field",
parent: span,
::tracing::Level::DEBUG,
error = %error.message(),
"gql.field.error"
);
}
}
}
}
}};
}
#[cfg(feature = "tracing")]
pub(crate) use record_field_outcome;
#[cfg(not(feature = "tracing"))]
macro_rules! record_field_outcome {
($span:expr, $outcome:expr) => {{}};
}
#[cfg(not(feature = "tracing"))]
pub(crate) use record_field_outcome;