Skip to content

Commit

Permalink
Expose rust tracing instrumentation to javascript (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadapc authored Dec 3, 2024
1 parent 57a3c74 commit 3701139
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 393 deletions.
4 changes: 3 additions & 1 deletion crates/atlaspack_monitoring/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ impl Tracer {
None
}
TracerMode::Chrome => {
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new().build();
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
.include_args(true)
.build();
tracing_subscriber::registry()
.with(chrome_layer)
.try_init()
Expand Down
46 changes: 46 additions & 0 deletions crates/node-bindings/src/js_tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! This module exposes the tracing API to JavaScript.
use napi_derive::napi;
use std::collections::HashMap;

pub type SpanId = u32;

#[napi]
struct AtlaspackTracer {
current_spans: HashMap<SpanId, tracing::span::EnteredSpan>,
current_id: SpanId,
}

#[napi]
impl AtlaspackTracer {
#[napi(constructor)]
pub fn new() -> Self {
Self {
current_spans: HashMap::new(),
current_id: 0,
}
}

#[napi]
pub fn enter(&mut self, label: String) -> SpanId {
let span = tracing::span!(tracing::Level::INFO, "js_span", label = label);

let id = self.current_id;
if self.current_id == u32::MAX {
self.current_id = 0;
} else {
self.current_id += 1;
}

self.current_spans.insert(id, span.entered());

id
}

#[napi]
pub fn exit(&mut self, id: SpanId) {
if let Some(span) = self.current_spans.remove(&id) {
drop(span);
}
}
}
1 change: 1 addition & 0 deletions crates/node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod fs_search;
mod hash;
#[cfg(not(target_arch = "wasm32"))]
mod image;
pub mod js_tracing;

#[cfg(not(target_arch = "wasm32"))]
mod atlaspack;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/Atlaspack.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export default class Atlaspack {
});
} catch (e) {
// Fallthrough
logger.warn(e);
// eslint-disable-next-line no-console
console.warn(e);
}

let resolvedOptions: AtlaspackOptions = await resolveOptions({
Expand Down
Loading

0 comments on commit 3701139

Please sign in to comment.