diff --git a/src/instrumentation.rs b/src/instrumentation.rs index 0e7c580..05eb4f3 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -33,6 +33,7 @@ pub struct Instrumentation { count: usize, is_correct_class: bool, has_injected: bool, + module_version: Option, } impl Instrumentation { @@ -43,9 +44,14 @@ impl Instrumentation { count: 0, is_correct_class: false, has_injected: false, + module_version: None, } } + pub fn set_module_version(&mut self, version: &str) { + self.module_version = Some(version.to_string()); + } + #[must_use] pub fn has_injected(&self) -> bool { self.has_injected @@ -134,10 +140,18 @@ impl Instrumentation { "if (!$ch.hasSubscribers) return __apm$traced();" as Stmt, ch = ch_ident ), - quote!( - "return $trace(__apm$traced, { arguments, self: this } );" as Stmt, - trace = trace_ident - ), + match &self.module_version { + Some(version) => quote!( + "return $trace(__apm$traced, { arguments, self: this, module_version: $version } );" + as Stmt, + trace = trace_ident, + version: Expr = version.as_str().into(), + ), + None => quote!( + "return $trace(__apm$traced, { arguments, self: this } );" as Stmt, + trace = trace_ident, + ), + }, ]; self.has_injected = true; @@ -184,7 +198,15 @@ impl Instrumentation { } body.stmts = vec![ - quote!("const $ctx = { arguments };" as Stmt, ctx = ctx_ident,), + match &self.module_version { + Some(version) => { + quote!("const $ctx = { arguments, module_version: $version };" as Stmt, + ctx = ctx_ident, + version: Expr = version.as_str().into() + ) + } + None => quote!("const $ctx = { arguments };" as Stmt, ctx = ctx_ident,), + }, try_catch, ]; diff --git a/src/lib.rs b/src/lib.rs index b7d0718..0618089 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,7 @@ pub struct Instrumentor { } impl Instrumentor { + #[must_use] pub fn new(config: Config) -> Self { Self { instrumentations: config @@ -106,7 +107,12 @@ impl Instrumentor { let instrumentations = self .instrumentations .iter() - .filter(|instr| instr.matches(module_name, version, file_path)); + .filter(|instr| instr.matches(module_name, version, file_path)) + .cloned() + .map(|mut i| { + i.set_module_version(version); + i + }); InstrumentationVisitor::new(instrumentations, &self.dc_module) } @@ -119,12 +125,12 @@ pub struct InstrumentationVisitor { } impl InstrumentationVisitor { - fn new<'b, I>(instrumentations: I, dc_module: &str) -> Self + fn new(instrumentations: I, dc_module: &str) -> Self where - I: Iterator, + I: Iterator, { Self { - instrumentations: instrumentations.cloned().collect(), + instrumentations: instrumentations.collect(), dc_module: dc_module.to_string(), } } diff --git a/tests/wasm/__snapshots__/tests.test.mjs.snap b/tests/wasm/__snapshots__/tests.test.mjs.snap index 0c78701..4ac8fda 100644 --- a/tests/wasm/__snapshots__/tests.test.mjs.snap +++ b/tests/wasm/__snapshots__/tests.test.mjs.snap @@ -8,7 +8,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co module.exports = class Up { constructor(){ const tr_ch_apm_ctx$up_constructor = { - arguments + arguments, + module_version: "1.0.0" }; try { if (tr_ch_apm$up_constructor.hasSubscribers) { @@ -42,7 +43,8 @@ module.exports = class Up { if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced(); return tr_ch_apm$up_fetch.traceSync(__apm$traced, { arguments, - self: this + self: this, + module_version: "1.0.0" }); } }; @@ -59,7 +61,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co export class Up { constructor(){ const tr_ch_apm_ctx$up_constructor = { - arguments + arguments, + module_version: "1.0.0" }; try { if (tr_ch_apm$up_constructor.hasSubscribers) { @@ -93,7 +96,8 @@ export class Up { if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced(); return tr_ch_apm$up_fetch.traceSync(__apm$traced, { arguments, - self: this + self: this, + module_version: "1.0.0" }); } } @@ -110,7 +114,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co export class Up { constructor(){ const tr_ch_apm_ctx$up_constructor = { - arguments + arguments, + module_version: "1.0.0" }; try { if (tr_ch_apm$up_constructor.hasSubscribers) { @@ -144,11 +149,12 @@ export class Up { if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced(); return tr_ch_apm$up_fetch.traceSync(__apm$traced, { arguments, - self: this + self: this, + module_version: "1.0.0" }); } } ", - "map": "{"version":3,"file":"module.js","sources":["module.ts"],"sourceRoot":"","names":[],"mappings":";;;AAEA,MAAM,CAAA,MAAO,EAAE;IACX,aAAA;;;;;;;;YACI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;;;;;;;;;;;;;;;;IAC/B,CAAC;IACD,KAAK,IAAS,EAAA;;;mCAAR;gBACF,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;;;;;;;;;IACzB,CAAC;CACJ"}", + "map": "{"version":3,"file":"module.js","sources":["module.ts"],"sourceRoot":"","names":[],"mappings":";;;AAEA,MAAM,CAAA,MAAO,EAAE;IACX,aAAA;;;;;;;;;YACI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;;;;;;;;;;;;;;;;IAC/B,CAAC;IACD,KAAK,IAAS,EAAA;;;mCAAR;gBACF,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;;;;;;;;;;IACzB,CAAC;CACJ"}", } `; diff --git a/tests/wasm/tests.test.mjs b/tests/wasm/tests.test.mjs index 094df4a..2788436 100644 --- a/tests/wasm/tests.test.mjs +++ b/tests/wasm/tests.test.mjs @@ -82,8 +82,6 @@ export class Up { } }); - console.log({ outputJavaScript }); - const outputTs = matchedTransforms.transform( outputJavaScript, "esm", @@ -92,12 +90,11 @@ export class Up { expect(outputTs).toMatchSnapshot(); - const sourceMapConsumer = (await new SourceMapConsumer(JSON.parse(outputTs.map))); const originalPosition = sourceMapConsumer.originalPositionFor({ // This is the position of the fetch function in the transformed JavaScript - line: 30, + line: 31, column: 4, });