Tracing in Rust


    pub enum Request {
        Create {
            name: &'static str,
            value: &'static str,
        },
    }
    
    #[derive(Default)]
    pub struct Handler {
        inner: std::collections::HashMap<&'static str, &'static str>,
    }
    
    impl Handler {
        pub fn handle_request(&mut self, request: Request) -> Result<(), ()> {
            match request {
                Request::Create {name, value} => self.handle_create(name, value),
            }
        }
        
        fn handle_create(&mut self, name: &'static str, value: &'static str) -> Result<(), ()> {
            if self.inner.contains_key(name) { return Err(()) }
            self.inner.insert(name, value);
            Ok(())
        }
        
    }
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let mut handler = Handler::default();
        handler.handle_request(Request::Create {name: "somename", value: "somevalue"})
            .expect("Failed :(");
        
        Ok(())
    }

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=36b6c9c5972b545ee8a2b74e66ea857d


        Finished dev [unoptimized + debuginfo] target(s) in 1.26s
         Running `target/debug/playground`


fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut handler = Handler::default();
    handler.handle_request(Request::Create {name: "somename", value: "somevalue"})
        .expect("Failed :(");
    
    handler.handle_request(Request::Create {name: "somename", value: "othervalue"})
        .expect("Failed :(");
    
    Ok(())
}


   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.21s
     Running `target/debug/playground`
thread 'main' panicked at 'Failed :(: ()', src/main.rs:45:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

    fn handle_create(&mut self, name: &'static str, value: &'static str) -> Result<(), ()> {
        if self.inner.contains_key(name) { 
            println!("Can't create a value more than once");
            return Err(()) 
        }
        self.inner.insert(name, value);
        Ok(())
    }

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.74s
     Running `target/debug/playground`
thread 'main' panicked at 'Failed :(: ()', src/main.rs:38:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Standard Output
Can't create a value more than once
#[derive(Default)]
pub struct Handler {
    inner: std::collections::HashMap<&'static str, &'static str>,
}

impl Handler {
	// Add "request" and a uuid for all events in handle_request scope
    #[tracing::instrument(skip(self), fields(request_id=?uuid::Uuid::new_v4()))]
    pub fn handle_request(&mut self, request: Request) -> Result<(), ()> {
        match request {
            Request::Create {name, value} => {
                tracing::info!("Handling create request");
                self.handle_create(name, value)
            },
        }
    }

    #[tracing::instrument(skip(self))]  // Add "name" and "value" to all events within fn scope
    fn handle_create(&mut self, name: &'static str, value: &'static str) -> Result<(), ()> {
        if self.inner.contains_key(name) {
            tracing::error!("Can't create a value more than once");
            return Err(())
        }
        self.inner.insert(name, value);
        Ok(())
    }

}


fn main() -> Result<(), Box<dyn std::error::Error>> {
    let subscriber = tracing_subscriber::FmtSubscriber::builder().finish();
    tracing::subscriber::set_global_default(subscriber)
        .expect("setting default subscriber failed");

    let mut handler = Handler::default();
    handler.handle_request(Request::Create {name: "somename", value: "somevalue"})
        .expect("Failed :(");
    
    handler.handle_request(Request::Create {name: "somename", value: "othervalue"})
        .expect("Failed :(");

    Ok(())
}

Jun 19 13:51:46.231  INFO handle_request{request=Create { name: "somename", value: "somevalue" } 
	request_id=7af6bc60-c2da-464c-9a1d-ebdcdbf58eab}: tracing_crash_course: Handling create request
Jun 19 13:51:46.232  INFO handle_request{request=Create { name: "somename", value: "othervalue" } 
	request_id=b5d894d9-32b2-45c7-a86b-7889fc0c46ee}: tracing_crash_course: Handling create request
Jun 19 13:51:46.232 ERROR handle_request{request=Create { name: "somename", value: "othervalue" } 
	request_id=b5d894d9-32b2-45c7-a86b-7889fc0c46ee}:
    		handle_create{name="somename" value="othervalue"}: 
            	tracing_crash_course: Can't create a value more than once

thread 'main' panicked at 'Failed :(: ()', src/main.rs:52:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

    let subscriber = tracing_subscriber::FmtSubscriber::builder().json().finish();
    tracing::subscriber::set_global_default(subscriber)
        .expect("setting default subscriber failed");
{
  timestamp: "Jun 19 13:53:45.328",
  level: "INFO",
  fields: {
  	message: "Handling create request"
  },
  target: "tracing_crash_course",
  span: {
    request: "Create { name: \"somename\", value: \"somevalue\" }",
    request_id: "475f48cf-a798-45e1-ae49-ee21d34a6715",
    name: "handle_request"
  },
  spans: [
  	{
      request: "Create { name: \"somename\", value: \"somevalue\" }",
      request_id: "475f48cf-a798-45e1-ae49-ee21d34a6715",
      name: "handle_request"
    }
  ]
}