-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose rust tracing instrumentation to javascript (#246)
- Loading branch information
Showing
8 changed files
with
487 additions
and
393 deletions.
There are no files selected for viewing
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,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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.