Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing support #720

Open
wants to merge 56 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e0f7eb5
Add tracing support
mihai-dinculescu Jul 25, 2020
419520d
Merge branch 'master' into add_tracing_support
LegNeato Jul 28, 2020
c1f9e5f
Merge branch 'master' into add_tracing_support
LegNeato Jul 29, 2020
753c80f
Rocket can now compile on stable
LegNeato Jul 28, 2020
c599bae
Remove `boxed` in favor of `pin`.
LegNeato Jul 29, 2020
b556934
Add tracing support example
LegNeato Jul 29, 2020
0134da3
Add some coarse execution tracing
LegNeato Jul 29, 2020
5c888a8
Remove old comment / docs
LegNeato Jul 29, 2020
ef2da9b
Merge branch 'master' into add_tracing_support
LegNeato Jul 29, 2020
01b3453
Fix book tests
LegNeato Jul 29, 2020
7ac7304
fix up some imports
LegNeato Jul 29, 2020
06687dd
Add back subscriber Cargo.toml instructions
LegNeato Jul 29, 2020
697a743
Change trace errors to trace
mihai-dinculescu Jul 29, 2020
f2977d5
Tracing unit tests
mihai-dinculescu Jul 29, 2020
6c3654c
Tracing unit tests names
mihai-dinculescu Jul 29, 2020
7f746de
Revert "Rocket can now compile on stable"
LegNeato Jul 29, 2020
64cad08
Merge branch 'master' into add_tracing_support
LegNeato Jul 30, 2020
d7acba9
Merge branch 'master' into add_tracing_support
LegNeato Aug 10, 2020
88dc9c4
Fix tracing spans
LegNeato Aug 14, 2020
8a2f6ec
Do not include trailing newline on the last error
LegNeato Aug 14, 2020
83fd44d
Standard tracing labels on snake_case
LegNeato Aug 14, 2020
c92dd87
Add a validation error case to the tracing example
LegNeato Aug 14, 2020
64d8e11
Use $crate everywhere
LegNeato Aug 15, 2020
cdbc0ce
Add other levels to span macros
LegNeato Aug 15, 2020
65ab688
Merge branch 'master' into add_tracing_support
LegNeato Aug 19, 2020
6cf48d9
Merge branch 'master' into add_tracing_support
LegNeato Aug 21, 2020
5f8a4ca
Merge branch 'master' into add_tracing_support
LegNeato Sep 3, 2020
ae1054e
Merge branch 'master' into add_tracing_support
LegNeato Oct 3, 2020
5938345
Use the released tracing crate
mihai-dinculescu Oct 3, 2020
8bc07f6
Fix email address
LegNeato Oct 29, 2020
82497d3
Standardize on snake case for tracing
LegNeato Oct 29, 2020
79102a3
Add tracing support
mihai-dinculescu Jul 25, 2020
6a16cf8
Rocket can now compile on stable
LegNeato Jul 28, 2020
11bb5e2
Add tracing support example
LegNeato Jul 29, 2020
57ecb56
Add some coarse execution tracing
LegNeato Jul 29, 2020
cff6678
Remove old comment / docs
LegNeato Jul 29, 2020
cb3ee27
Fix book tests
LegNeato Jul 29, 2020
0d52c50
fix up some imports
LegNeato Jul 29, 2020
7f24509
Add back subscriber Cargo.toml instructions
LegNeato Jul 29, 2020
cb7ab0c
Change trace errors to trace
mihai-dinculescu Jul 29, 2020
7b02ccc
Tracing unit tests
mihai-dinculescu Jul 29, 2020
4000f1e
Tracing unit tests names
mihai-dinculescu Jul 29, 2020
e968c4b
Revert "Rocket can now compile on stable"
LegNeato Jul 29, 2020
58fed76
Fix tracing spans
LegNeato Aug 14, 2020
7b5ad7e
Do not include trailing newline on the last error
LegNeato Aug 14, 2020
c34a1c2
Standard tracing labels on snake_case
LegNeato Aug 14, 2020
26407e2
Add a validation error case to the tracing example
LegNeato Aug 14, 2020
94279d1
Use $crate everywhere
LegNeato Aug 15, 2020
bf35077
Add other levels to span macros
LegNeato Aug 15, 2020
2365016
Use the released tracing crate
mihai-dinculescu Oct 3, 2020
ca744b5
Fix email address
LegNeato Oct 29, 2020
62acd3f
Standardize on snake case for tracing
LegNeato Oct 29, 2020
4d7797a
Merge master
LegNeato Oct 29, 2020
72a28d4
Remove rustfmt option causing breakage
LegNeato Oct 29, 2020
2977b1f
Remove subscription helper mod
LegNeato Oct 29, 2020
264030f
Merge branch 'master' into add_tracing_support
LegNeato Nov 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"juniper_codegen",
"juniper",
"examples/basic_subscriptions",
"examples/tracing_support",
"examples/warp_async",
"examples/warp_subscriptions",
"examples/actix_subscriptions",
Expand Down
2 changes: 2 additions & 0 deletions docs/book/content/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- [Hyper](servers/hyper.md)
- [Third Party Integrations](servers/third-party.md)

- [Tracing](tracing/index.md)

- [Advanced Topics](advanced/index.md)

- [Introspection](advanced/introspection.md)
Expand Down
66 changes: 66 additions & 0 deletions docs/book/content/tracing/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Tracing

Juniper has optional support for the [tracing](https://crates.io/crates/tracing) crate for instrumentation.

This feature is off by default and can be enabled via the `tracing` feature.

!FILENAME Cargo.toml

```toml
[dependencies]
juniper = { version = "0.14.2", features = ["default", "tracing"]}
tracing = "0.1.17"
tracing-subscriber = "0.2.9"
```

## Usage

```rust
# extern crate juniper;
extern crate tokio;
extern crate tracing;
extern crate tracing_subscriber;
use juniper::{EmptyMutation, EmptySubscription, RootNode, graphql_object, Variables};

#[derive(Clone, Copy, Debug)]
struct Query;

#[graphql_object]
impl Query {
async fn foo() -> i32 {
42
}
}

type Schema = RootNode<'static, Query, EmptyMutation<()>, EmptySubscription<()>>;


#[tokio::main]
async fn main() {
// Set up the tracing subscriber.
let subscriber = tracing_subscriber::fmt()
// This enables standard env variables such as `RUST_LOG=trace`.
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
// This makes it so we can see all span events.
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
.finish();

tracing::subscriber::set_global_default(subscriber)
.expect("Setting default tracing subscriber failed");

// Set up GraphQL information.
let vars = Variables::new();
let root = Schema::new(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);

// When run with `RUST_LOG=trace cargo run`, this should output traces /
// span events to `stdout`.
let query = "{ foo }";
let (_, _errors) = juniper::execute(query, None, &root, &vars, &())
.await
.unwrap();
}
```
7 changes: 5 additions & 2 deletions docs/book/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ edition = "2018"
build = "build.rs"

[dependencies]
juniper = { path = "../../../juniper" }
juniper = { path = "../../../juniper", features = ["default", "tracing"] }
juniper_iron = { path = "../../../juniper_iron" }
juniper_subscriptions = { path = "../../../juniper_subscriptions" }

derive_more = "0.99.7"
futures = "0.3"
tokio = { version = "0.2", features = ["rt-core", "blocking", "stream", "rt-util"] }
tokio = { version = "0.2", features = ["rt-core", "blocking", "stream", "rt-util", "macros"] }
iron = "0.5.0"
mount = "0.4.0"

skeptic = "0.13"
serde_json = "1.0.39"
uuid = "0.8"

tracing = { git = "https://github.com/tokio-rs/tracing.git", branch = "davidbarsky/add-instrument-trait-to-tracing"}
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing.git", branch = "davidbarsky/add-instrument-trait-to-tracing"}

[build-dependencies]
skeptic = "0.13"

Expand Down
4 changes: 4 additions & 0 deletions examples/tracing_support/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
**/*.rs.bk
Cargo.lock

14 changes: 14 additions & 0 deletions examples/tracing_support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "tracing_support"
version = "0.1.0"
authors = ["Christian Legnitto <[email protected]>"]
LegNeato marked this conversation as resolved.
Show resolved Hide resolved
edition = "2018"
publish = false

[dependencies]
tracing = { git = "https://github.com/tokio-rs/tracing.git", branch = "davidbarsky/add-instrument-trait-to-tracing" }
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing.git", branch = "davidbarsky/add-instrument-trait-to-tracing"}
# Note instead of a path you would use the proper Juniper version in your own
# project.
juniper = { path = "../../juniper", features = ["tracing"] }
tokio = { version = "0.2.22", features = ["rt-core", "macros", "blocking"] }
20 changes: 20 additions & 0 deletions examples/tracing_support/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tasks.run]
disabled = true

[tasks.release]
disabled = true

[tasks.release-some]
disabled = true

[tasks.release-local-test]
disabled = true

[tasks.release-some-local-test]
disabled = true

[tasks.release-dry-run]
disabled = true

[tasks.release-some-dry-run]
disabled = true
121 changes: 121 additions & 0 deletions examples/tracing_support/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
extern crate juniper;
extern crate tokio;
extern crate tracing;
extern crate tracing_subscriber;

use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldError, GraphQLEnum, RootNode, Variables,
};
use tracing::{trace_span, Instrument as _};
use tracing_subscriber::EnvFilter;

#[derive(Clone, Copy, Debug)]
struct Context;
impl juniper::Context for Context {}

#[derive(Clone, Copy, Debug, GraphQLEnum)]
enum UserKind {
Admin,
User,
Guest,
}

#[derive(Clone, Debug)]
struct User {
id: i32,
kind: UserKind,
name: String,
}

#[graphql_object(Context = Context)]
impl User {
fn id(&self) -> i32 {
self.id
}

fn kind(&self) -> UserKind {
self.kind
}

fn name(&self) -> &str {
&self.name
}

async fn friends(&self) -> Vec<User> {
vec![]
}
}

#[derive(Clone, Copy, Debug)]
struct Query;

#[graphql_object(Context = Context)]
impl Query {
async fn users() -> Vec<User> {
vec![User {
id: 1,
kind: UserKind::Admin,
name: "user1".into(),
}]
}

fn bob() -> User {
User {
id: 1,
kind: UserKind::Admin,
name: "Bob".into(),
}
}

/// Double the provided number.
async fn double(x: i32) -> Result<i32, FieldError> {
Ok(x * 2)
}
}

type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;

fn schema() -> Schema {
Schema::new(
Query,
EmptyMutation::<Context>::new(),
EmptySubscription::<Context>::new(),
)
}

#[tokio::main]
async fn main() {
// A builder for `FmtSubscriber`.
let subscriber = tracing_subscriber::fmt()
// This enables standard env variables such as `RUST_LOG=trace`.
.with_env_filter(EnvFilter::from_default_env())
// This makes it so we can see all span events.
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
.finish();

tracing::subscriber::set_global_default(subscriber).expect("no global subscriber has been set");

let ctx = Context {};
let vars = Variables::new();
let root = schema();

// When run with `RUST_LOG=trace cargo run`, this should output to `stdout`.
let query = "{ users { id } }";
let (_, _errors) = juniper::execute(query, None, &root, &vars, &ctx)
.await
.unwrap();

// When run with `RUST_LOG=trace cargo run`, this should output to `stdout`.
// Note that there is a top-level span of "doubling{42}" as it was set
// here. This is useful to attach context to each call to `execute`.
let query = "{ double(x: 42) }";
let (_, _errors) = juniper::execute(query, None, &root, &vars, &ctx)
.instrument(trace_span!("doubling", "{}", 42))
.await
.unwrap();

// You can also trace sync execution.
// This should output a validation error in the middle of other spans.
let query = "{ bob { field_that_does_not_exist } }";
let _ = juniper::execute_sync(query, None, &root, &vars, &ctx);
}
2 changes: 2 additions & 0 deletions juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ url = { version = "2", optional = true }
uuid = { version = "0.8", optional = true }
graphql-parser = {version = "0.3.0", optional = true }

tracing = { git = "https://github.com/tokio-rs/tracing.git", branch = "davidbarsky/add-instrument-trait-to-tracing", optional = true }

[dev-dependencies]
bencher = "0.1.2"
serde_json = { version = "1.0.2" }
Expand Down
Loading