Skip to content

Commit

Permalink
WIP fix request_id parse + debug
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Aug 15, 2024
1 parent c14935b commit 0fa4821
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions libs/blockscout-service-launcher/src/tracing/request_id_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,58 @@ impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Layer<S> for RequestIdSto
let span = ctx.span(id).expect("Span not found, this is a bug");

let request_id = if let Some(parent_span) = span.parent() {
dbg!(parent_span.extensions().get::<RequestId>());
let request_id = parent_span.extensions().get::<RequestId>().cloned();

let mut extensions = span.extensions_mut();
if extensions
.get_mut::<FormattedFields<JsonFields>>()
.is_none()
{
let mut fields = FormattedFields::<JsonFields>::new(String::new());
if JsonFields::new()
.format_fields(fields.as_writer(), attrs)
.is_ok()
if let Some(request_id) = request_id {
let mut extensions = span.extensions_mut();
if extensions
.get_mut::<FormattedFields<JsonFields>>()
.is_none()
{
extensions.insert(fields);
} else {
eprintln!(
"[tracing-subscriber] Unable to format the following event, ignoring: {:?}",
attrs
);
let mut fields = FormattedFields::<JsonFields>::new(String::new());
if JsonFields::new()
.format_fields(fields.as_writer(), attrs)
.is_ok()
{
extensions.insert(fields);
} else {
eprintln!(
"[tracing-subscriber] Unable to format the following event, ignoring: {:?}",
attrs
);
}
}
}
let data = extensions.get_mut::<FormattedFields<JsonFields>>().unwrap();
if let Some(request_id) = request_id {
dbg!("LOLdasdsa");
let data = extensions.get_mut::<FormattedFields<JsonFields>>().unwrap();
match serde_json::from_str::<serde_json::Value>(data) {
Ok(serde_json::Value::Object(mut fields))
if !fields.contains_key("request_id") =>
{
fields.insert("request_id".into(), request_id.0.to_string().into());
data.fields = serde_json::Value::Object(fields).to_string();
dbg!("LOL");
}
// If the value is not found or has invalid type, just ignore the error
// and propagate it further. Default Format<Json> layer will handle those cases.
_ => {}
_ => {
dbg!("KEK");
}
};
};
request_id
} else if let Some(field) = attrs.fields().field("request_id") {
dbg!(&field);
struct Visitor {
request_id_field: Field,
request_id_value: Option<RequestId>,
}
impl tracing::field::Visit for Visitor {
fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
dbg!(&field);
dbg!(&self.request_id_field);
if field == &self.request_id_field {
dbg!(&format!("{value:?}"));
dbg!(Uuid::from_str(&format!("{value:?}")));
if let Ok(request_id) = Uuid::from_str(&format!("{value:?}")) {
self.request_id_value = Some(RequestId(request_id));
}
Expand All @@ -78,6 +87,7 @@ impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Layer<S> for RequestIdSto
};
attrs.record(&mut visitor);

dbg!(&visitor.request_id_value);
visitor.request_id_value
} else {
None
Expand All @@ -93,6 +103,7 @@ impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Layer<S> for RequestIdSto
mod tests {
use std::{
io,
str::FromStr,
sync::{Arc, Mutex, MutexGuard, TryLockError},
};

Expand Down Expand Up @@ -218,8 +229,10 @@ mod tests {
let registry = tracing_subscriber::registry().with(layers);

with_default(registry, || {
let span0_with_request_id =
tracing::info_span!("span0_with_request_id", request_id = 322);
let span0_with_request_id = tracing::info_span!(
"span0_with_request_id",
request_id = uuid::Uuid::from_str("02f09a3f-1624-3b1d-8409-44eff7708208").unwrap()
);
let _e0 = span0_with_request_id.enter();
let span1 = tracing::info_span!("span1", x = 42);
let _e = span1.enter();
Expand All @@ -233,12 +246,12 @@ mod tests {
"message":"enter",
"target":"blockscout_service_launcher::tracing::request_id_layer::tests",
"span":{
"request_id":322,
"request_id":"02f09a3f-1624-3b1d-8409-44eff7708208",
"name":"span0_with_request_id"
},
"spans":[
{
"request_id":322,
"request_id":"02f09a3f-1624-3b1d-8409-44eff7708208",
"name":"span0_with_request_id"
}
]
Expand All @@ -250,17 +263,17 @@ mod tests {
"message":"enter",
"target":"blockscout_service_launcher::tracing::request_id_layer::tests",
"span":{
"request_id":322,
"request_id":"02f09a3f-1624-3b1d-8409-44eff7708208",
"x":42,
"name":"span1"
},
"spans":[
{
"request_id":322,
"request_id":"02f09a3f-1624-3b1d-8409-44eff7708208",
"name":"span0_with_request_id"
},
{
"request_id":322,
"request_id":"02f09a3f-1624-3b1d-8409-44eff7708208",
"x":42,
"name":"span1"
}
Expand Down

0 comments on commit 0fa4821

Please sign in to comment.