Skip to content

Commit

Permalink
lazily construct span
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Jul 21, 2023
1 parent 34e6569 commit bd171b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions src/internal/middleware/mw_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl MiddlewareContext {

#[cfg(feature = "tracing")]
pub fn with_span(mut self, span: Option<tracing::Span>) -> Self {
self.req.span = span;
self.req.span = Some(span);
self
}

Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct RequestContext {
pub kind: ProcedureKind,
pub path: Cow<'static, str>,
#[cfg(feature = "tracing")]
pub span: Option<tracing::Span>,
span: Option<Option<tracing::Span>>,
// Prevents downstream user constructing type
_priv: (),
}
Expand All @@ -74,23 +74,30 @@ impl RequestContext {
Self {
id,
#[cfg(feature = "tracing")]
span: Some(match kind {
span: None,
kind,
path,
_priv: (),
}
}

#[cfg(feature = "tracing")]
pub fn span(&self) -> Option<tracing::Span> {
self.span.clone().unwrap_or_else(|| {
Some(match self.kind {
ProcedureKind::Query => {
let query = path.as_ref();
let query = self.path.as_ref();
tracing::info_span!("rspc", query)
}
ProcedureKind::Mutation => {
let mutation = path.as_ref();
let mutation = self.path.as_ref();
tracing::info_span!("rspc", mutation)
}
ProcedureKind::Subscription => {
let subscription = path.as_ref();
let subscription = self.path.as_ref();
tracing::info_span!("rspc", subscription)
}
}),
kind,
path,
_priv: (),
}
})
})
}
}
2 changes: 1 addition & 1 deletion src/internal/middleware/resolver_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
c: RequestContext,
) -> Result<Self::Stream<'_>, ExecError> {
#[cfg(feature = "tracing")]
let span = c.span.clone();
let span = c.span();

#[cfg(feature = "tracing")]
let _enter = span.as_ref().map(|s| s.enter());
Expand Down

0 comments on commit bd171b3

Please sign in to comment.