Skip to content

Commit cc7f553

Browse files
committed
feat: Include module version in event args
1 parent 288764e commit cc7f553

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

src/instrumentation.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Instrumentation {
3333
count: usize,
3434
is_correct_class: bool,
3535
has_injected: bool,
36+
module_version: Option<String>,
3637
}
3738

3839
impl Instrumentation {
@@ -43,9 +44,14 @@ impl Instrumentation {
4344
count: 0,
4445
is_correct_class: false,
4546
has_injected: false,
47+
module_version: None,
4648
}
4749
}
4850

51+
pub fn set_module_version(&mut self, version: &str) {
52+
self.module_version = Some(version.to_string());
53+
}
54+
4955
#[must_use]
5056
pub fn has_injected(&self) -> bool {
5157
self.has_injected
@@ -134,10 +140,18 @@ impl Instrumentation {
134140
"if (!$ch.hasSubscribers) return __apm$traced();" as Stmt,
135141
ch = ch_ident
136142
),
137-
quote!(
138-
"return $trace(__apm$traced, { arguments, self: this } );" as Stmt,
139-
trace = trace_ident
140-
),
143+
match &self.module_version {
144+
Some(version) => quote!(
145+
"return $trace(__apm$traced, { arguments, self: this, module_version: $version } );"
146+
as Stmt,
147+
trace = trace_ident,
148+
version: Expr = version.as_str().into(),
149+
),
150+
None => quote!(
151+
"return $trace(__apm$traced, { arguments, self: this } );" as Stmt,
152+
trace = trace_ident,
153+
),
154+
},
141155
];
142156

143157
self.has_injected = true;
@@ -184,7 +198,15 @@ impl Instrumentation {
184198
}
185199

186200
body.stmts = vec![
187-
quote!("const $ctx = { arguments };" as Stmt, ctx = ctx_ident,),
201+
match &self.module_version {
202+
Some(version) => {
203+
quote!("const $ctx = { arguments, module_version: $version };" as Stmt,
204+
ctx = ctx_ident,
205+
version: Expr = version.as_str().into()
206+
)
207+
}
208+
None => quote!("const $ctx = { arguments };" as Stmt, ctx = ctx_ident,),
209+
},
188210
try_catch,
189211
];
190212

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct Instrumentor {
8282
}
8383

8484
impl Instrumentor {
85+
#[must_use]
8586
pub fn new(config: Config) -> Self {
8687
Self {
8788
instrumentations: config
@@ -105,7 +106,12 @@ impl Instrumentor {
105106
let instrumentations = self
106107
.instrumentations
107108
.iter()
108-
.filter(|instr| instr.matches(module_name, version, file_path));
109+
.filter(|instr| instr.matches(module_name, version, file_path))
110+
.cloned()
111+
.map(|mut i| {
112+
i.set_module_version(version);
113+
i
114+
});
109115

110116
InstrumentationVisitor::new(instrumentations, &self.dc_module)
111117
}
@@ -118,12 +124,12 @@ pub struct InstrumentationVisitor {
118124
}
119125

120126
impl InstrumentationVisitor {
121-
fn new<'b, I>(instrumentations: I, dc_module: &str) -> Self
127+
fn new<I>(instrumentations: I, dc_module: &str) -> Self
122128
where
123-
I: Iterator<Item = &'b Instrumentation>,
129+
I: Iterator<Item = Instrumentation>,
124130
{
125131
Self {
126-
instrumentations: instrumentations.cloned().collect(),
132+
instrumentations: instrumentations.collect(),
127133
dc_module: dc_module.to_string(),
128134
}
129135
}

tests/wasm/__snapshots__/tests.test.mjs.snap

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co
88
module.exports = class Up {
99
constructor(){
1010
const tr_ch_apm_ctx$up_constructor = {
11-
arguments
11+
arguments,
12+
module_version: "1.0.0"
1213
};
1314
try {
1415
if (tr_ch_apm$up_constructor.hasSubscribers) {
@@ -42,7 +43,8 @@ module.exports = class Up {
4243
if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced();
4344
return tr_ch_apm$up_fetch.traceSync(__apm$traced, {
4445
arguments,
45-
self: this
46+
self: this,
47+
module_version: "1.0.0"
4648
});
4749
}
4850
};
@@ -59,7 +61,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co
5961
export class Up {
6062
constructor(){
6163
const tr_ch_apm_ctx$up_constructor = {
62-
arguments
64+
arguments,
65+
module_version: "1.0.0"
6366
};
6467
try {
6568
if (tr_ch_apm$up_constructor.hasSubscribers) {
@@ -93,7 +96,8 @@ export class Up {
9396
if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced();
9497
return tr_ch_apm$up_fetch.traceSync(__apm$traced, {
9598
arguments,
96-
self: this
99+
self: this,
100+
module_version: "1.0.0"
97101
});
98102
}
99103
}
@@ -110,7 +114,8 @@ const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:co
110114
export class Up {
111115
constructor(){
112116
const tr_ch_apm_ctx$up_constructor = {
113-
arguments
117+
arguments,
118+
module_version: "1.0.0"
114119
};
115120
try {
116121
if (tr_ch_apm$up_constructor.hasSubscribers) {
@@ -144,11 +149,12 @@ export class Up {
144149
if (!tr_ch_apm$up_fetch.hasSubscribers) return __apm$traced();
145150
return tr_ch_apm$up_fetch.traceSync(__apm$traced, {
146151
arguments,
147-
self: this
152+
self: this,
153+
module_version: "1.0.0"
148154
});
149155
}
150156
}
151157
",
152-
"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"}",
158+
"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"}",
153159
}
154160
`;

tests/wasm/tests.test.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ export class Up {
8282
}
8383
});
8484

85-
console.log({ outputJavaScript });
86-
8785
const outputTs = matchedTransforms.transform(
8886
outputJavaScript,
8987
"esm",
@@ -92,12 +90,11 @@ export class Up {
9290

9391
expect(outputTs).toMatchSnapshot();
9492

95-
9693
const sourceMapConsumer = (await new SourceMapConsumer(JSON.parse(outputTs.map)));
9794

9895
const originalPosition = sourceMapConsumer.originalPositionFor({
9996
// This is the position of the fetch function in the transformed JavaScript
100-
line: 30,
97+
line: 31,
10198
column: 4,
10299
});
103100

0 commit comments

Comments
 (0)