-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(me): implement timing events for spans
This is a minimal tracing layer that times how long each span takes and logs an event with the result after the span is closed, for our own needs in performance observability. closes prisma/prisma#13693
- Loading branch information
Showing
10 changed files
with
107 additions
and
109 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::time; | ||
use tracing::Id as SpanId; | ||
|
||
/// Gather and display timings of tracing spans. | ||
#[derive(Default)] | ||
pub struct TimingsLayer; | ||
|
||
struct TimerTime(pub time::Instant, String); | ||
|
||
impl<S> tracing_subscriber::Layer<S> for TimingsLayer | ||
where | ||
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>, | ||
{ | ||
fn on_new_span( | ||
&self, | ||
attrs: &tracing::span::Attributes<'_>, | ||
id: &SpanId, | ||
ctx: tracing_subscriber::layer::Context<'_, S>, | ||
) { | ||
let span_ctx = ctx.span(id).unwrap(); | ||
let mut extensions = span_ctx.extensions_mut(); | ||
extensions.insert(TimerTime(time::Instant::now(), attrs.values().to_string())); | ||
} | ||
|
||
fn on_close(&self, id: SpanId, ctx: tracing_subscriber::layer::Context<'_, S>) { | ||
let span_ctx = ctx.span(&id).unwrap(); | ||
let span_name = span_ctx.name(); | ||
let mut extensions = span_ctx.extensions_mut(); | ||
let TimerTime(start, values) = extensions.remove::<TimerTime>().unwrap(); | ||
let elapsed = time::Instant::now().duration_since(start); | ||
tracing::debug!( | ||
span_timing_μs = elapsed.as_micros() as u32, | ||
"{span_name}{values}: Span closed. Elapsed: {elapsed:?}", | ||
); | ||
} | ||
} |
Oops, something went wrong.