Skip to content

feat: Include module version in event args #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Instrumentation {
count: usize,
is_correct_class: bool,
has_injected: bool,
module_version: Option<String>,
}

impl Instrumentation {
Expand All @@ -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
Expand Down Expand Up @@ -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 } );"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick but can this be camel case instead of snake? moduleVersion?

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;
Expand Down Expand Up @@ -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,
];

Expand Down
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct Instrumentor {
}

impl Instrumentor {
#[must_use]
pub fn new(config: Config) -> Self {
Self {
instrumentations: config
Expand All @@ -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)
}
Expand All @@ -119,12 +125,12 @@ pub struct InstrumentationVisitor {
}

impl InstrumentationVisitor {
fn new<'b, I>(instrumentations: I, dc_module: &str) -> Self
fn new<I>(instrumentations: I, dc_module: &str) -> Self
where
I: Iterator<Item = &'b Instrumentation>,
I: Iterator<Item = Instrumentation>,
{
Self {
instrumentations: instrumentations.cloned().collect(),
instrumentations: instrumentations.collect(),
dc_module: dc_module.to_string(),
}
}
Expand Down
20 changes: 13 additions & 7 deletions tests/wasm/__snapshots__/tests.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
});
}
};
Expand All @@ -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) {
Expand Down Expand Up @@ -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"
});
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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"}",
}
`;
5 changes: 1 addition & 4 deletions tests/wasm/tests.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ export class Up {
}
});

console.log({ outputJavaScript });

const outputTs = matchedTransforms.transform(
outputJavaScript,
"esm",
Expand All @@ -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,
});

Expand Down
Loading