- 
                Notifications
    
You must be signed in to change notification settings  - Fork 441
 
Add tracing support #720
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
          
     Open
      
      
            mihai-dinculescu
  wants to merge
  56
  commits into
  graphql-rust:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
mihai-dinculescu:add_tracing_support
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Add tracing support #720
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 419520d
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato c1f9e5f
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 753c80f
              
                Rocket can now compile on stable
              
              
                LegNeato c599bae
              
                Remove `boxed` in favor of `pin`.
              
              
                LegNeato b556934
              
                Add tracing support example
              
              
                LegNeato 0134da3
              
                Add some coarse execution tracing
              
              
                LegNeato 5c888a8
              
                Remove old comment / docs
              
              
                LegNeato ef2da9b
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 01b3453
              
                Fix book tests
              
              
                LegNeato 7ac7304
              
                fix up some imports
              
              
                LegNeato 06687dd
              
                Add back subscriber Cargo.toml instructions
              
              
                LegNeato 697a743
              
                Change trace errors to trace
              
              
                mihai-dinculescu f2977d5
              
                Tracing unit tests
              
              
                mihai-dinculescu 6c3654c
              
                Tracing unit tests names
              
              
                mihai-dinculescu 7f746de
              
                Revert "Rocket can now compile on stable"
              
              
                LegNeato 64cad08
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato d7acba9
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 88dc9c4
              
                Fix tracing spans
              
              
                LegNeato 8a2f6ec
              
                Do not include trailing newline on the last error
              
              
                LegNeato 83fd44d
              
                Standard tracing labels on snake_case
              
              
                LegNeato c92dd87
              
                Add a validation error case to the tracing example
              
              
                LegNeato 64d8e11
              
                Use $crate everywhere
              
              
                LegNeato cdbc0ce
              
                Add other levels to span macros
              
              
                LegNeato 65ab688
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 6cf48d9
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 5f8a4ca
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato ae1054e
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato 5938345
              
                Use the released tracing crate
              
              
                mihai-dinculescu 8bc07f6
              
                Fix email address
              
              
                LegNeato 82497d3
              
                Standardize on snake case for tracing
              
              
                LegNeato 79102a3
              
                Add tracing support
              
              
                mihai-dinculescu 6a16cf8
              
                Rocket can now compile on stable
              
              
                LegNeato 11bb5e2
              
                Add tracing support example
              
              
                LegNeato 57ecb56
              
                Add some coarse execution tracing
              
              
                LegNeato cff6678
              
                Remove old comment / docs
              
              
                LegNeato cb3ee27
              
                Fix book tests
              
              
                LegNeato 0d52c50
              
                fix up some imports
              
              
                LegNeato 7f24509
              
                Add back subscriber Cargo.toml instructions
              
              
                LegNeato cb7ab0c
              
                Change trace errors to trace
              
              
                mihai-dinculescu 7b02ccc
              
                Tracing unit tests
              
              
                mihai-dinculescu 4000f1e
              
                Tracing unit tests names
              
              
                mihai-dinculescu e968c4b
              
                Revert "Rocket can now compile on stable"
              
              
                LegNeato 58fed76
              
                Fix tracing spans
              
              
                LegNeato 7b5ad7e
              
                Do not include trailing newline on the last error
              
              
                LegNeato c34a1c2
              
                Standard tracing labels on snake_case
              
              
                LegNeato 26407e2
              
                Add a validation error case to the tracing example
              
              
                LegNeato 94279d1
              
                Use $crate everywhere
              
              
                LegNeato bf35077
              
                Add other levels to span macros
              
              
                LegNeato 2365016
              
                Use the released tracing crate
              
              
                mihai-dinculescu ca744b5
              
                Fix email address
              
              
                LegNeato 62acd3f
              
                Standardize on snake case for tracing
              
              
                LegNeato 4d7797a
              
                Merge master
              
              
                LegNeato 72a28d4
              
                Remove rustfmt option causing breakage
              
              
                LegNeato 2977b1f
              
                Remove subscription helper mod
              
              
                LegNeato 264030f
              
                Merge branch 'master' into add_tracing_support
              
              
                LegNeato File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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(); | ||
| } | ||
| ``` | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /target/ | ||
| **/*.rs.bk | ||
| Cargo.lock | ||
| 
     | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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]>"] | ||
| 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"] } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.