Skip to content

Commit

Permalink
Merge pull request #453 from answerbook/mdeltito/LOG-19789
Browse files Browse the repository at this point in the history
fix(user_trace): log http error responses as captured_data
  • Loading branch information
mdeltito committed Apr 30, 2024
2 parents 60f375f + 6870759 commit ee3fac9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/mezmo/user_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ pub trait UserLoggingResponse {
fn log_msg(&self) -> Option<Value> {
None
}

fn log_captured_data(&self) -> Option<Value> {
None
}
}

pub trait UserLoggingError {
Expand Down Expand Up @@ -330,7 +334,14 @@ where
match &res {
Ok(response) => {
if let Some(msg) = response.log_msg() {
user_log_error!(ctx, msg);
match response.log_captured_data() {
Some(captured_data) => {
user_log_error!(ctx, msg, captured_data: captured_data);
}
None => {
user_log_error!(ctx, msg);
}
}
}
}
Err(err) => {
Expand Down Expand Up @@ -748,6 +759,12 @@ mod tests {
fn log_msg(&self) -> Option<Value> {
Some("log_msg(): response".into())
}

fn log_captured_data(&self) -> Option<Value> {
Some(Value::Object(btreemap! {
"response" => r#"{"error": "badness"}"#
}))
}
}

struct MockWrapperService {
Expand Down Expand Up @@ -828,5 +845,16 @@ mod tests {

let msg = log_res.get(".message").unwrap().as_str().unwrap();
assert_eq!(msg, "log_msg(): response");

let captured_data = log_res
.get(".meta.mezmo.captured_data")
.expect("captured data should exist");

assert_eq!(
captured_data,
&Value::Object(btreemap! {
"response" => r#"{"error": "badness"}"#
})
);
}
}
6 changes: 6 additions & 0 deletions src/sinks/util/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@ impl UserLoggingResponse for HttpResponse {
None
}
}

fn log_captured_data(&self) -> Option<Value> {
Some(Value::Object(btreemap! {
"response" => self.http_response.body().to_owned()
}))
}
}

impl UserLoggingError for crate::Error {
Expand Down

0 comments on commit ee3fac9

Please sign in to comment.