feat: adds trace events to server

This commit is contained in:
Avery Harnish
2020-03-03 12:31:49 -06:00
parent 80511f2cb5
commit 2b6b19ebc3
7 changed files with 651 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
use crate::{Connection, Frame, Kv, Parse, ParseError};
use std::io;
use tracing::{debug, instrument};
#[derive(Debug)]
pub struct Get {
@@ -8,18 +9,31 @@ pub struct Get {
}
impl Get {
// instrumenting functions will log all of the arguments passed to the function
// with their debug implementations
// see https://docs.rs/tracing/0.1.13/tracing/attr.instrument.html
#[instrument]
pub(crate) fn parse(parse: &mut Parse) -> Result<Get, ParseError> {
let key = parse.next_string()?;
// adding this debug event allows us to see what key is parsed
// the ? sigil tells `tracing` to use the `Debug` implementation
// get parse events can be filtered by running
// RUST_LOG=mini_redis::cmd::get[parse]=debug cargo run --bin server
// see https://docs.rs/tracing/0.1.13/tracing/#recording-fields
debug!(?key);
Ok(Get { key })
}
#[instrument]
pub(crate) async fn apply(self, kv: &Kv, dst: &mut Connection) -> io::Result<()> {
let response = if let Some(value) = kv.get(&self.key) {
Frame::Bulk(value)
} else {
Frame::Null
};
debug!(?response);
dst.write_frame(&response).await
}
}

View File

@@ -4,6 +4,7 @@ use crate::{Connection, Frame, Kv};
use bytes::Bytes;
use std::io;
use std::time::Duration;
use tracing::{debug, instrument};
#[derive(Debug)]
pub struct Set {
@@ -13,6 +14,7 @@ pub struct Set {
}
impl Set {
#[instrument]
pub(crate) fn parse(parse: &mut Parse) -> Result<Set, ParseError> {
use ParseError::EndOfStream;
@@ -34,14 +36,18 @@ impl Set {
Err(err) => return Err(err),
}
debug!(?key, ?value, ?expire);
Ok(Set { key, value, expire })
}
#[instrument]
pub(crate) async fn apply(self, kv: &Kv, dst: &mut Connection) -> io::Result<()> {
// Set the value
kv.set(self.key, self.value, self.expire);
let response = Frame::Simple("OK".to_string());
debug!(?response);
dst.write_frame(&response).await
}
}