async-graphql/src/extensions/tracing.rs

153 lines
4.5 KiB
Rust
Raw Normal View History

2020-03-26 03:34:28 +00:00
use crate::extensions::{Extension, ResolveInfo};
use crate::{Error, Variables};
2020-09-25 04:58:45 +00:00
use async_graphql_parser::types::ExecutableDocument;
2020-03-31 03:19:18 +00:00
use std::collections::BTreeMap;
2020-09-25 04:58:45 +00:00
use tracing::{span, Level, Span};
2020-03-26 03:34:28 +00:00
2020-04-28 07:01:19 +00:00
/// Tracing extension
///
/// # References
///
2020-09-15 18:32:13 +00:00
/// <https://crates.io/crates/tracing>
2020-08-30 20:32:14 +00:00
#[derive(Default)]
2020-09-15 18:32:13 +00:00
#[cfg_attr(feature = "nightly", doc(cfg(feature = "tracing")))]
2020-04-28 07:01:19 +00:00
pub struct Tracing {
2020-09-25 04:58:45 +00:00
root: Option<Span>,
parse: Option<Span>,
validation: Option<Span>,
execute: Option<Span>,
fields: BTreeMap<usize, Span>,
2020-03-26 03:34:28 +00:00
}
2020-04-28 07:01:19 +00:00
impl Extension for Tracing {
2020-09-25 04:58:45 +00:00
fn parse_start(&mut self, query_source: &str, _variables: &Variables) {
let root_span = span!(
target: "async_graphql::graphql",
2020-09-25 04:58:45 +00:00
parent: None,
Level::INFO,
2020-09-25 04:58:45 +00:00
"query",
source = %query_source
);
2020-09-25 04:58:45 +00:00
let parse_span = span!(
target: "async_graphql::graphql",
parent: &root_span,
Level::INFO,
"parse"
);
root_span.with_subscriber(|(id, d)| d.enter(id));
self.root.replace(root_span);
parse_span.with_subscriber(|(id, d)| d.enter(id));
self.parse.replace(parse_span);
}
fn parse_end(&mut self, _document: &ExecutableDocument) {
self.parse
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
2020-09-25 04:58:45 +00:00
}
2020-09-25 04:58:45 +00:00
fn validation_start(&mut self) {
if let Some(parent) = &self.root {
let validation_span = span!(
target: "async_graphql::graphql",
parent: parent,
Level::INFO,
"validation"
);
validation_span.with_subscriber(|(id, d)| d.enter(id));
self.validation.replace(validation_span);
}
2020-09-25 04:58:45 +00:00
}
fn validation_end(&mut self) {
self.validation
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
2020-09-25 04:58:45 +00:00
}
fn execution_start(&mut self) {
2020-09-26 07:52:59 +00:00
let execute_span = if let Some(parent) = &self.root {
span!(
target: "async_graphql::graphql",
parent: parent,
Level::INFO,
"execute"
2020-09-26 07:52:59 +00:00
)
} else {
// For every step of the subscription stream.
span!(
target: "async_graphql::graphql",
parent: None,
Level::INFO,
"execute"
)
};
execute_span.with_subscriber(|(id, d)| d.enter(id));
self.execute.replace(execute_span);
2020-03-26 03:34:28 +00:00
}
fn execution_end(&mut self) {
2020-09-25 04:58:45 +00:00
self.execute
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
2020-09-25 04:58:45 +00:00
self.root
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
2020-03-26 03:34:28 +00:00
}
fn resolve_start(&mut self, info: &ResolveInfo<'_>) {
2020-09-25 04:58:45 +00:00
let parent_span = match info.resolve_id.parent {
Some(parent_id) if parent_id > 0 => self.fields.get(&parent_id),
_ => self.execute.as_ref(),
2020-09-25 04:58:45 +00:00
};
if let Some(parent_span) = parent_span {
let span = span!(
target: "async_graphql::graphql",
parent: parent_span,
Level::INFO,
"field",
id = %info.resolve_id.current,
2020-09-26 07:52:59 +00:00
path = %info.path_node,
parent_type = %info.parent_type,
return_type = %info.return_type,
);
span.with_subscriber(|(id, d)| d.enter(id));
self.fields.insert(info.resolve_id.current, span);
}
2020-03-26 03:34:28 +00:00
}
fn resolve_end(&mut self, info: &ResolveInfo<'_>) {
2020-09-25 04:58:45 +00:00
if let Some(span) = self.fields.remove(&info.resolve_id.current) {
span.with_subscriber(|(id, d)| d.exit(id));
2020-04-28 07:01:19 +00:00
}
2020-03-26 03:34:28 +00:00
}
fn error(&mut self, err: &Error) {
tracing::error!(target: "async_graphql::graphql", error = %err.to_string());
for span in self.fields.values() {
span.with_subscriber(|(id, d)| d.exit(id));
}
self.fields.clear();
self.execute
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
self.validation
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
self.parse
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
self.root
.take()
.and_then(|span| span.with_subscriber(|(id, d)| d.exit(id)));
}
2020-03-26 03:34:28 +00:00
}