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::{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
///
/// # References
///
/// https://crates.io/crates/tracing
#[derive(Default)]
pub struct Tracing {
root_id: Option<Id>,
fields: BTreeMap<usize, Id>,
}
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::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) {
@ -33,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);