Skip to content

Unnecessary Mutex in logging layer #7234

Open
@macladson

Description

@macladson

In order to record span data (which allows us to write out the service name in the log, and also allows us to determine if certain errors are crits), we store them in an Arc<Mutex<HashMap<_, _>>. This has the downside of causing us to need to manage the lock, check if it's poisoned, etc. We are also using an unwrap here:

if let Some(scope) = ctx.event_scope(event) {
for span in scope {
let id = span.id();
let span_fields_map = span_fields.lock().unwrap();
if let Some(span_data) = span_fields_map.get(&id) {
collected_span_fields.push((span_data.name.clone(), span_data.fields.clone()));
}
}
}

I believe we can use tracing's built in Extension system to manage this data for us, something like this:

let mut extensions = span.extensions_mut(); 

let span_data = SpanData {
    name: attrs.metadata().name().to_string(),
    fields: visitor.fields,
 };

extensions.insert(span_data);

This would allow us to remove the Mutex and the unwrap.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions