Merge pull request #250 from D1plo1d/tracing

Tracing Extension: Default Implementation + More Filtering + Better Readability
This commit is contained in:
Sunli 2020-08-31 15:35:17 +08:00 committed by GitHub
commit 0470ef5851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,25 +1,41 @@
use crate::extensions::{Extension, ResolveInfo}; use crate::extensions::{Extension, ResolveInfo};
use crate::{QueryPathSegment, Variables}; use crate::{Variables};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use tracing::{span, Id, Level}; use tracing::{span, event, Id, Level};
use uuid::Uuid;
/// Tracing extension /// Tracing extension
/// ///
/// # References /// # References
/// ///
/// https://crates.io/crates/tracing /// https://crates.io/crates/tracing
#[derive(Default)]
pub struct Tracing { pub struct Tracing {
root_id: Option<Id>, root_id: Option<Id>,
fields: BTreeMap<usize, Id>, fields: BTreeMap<usize, Id>,
} }
impl Extension for Tracing { impl Extension for Tracing {
fn parse_start(&mut self, query_source: &str, _variables: &Variables) { 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); let root_span: tracing::Span = span!(
target: "async_graphql::graphql",
parent:None,
Level::INFO,
"graphql",
id = %Uuid::new_v4().to_string(),
);
if let Some(id) = root_span.id() { if let Some(id) = root_span.id() {
tracing::dispatcher::get_default(|d| d.enter(&id)); tracing::dispatcher::get_default(|d| d.enter(&id));
self.root_id.replace(id); self.root_id.replace(id);
} }
event!(
target: "async_graphql::query",
Level::DEBUG,
%variables,
query = %query_source
);
} }
fn execution_end(&mut self) { fn execution_end(&mut self) {
@ -33,27 +49,15 @@ impl Extension for Tracing {
.resolve_id .resolve_id
.parent .parent
.and_then(|id| self.fields.get(&id)) .and_then(|id| self.fields.get(&id))
.or_else(|| self.root_id.as_ref())
.cloned(); .cloned();
let span = match &info.path_node.segment { let span = span!(
QueryPathSegment::Index(idx) => span!( target: "async_graphql::field",
target: "async-graphql", parent: parent_span,
parent: parent_span, Level::INFO,
Level::INFO, "field",
"field", path = %info.path_node,
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
),
};
if let Some(id) = span.id() { if let Some(id) = span.id() {
tracing::dispatcher::get_default(|d| d.enter(&id)); tracing::dispatcher::get_default(|d| d.enter(&id));
self.fields.insert(info.resolve_id.current, id); self.fields.insert(info.resolve_id.current, id);