Refactored tracing extension to allow better span and event filtering and improve signal to noise

This commit is contained in:
D1plo1d 2020-08-30 21:01:53 -04:00
parent c675b929d3
commit 5a6c77ba9d

View File

@ -1,7 +1,8 @@
use crate::extensions::{Extension, ResolveInfo};
use crate::{QueryPathSegment, Variables};
use crate::{Variables};
use std::collections::BTreeMap;
use tracing::{span, Id, Level};
use tracing::{span, event, Id, Level};
use uuid::Uuid;
/// Tracing extension
///
@ -15,12 +16,26 @@ pub struct Tracing {
}
impl Extension for Tracing {
fn parse_start(&mut self, query_source: &str, _variables: &Variables) {
let root_span = span!(target: "async-graphql", parent:None, Level::INFO, "query", source = query_source);
fn parse_start(&mut self, query_source: &str, variables: &Variables) {
let root_span: tracing::Span = span!(
target: "async_graphql",
parent:None,
Level::INFO,
"graphql",
id = %Uuid::new_v4().to_string(),
);
if let Some(id) = root_span.id() {
tracing::dispatcher::get_default(|d| d.enter(&id));
self.root_id.replace(id);
}
event!(
target: "async_graphql::query",
Level::DEBUG,
%variables,
query = %query_source
);
}
fn execution_end(&mut self) {
@ -34,27 +49,15 @@ impl Extension for Tracing {
.resolve_id
.parent
.and_then(|id| self.fields.get(&id))
.or_else(|| self.root_id.as_ref())
.cloned();
let span = match &info.path_node.segment {
QueryPathSegment::Index(idx) => span!(
target: "async-graphql",
parent: parent_span,
Level::INFO,
"field",
index = *idx,
parent_type = info.parent_type,
return_type = info.return_type
),
QueryPathSegment::Name(name) => span!(
target: "async-graphql",
parent: parent_span,
Level::INFO,
"field",
name = name,
parent_type = info.parent_type,
return_type = info.return_type
),
};
let span = span!(
target: "async_graphql::field",
parent: parent_span,
Level::INFO,
"field",
path = %info.path_node,
);
if let Some(id) = span.id() {
tracing::dispatcher::get_default(|d| d.enter(&id));
self.fields.insert(info.resolve_id.current, id);