Skip to content

Commit e0f7eb5

Browse files
Add tracing support
1 parent fe6d622 commit e0f7eb5

File tree

7 files changed

+116
-5
lines changed

7 files changed

+116
-5
lines changed

docs/book/content/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
- [Hyper](servers/hyper.md)
2626
- [Third Party Integrations](servers/third-party.md)
2727

28+
- [Tracing](tracing/index.md)
29+
2830
- [Advanced Topics](advanced/index.md)
2931

3032
- [Introspection](advanced/introspection.md)

docs/book/content/tracing/index.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Tracing
2+
3+
Juniper relies on the [tracing](https://crates.io/crates/tracing) crate for instrumentation.
4+
5+
!FILENAME Cargo.toml
6+
7+
```toml
8+
[dependencies]
9+
tracing = "0.1.17"
10+
tracing-subscriber = "0.2.9"
11+
tracing-log = "0.1.1"
12+
```
13+
14+
## Usage
15+
16+
```rust
17+
# extern crate tracing;
18+
# extern crate tracing_subscriber;
19+
# extern crate tracing_log;
20+
fn main() {
21+
// compatibility with the log crate (unstable)
22+
// converts all log records into tracing events
23+
tracing_log::LogTracer::init().expect("LogTracer init failed");
24+
25+
// a builder for `FmtSubscriber`.
26+
let subscriber = tracing_subscriber::FmtSubscriber::builder()
27+
// all spans/events with a level higher than TRACE
28+
// (e.g, debug, info, warn, etc.) will be written to stdout.
29+
.with_max_level(tracing::Level::TRACE)
30+
// completes the builder.
31+
.finish();
32+
33+
tracing::subscriber::set_global_default(subscriber)
34+
.expect("Setting default tracing subscriber failed");
35+
36+
// ...
37+
}
38+
```

docs/book/tests/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ skeptic = "0.13"
2020
serde_json = "1.0.39"
2121
uuid = "0.8"
2222

23+
tracing = "0.1.17"
24+
tracing-subscriber = "0.2.9"
25+
tracing-log = "0.1.1"
26+
2327
[build-dependencies]
2428
skeptic = "0.13"
2529

juniper/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ url = { version = "2", optional = true }
5252
uuid = { version = "0.8", optional = true }
5353
graphql-parser = {version = "0.3.0", optional = true }
5454

55+
tracing = {version = "0.1.17", optional = true }
56+
5557
[dev-dependencies]
5658
bencher = "0.1.2"
5759
serde_json = { version = "1.0.2" }

juniper/src/lib.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ where
260260
let errors = validate_input_values(variables, operation, &root_node.schema);
261261

262262
if !errors.is_empty() {
263-
return Err(GraphQLError::ValidationError(errors));
263+
let gql_error = GraphQLError::ValidationError(errors);
264+
__juniper_trace_error!("GraphQLError: {:?}", gql_error);
265+
266+
return Err(gql_error);
264267
}
265268
}
266269

@@ -293,7 +296,10 @@ where
293296

294297
let errors = ctx.into_errors();
295298
if !errors.is_empty() {
296-
return Err(GraphQLError::ValidationError(errors));
299+
let gql_error = GraphQLError::ValidationError(errors);
300+
__juniper_trace_error!("GraphQLError: {:?}", gql_error);
301+
302+
return Err(gql_error);
297303
}
298304
}
299305

@@ -303,7 +309,10 @@ where
303309
let errors = validate_input_values(variables, operation, &root_node.schema);
304310

305311
if !errors.is_empty() {
306-
return Err(GraphQLError::ValidationError(errors));
312+
let gql_error = GraphQLError::ValidationError(errors);
313+
__juniper_trace_error!("GraphQLError: {:?}", gql_error);
314+
315+
return Err(gql_error);
307316
}
308317
}
309318

@@ -338,7 +347,10 @@ where
338347

339348
let errors = ctx.into_errors();
340349
if !errors.is_empty() {
341-
return Err(GraphQLError::ValidationError(errors));
350+
let gql_error = GraphQLError::ValidationError(errors);
351+
__juniper_trace_error!("GraphQLError: {:?}", gql_error);
352+
353+
return Err(gql_error);
342354
}
343355
}
344356

@@ -348,7 +360,10 @@ where
348360
let errors = validate_input_values(&variables, operation, &root_node.schema);
349361

350362
if !errors.is_empty() {
351-
return Err(GraphQLError::ValidationError(errors));
363+
let gql_error = GraphQLError::ValidationError(errors);
364+
__juniper_trace_error!("GraphQLError: {:?}", gql_error);
365+
366+
return Err(gql_error);
352367
}
353368
}
354369

juniper/src/macros/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ mod interface;
1010
mod tests;
1111

1212
pub mod subscription_helpers;
13+
14+
mod tracing;

juniper/src/macros/tracing.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[doc(hidden)]
2+
#[macro_export]
3+
macro_rules! __juniper_trace_internal {
4+
($trace_type:ident; $($element:expr),*) => {{
5+
#[cfg(feature = "tracing")]
6+
tracing::$trace_type!($($element),*);
7+
}};
8+
}
9+
10+
#[doc(hidden)]
11+
#[macro_export]
12+
macro_rules! __juniper_trace {
13+
($($element:expr),*) => {{
14+
$crate::__juniper_trace_internal!(trace; $($element),*)
15+
}};
16+
}
17+
18+
#[doc(hidden)]
19+
#[macro_export]
20+
macro_rules! __juniper_trace_debug {
21+
($($element:expr),*) => {{
22+
$crate::__juniper_trace_internal!(debug; $($element),*)
23+
}};
24+
}
25+
26+
#[doc(hidden)]
27+
#[macro_export]
28+
macro_rules! __juniper_trace_info {
29+
($($element:expr),*) => {{
30+
$crate::__juniper_trace_internal!(info; $($element),*)
31+
}};
32+
}
33+
34+
#[doc(hidden)]
35+
#[macro_export]
36+
macro_rules! __juniper_trace_warn {
37+
($($element:expr),*) => {{
38+
$crate::__juniper_trace_internal!(warn; $($element),*)
39+
}};
40+
}
41+
42+
#[doc(hidden)]
43+
#[macro_export]
44+
macro_rules! __juniper_trace_error {
45+
($($element:expr),*) => {{
46+
$crate::__juniper_trace_internal!(error; $($element),*)
47+
}};
48+
}

0 commit comments

Comments
 (0)