2020-03-31 03:19:18 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-10-15 06:38:10 +00:00
|
|
|
|
2020-09-25 04:58:45 +00:00
|
|
|
use tracing::{span, Level, Span};
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2020-10-15 06:38:10 +00:00
|
|
|
use crate::extensions::{Extension, ExtensionContext, ExtensionFactory, ResolveInfo};
|
|
|
|
use crate::parser::types::ExecutableDocument;
|
2020-12-18 15:58:03 +00:00
|
|
|
use crate::{ServerError, ValidationResult, Variables};
|
2020-10-15 06:38:10 +00:00
|
|
|
|
2020-11-07 13:23:22 +00:00
|
|
|
/// Tracing extension configuration for each request.
|
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-11-07 13:23:22 +00:00
|
|
|
pub struct TracingConfig {
|
|
|
|
/// Use a span as the parent node of the entire query.
|
2020-11-07 00:22:24 +00:00
|
|
|
parent: Option<Span>,
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:23:22 +00:00
|
|
|
impl TracingConfig {
|
2020-11-07 00:22:24 +00:00
|
|
|
/// Use a span as the parent node of the entire query.
|
2020-11-07 13:23:22 +00:00
|
|
|
pub fn parent_span(mut self, span: Span) -> Self {
|
|
|
|
self.parent = Some(span);
|
|
|
|
self
|
2020-11-07 00:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 12:47:37 +00:00
|
|
|
|
2020-11-07 13:23:22 +00:00
|
|
|
/// Tracing extension
|
|
|
|
///
|
|
|
|
/// # References
|
|
|
|
///
|
|
|
|
/// <https://crates.io/crates/tracing>
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use async_graphql::*;
|
|
|
|
/// use async_graphql::extensions::{Tracing, TracingConfig};
|
|
|
|
/// use tracing::{span, Level};
|
|
|
|
///
|
|
|
|
/// #[derive(SimpleObject)]
|
|
|
|
/// struct Query {
|
|
|
|
/// value: i32,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let schema = Schema::build(Query { value: 100 }, EmptyMutation, EmptySubscription).
|
|
|
|
/// extension(Tracing::default())
|
|
|
|
/// .finish();
|
|
|
|
///
|
|
|
|
/// let root_span = span!(
|
|
|
|
/// parent: None,
|
|
|
|
/// Level::INFO,
|
|
|
|
/// "span root"
|
|
|
|
/// );
|
|
|
|
///
|
2021-03-12 04:47:24 +00:00
|
|
|
/// tokio::runtime::Runtime::new().unwrap().block_on(async move {
|
2020-11-07 13:23:22 +00:00
|
|
|
/// let request = Request::new("{ value }")
|
|
|
|
/// .data(TracingConfig::default().parent_span(root_span));
|
|
|
|
/// schema.execute(request).await;
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
#[derive(Default)]
|
|
|
|
#[cfg_attr(feature = "nightly", doc(cfg(feature = "tracing")))]
|
|
|
|
pub struct Tracing;
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
impl ExtensionFactory for Tracing {
|
|
|
|
fn create(&self) -> Box<dyn Extension> {
|
2020-11-07 13:23:22 +00:00
|
|
|
Box::new(TracingExtension::default())
|
2020-09-29 12:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct TracingExtension {
|
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-09-29 12:47:37 +00:00
|
|
|
impl Extension for TracingExtension {
|
|
|
|
fn parse_start(
|
|
|
|
&mut self,
|
2020-11-07 13:23:22 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
2020-09-29 12:47:37 +00:00
|
|
|
query_source: &str,
|
|
|
|
_variables: &Variables,
|
|
|
|
) {
|
2020-11-07 13:23:22 +00:00
|
|
|
let parent_span = ctx
|
|
|
|
.data_opt::<TracingConfig>()
|
|
|
|
.and_then(|cfg| cfg.parent.as_ref());
|
|
|
|
|
|
|
|
let root_span = match parent_span {
|
2020-11-07 00:22:24 +00:00
|
|
|
Some(parent) => span!(
|
|
|
|
target: "async_graphql::graphql",
|
2020-11-07 13:23:22 +00:00
|
|
|
parent: parent,
|
2020-11-07 00:22:24 +00:00
|
|
|
Level::INFO,
|
|
|
|
"query",
|
|
|
|
source = %query_source
|
|
|
|
),
|
|
|
|
None => span!(
|
|
|
|
target: "async_graphql::graphql",
|
|
|
|
parent: None,
|
|
|
|
Level::INFO,
|
|
|
|
"query",
|
|
|
|
source = %query_source
|
|
|
|
),
|
|
|
|
};
|
2020-08-31 01:01:53 +00:00
|
|
|
|
2020-09-25 04:58:45 +00:00
|
|
|
let parse_span = span!(
|
|
|
|
target: "async_graphql::graphql",
|
|
|
|
parent: &root_span,
|
|
|
|
Level::INFO,
|
|
|
|
"parse"
|
|
|
|
);
|
|
|
|
|
2021-03-20 02:31:39 +00:00
|
|
|
enter_span(&root_span);
|
2020-09-25 04:58:45 +00:00
|
|
|
self.root.replace(root_span);
|
|
|
|
|
2021-03-20 02:31:39 +00:00
|
|
|
enter_span(&parse_span);
|
2020-09-25 04:58:45 +00:00
|
|
|
self.parse.replace(parse_span);
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn parse_end(&mut self, _ctx: &ExtensionContext<'_>, _document: &ExecutableDocument) {
|
2021-03-20 02:31:39 +00:00
|
|
|
if let Some(span) = self.parse.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
2020-09-25 04:58:45 +00:00
|
|
|
}
|
2020-08-31 01:01:53 +00:00
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn validation_start(&mut self, _ctx: &ExtensionContext<'_>) {
|
2020-09-26 04:35:28 +00:00
|
|
|
if let Some(parent) = &self.root {
|
|
|
|
let validation_span = span!(
|
|
|
|
target: "async_graphql::graphql",
|
|
|
|
parent: parent,
|
|
|
|
Level::INFO,
|
|
|
|
"validation"
|
|
|
|
);
|
2021-03-20 02:31:39 +00:00
|
|
|
enter_span(&validation_span);
|
2020-09-26 04:35:28 +00:00
|
|
|
self.validation.replace(validation_span);
|
|
|
|
}
|
2020-09-25 04:58:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:58:03 +00:00
|
|
|
fn validation_end(&mut self, _ctx: &ExtensionContext<'_>, _result: &ValidationResult) {
|
2021-03-20 02:31:39 +00:00
|
|
|
if let Some(span) = self.validation.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
2020-09-25 04:58:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn execution_start(&mut self, _ctx: &ExtensionContext<'_>) {
|
2020-09-26 07:52:59 +00:00
|
|
|
let execute_span = if let Some(parent) = &self.root {
|
|
|
|
span!(
|
2020-09-26 04:35:28 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-03-20 02:31:39 +00:00
|
|
|
enter_span(&execute_span);
|
2020-09-26 07:52:59 +00:00
|
|
|
self.execute.replace(execute_span);
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn execution_end(&mut self, _ctx: &ExtensionContext<'_>) {
|
2021-03-20 02:31:39 +00:00
|
|
|
if let Some(span) = self.execute.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
|
|
|
if let Some(span) = self.root.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn resolve_start(&mut self, _ctx: &ExtensionContext<'_>, info: &ResolveInfo<'_>) {
|
2020-09-25 04:58:45 +00:00
|
|
|
let parent_span = match info.resolve_id.parent {
|
2020-09-26 04:35:28 +00:00
|
|
|
Some(parent_id) if parent_id > 0 => self.fields.get(&parent_id),
|
|
|
|
_ => self.execute.as_ref(),
|
2020-09-25 04:58:45 +00:00
|
|
|
};
|
|
|
|
|
2020-09-26 04:35:28 +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,
|
2020-09-26 04:35:28 +00:00
|
|
|
);
|
2021-03-20 02:31:39 +00:00
|
|
|
enter_span(&span);
|
2020-09-26 04:35:28 +00:00
|
|
|
self.fields.insert(info.resolve_id.current, span);
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
fn resolve_end(&mut self, _ctx: &ExtensionContext<'_>, info: &ResolveInfo<'_>) {
|
2020-09-25 04:58:45 +00:00
|
|
|
if let Some(span) = self.fields.remove(&info.resolve_id.current) {
|
2021-03-20 02:31:39 +00:00
|
|
|
exit_span(span);
|
2020-04-28 07:01:19 +00:00
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
2020-09-26 01:49:46 +00:00
|
|
|
|
2020-09-30 17:24:24 +00:00
|
|
|
fn error(&mut self, _ctx: &ExtensionContext<'_>, err: &ServerError) {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
tracing::error!(target: "async_graphql::graphql", error = %err.message);
|
2020-09-26 04:35:28 +00:00
|
|
|
|
2021-03-20 02:31:39 +00:00
|
|
|
for (_, span) in std::mem::take(&mut self.fields) {
|
|
|
|
exit_span(span);
|
2020-09-26 04:35:28 +00:00
|
|
|
}
|
|
|
|
self.fields.clear();
|
|
|
|
|
2021-03-20 02:31:39 +00:00
|
|
|
if let Some(span) = self.execute.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
|
|
|
if let Some(span) = self.validation.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
|
|
|
if let Some(span) = self.parse.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
|
|
|
if let Some(span) = self.root.take() {
|
|
|
|
exit_span(span);
|
|
|
|
}
|
2020-09-26 01:49:46 +00:00
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
2021-03-20 02:31:39 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn enter_span(span: &Span) {
|
|
|
|
let _enter = span.enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn exit_span(span: Span) {
|
|
|
|
let _enter = span.enter();
|
|
|
|
}
|