From a65ceed0aa33eb2aeb571902d36ba1d44f73ec0f Mon Sep 17 00:00:00 2001 From: Jenan Wise Date: Sun, 9 Aug 2020 10:13:30 -0700 Subject: [PATCH] Guard `performance.*` with `report_logs_in_timings`. The global `performance` object is not available in some javascript enviroments. E.g.: - nodejs exports it as `perf_hooks.performance`, not as a global `peformance` object. - cloudflare workers have a limited subset of global objects which does not include `performance` at all. `WASMLayerConfig` allows opt-out of the performance timing APIs with the `report_logs_in_timings` option, but it was not previously being checked before some usage of `performance.mark` and `performance.measure`. This patch guards those calls against the config option. --- src/lib.rs | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cdbc51f..b0a48a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,31 +149,35 @@ impl LookupSpan<'a>> Layer for WASMLayer { } /// doc: Notifies this layer that a span with the given ID was entered. fn on_enter(&self, id: &tracing::Id, _ctx: Context<'_, S>) { - mark(&mark_name(id)); + if self.config.report_logs_in_timings { + mark(&mark_name(id)); + } } /// doc: Notifies this layer that the span with the given ID was exited. fn on_exit(&self, id: &tracing::Id, ctx: Context<'_, S>) { - if let Some(span_ref) = ctx.span(id) { - let meta = span_ref.metadata(); - if let Some(debug_record) = span_ref.extensions().get::() { - measure( - &format!( - "\"{}\" {} {}", - meta.name(), - meta.module_path().unwrap_or("..."), - debug_record, - ), - &mark_name(id), - ) - } else { - measure( - &format!( - "\"{}\" {}", - meta.name(), - meta.module_path().unwrap_or("..."), - ), - &mark_name(id), - ) + if self.config.report_logs_in_timings { + if let Some(span_ref) = ctx.span(id) { + let meta = span_ref.metadata(); + if let Some(debug_record) = span_ref.extensions().get::() { + measure( + &format!( + "\"{}\" {} {}", + meta.name(), + meta.module_path().unwrap_or("..."), + debug_record, + ), + &mark_name(id), + ) + } else { + measure( + &format!( + "\"{}\" {}", + meta.name(), + meta.module_path().unwrap_or("..."), + ), + &mark_name(id), + ) + } } } }