Skip to content

Commit 0570bd0

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

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
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: 12 additions & 7 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
@@ -102,10 +103,16 @@ impl Instrumentor {
102103
version: &str,
103104
file_path: &PathBuf,
104105
) -> InstrumentationVisitor {
105-
let instrumentations = self
106+
let mut 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+
.collect::<Vec<_>>();
112+
113+
for instr in &mut instrumentations {
114+
instr.set_module_version(version);
115+
}
109116

110117
InstrumentationVisitor::new(instrumentations, &self.dc_module)
111118
}
@@ -118,12 +125,10 @@ pub struct InstrumentationVisitor {
118125
}
119126

120127
impl InstrumentationVisitor {
121-
fn new<'b, I>(instrumentations: I, dc_module: &str) -> Self
122-
where
123-
I: Iterator<Item = &'b Instrumentation>,
124-
{
128+
fn new(instrumentations: Vec<Instrumentation>, dc_module: &str) -> Self
129+
where {
125130
Self {
126-
instrumentations: instrumentations.cloned().collect(),
131+
instrumentations,
127132
dc_module: dc_module.to_string(),
128133
}
129134
}

0 commit comments

Comments
 (0)