Skip to content

Commit 515fe00

Browse files
mzabaluevdjc
authored andcommitted
Reduce locking in Layer::on_close
Only hold the lock on span's extension to retrieve the extension data, but release it before processing and exporting.
1 parent 74d6ba0 commit 515fe00

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/layer.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -1104,25 +1104,31 @@ where
11041104
/// [`Span`]: opentelemetry::trace::Span
11051105
fn on_close(&self, id: span::Id, ctx: Context<'_, S>) {
11061106
let span = ctx.span(&id).expect("Span not found, this is a bug");
1107-
let mut extensions = span.extensions_mut();
1107+
let (otel_data, timings) = {
1108+
let mut extensions = span.extensions_mut();
1109+
let timings = if self.tracked_inactivity {
1110+
extensions.remove::<Timings>()
1111+
} else {
1112+
None
1113+
};
1114+
(extensions.remove::<OtelData>(), timings)
1115+
};
11081116

11091117
if let Some(OtelData {
11101118
mut builder,
11111119
parent_cx,
1112-
}) = extensions.remove::<OtelData>()
1120+
}) = otel_data
11131121
{
1114-
if self.tracked_inactivity {
1115-
// Append busy/idle timings when enabled.
1116-
if let Some(timings) = extensions.get_mut::<Timings>() {
1117-
let busy_ns = Key::new("busy_ns");
1118-
let idle_ns = Key::new("idle_ns");
1122+
// Append busy/idle timings when enabled.
1123+
if let Some(timings) = timings {
1124+
let busy_ns = Key::new("busy_ns");
1125+
let idle_ns = Key::new("idle_ns");
11191126

1120-
let attributes = builder
1121-
.attributes
1122-
.get_or_insert_with(|| Vec::with_capacity(2));
1123-
attributes.push(KeyValue::new(busy_ns, timings.busy));
1124-
attributes.push(KeyValue::new(idle_ns, timings.idle));
1125-
}
1127+
let attributes = builder
1128+
.attributes
1129+
.get_or_insert_with(|| Vec::with_capacity(2));
1130+
attributes.push(KeyValue::new(busy_ns, timings.busy));
1131+
attributes.push(KeyValue::new(idle_ns, timings.idle));
11261132
}
11271133

11281134
// Build and start span, drop span to export

0 commit comments

Comments
 (0)