Skip to content

Commit ac24249

Browse files
authored
add more tests, and some fixes to go along with them (#4)
* indices, make them actually work * make multiple instrumentations in one file actually work * fix instrumentor being used multiple times
1 parent f58291b commit ac24249

File tree

14 files changed

+205
-4
lines changed

14 files changed

+205
-4
lines changed

src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! get_arr {
2323
};
2424
}
2525

26-
#[derive(Clone)]
26+
#[derive(Clone, Debug)]
2727
pub enum InstrumentationOperator {
2828
Callback,
2929
Promise,
@@ -52,6 +52,7 @@ impl InstrumentationOperator {
5252
}
5353
}
5454

55+
#[derive(Debug)]
5556
pub struct InstrumentationConfig {
5657
pub module_name: String,
5758
pub version_range: Range,

src/function_query.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ macro_rules! get_str {
1010
};
1111
}
1212

13+
#[derive(Debug)]
1314
pub enum FunctionType {
1415
FunctionDeclaration,
1516
FunctionExpression,
@@ -27,6 +28,7 @@ impl FunctionType {
2728
}
2829
}
2930

31+
#[derive(Debug)]
3032
pub enum FunctionKind {
3133
Sync,
3234
Async,
@@ -63,6 +65,7 @@ impl FunctionKind {
6365
}
6466
}
6567

68+
#[derive(Debug)]
6669
pub struct FunctionQuery {
6770
pub name: String,
6871
pub class: Option<String>,

src/instrumentation.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ impl Instrumentation {
3838
}
3939
}
4040

41+
pub(crate) fn reset(&mut self) {
42+
self.count = 0;
43+
self.is_correct_class = false;
44+
}
45+
4146
fn new_fn(&self, body: BlockStmt) -> ArrowExpr {
4247
ArrowExpr {
4348
params: vec![],
@@ -70,7 +75,9 @@ impl Instrumentation {
7075
define_channel
7176
}
7277

73-
fn insert_tracing(&self, body: &mut BlockStmt) {
78+
fn insert_tracing(&mut self, body: &mut BlockStmt) {
79+
self.count += 1;
80+
7481
let original_stmts = std::mem::take(&mut body.stmts);
7582

7683
// Create a new BlockStmt with the original statements
@@ -102,7 +109,9 @@ impl Instrumentation {
102109
];
103110
}
104111

105-
fn insert_constructor_tracing(&self, body: &mut BlockStmt) {
112+
fn insert_constructor_tracing(&mut self, body: &mut BlockStmt) {
113+
self.count += 1;
114+
106115
let original_stmts = std::mem::take(&mut body.stmts);
107116

108117
let ch_ident = ident!(format!("tr_ch_apm${}", &self.config.channel_name));

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ macro_rules! visit_with_all {
104104
($self:expr, $method:ident, $item:expr) => {
105105
let mut recurse = false;
106106
for instr in &mut $self.instrumentations {
107-
recurse = recurse || instr.$method($item);
107+
let needs_recurse = instr.$method($item);
108+
recurse = recurse || needs_recurse;
108109
}
109110
if recurse {
110111
$item.visit_mut_children_with($self);
@@ -132,6 +133,9 @@ impl VisitMut for InstrumentationVisitor<'_> {
132133
}
133134
}
134135
visit_with_all!(self, visit_mut_module, item);
136+
for instr in &mut self.instrumentations {
137+
instr.reset();
138+
}
135139
}
136140

137141
fn visit_mut_script(&mut self, item: &mut Script) {
@@ -141,6 +145,9 @@ impl VisitMut for InstrumentationVisitor<'_> {
141145
);
142146
item.body.insert(get_script_start_index(item), import);
143147
visit_with_all!(self, visit_mut_script, item);
148+
for instr in &mut self.instrumentations {
149+
instr.reset();
150+
}
144151
}
145152

146153
visit_with_all_fn!(visit_mut_fn_decl, FnDecl);

tests/index_cjs/instrumentations.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1
2+
instrumentations:
3+
- module_name: undici
4+
version_range: ">=0.0.1"
5+
file_path: index.mjs
6+
function_query:
7+
class: Undici
8+
name: fetch
9+
type: method
10+
kind: async
11+
index: 2
12+
operator: tracePromise
13+
channel_name: Undici_fetch

tests/index_cjs/mod.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
class Undici {
3+
async fetch (url) {
4+
return 0;
5+
}
6+
}
7+
8+
exports.Undici0 = Undici;
9+
}
10+
{
11+
class Undici {
12+
async fetch (url) {
13+
return 1;
14+
}
15+
}
16+
17+
exports.Undici1 = Undici;
18+
}
19+
{
20+
class Undici {
21+
async fetch (url) {
22+
return 2;
23+
}
24+
}
25+
26+
exports.Undici2 = Undici;
27+
}
28+
{
29+
class Undici {
30+
async fetch (url) {
31+
return 3;
32+
}
33+
}
34+
35+
exports.Undici3 = Undici;
36+
}
37+
{
38+
class Undici {
39+
async fetch (url) {
40+
return 4;
41+
}
42+
}
43+
44+
exports.Undici4 = Undici;
45+
}

tests/index_cjs/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const undicis = require('./instrumented.js');
2+
const { assert, getContext } = require('../common/preamble.js');
3+
const context = getContext('orchestrion:undici:Undici_fetch');
4+
5+
async function testOne(Undici, num, expectedCtx) {
6+
const undici = new Undici;
7+
const result = await undici.fetch('https://example.com');
8+
assert.strictEqual(result, num);
9+
assert.deepStrictEqual(context, expectedCtx);
10+
delete context.start;
11+
delete context.end;
12+
delete context.asyncStart;
13+
delete context.asyncEnd;
14+
}
15+
16+
(async () => {
17+
await testOne(undicis.Undici0, 0, {});
18+
await testOne(undicis.Undici1, 1, {});
19+
await testOne(undicis.Undici2, 2, {
20+
start: true,
21+
end: true,
22+
asyncStart: 2,
23+
asyncEnd: 2
24+
});
25+
await testOne(undicis.Undici3, 3, {});
26+
await testOne(undicis.Undici4, 4, {});
27+
})();

tests/instrumentor_test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ macro_rules! make_test {
1313
instrumentor.get_matching_instrumentations("undici", "0.0.1", &file_path);
1414

1515
transpile_and_test(stringify!($name), $mjs, &mut instrumentations);
16+
17+
// It has to work twice, since we might use the same instrumentor on multiple files
18+
transpile_and_test(stringify!($name), $mjs, &mut instrumentations);
1619
}
1720
};
1821
}
@@ -27,6 +30,8 @@ make_test!(expr_cjs, false);
2730

2831
make_test!(class_method_cjs, false);
2932

33+
make_test!(multiple_class_method_cjs, false);
34+
3035
make_test!(object_method_cjs, false);
3136

3237
make_test!(constructor_cjs, false);
@@ -36,3 +41,7 @@ make_test!(constructor_mjs, true);
3641
make_test!(polyfill_mjs, true);
3742

3843
make_test!(polyfill_cjs, false);
44+
45+
make_test!(index_cjs, false);
46+
47+
make_test!(no_index_cjs, false);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: 1
2+
instrumentations:
3+
- module_name: undici
4+
version_range: ">=0.0.1"
5+
file_path: index.mjs
6+
function_query:
7+
class: Undici
8+
name: fetch1
9+
type: method
10+
kind: async
11+
operator: tracePromise
12+
channel_name: Undici_fetch1
13+
- module_name: undici
14+
version_range: ">=0.0.1"
15+
file_path: index.mjs
16+
function_query:
17+
class: Undici
18+
name: fetch2
19+
type: method
20+
kind: async
21+
operator: tracePromise
22+
channel_name: Undici_fetch2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Undici {
2+
async fetch1 (url) {
3+
return 42;
4+
}
5+
6+
async fetch2 (url) {
7+
return 43;
8+
}
9+
}
10+
11+
module.exports = Undici;

0 commit comments

Comments
 (0)