Skip to content

Commit 63f52c1

Browse files
committed
add more tests, and some fixes to go along with them
* indices, make them actually work * make multiple instrumentations in one file actually work
1 parent f58291b commit 63f52c1

14 files changed

Lines changed: 197 additions & 4 deletions

File tree

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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ impl Instrumentation {
7070
define_channel
7171
}
7272

73-
fn insert_tracing(&self, body: &mut BlockStmt) {
73+
fn insert_tracing(&mut self, body: &mut BlockStmt) {
74+
self.count += 1;
75+
7476
let original_stmts = std::mem::take(&mut body.stmts);
7577

7678
// Create a new BlockStmt with the original statements
@@ -102,7 +104,9 @@ impl Instrumentation {
102104
];
103105
}
104106

105-
fn insert_constructor_tracing(&self, body: &mut BlockStmt) {
107+
fn insert_constructor_tracing(&mut self, body: &mut BlockStmt) {
108+
self.count += 1;
109+
106110
let original_stmts = std::mem::take(&mut body.stmts);
107111

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

src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ impl<'a> InstrumentationVisitor<'a> {
103103
macro_rules! visit_with_all {
104104
($self:expr, $method:ident, $item:expr) => {
105105
let mut recurse = false;
106+
println!(
107+
"InstrumentationVisitor::visit_mut_{}: Instrumentation count is {}",
108+
stringify!($method),
109+
$self.instrumentations.len()
110+
);
106111
for instr in &mut $self.instrumentations {
107-
recurse = recurse || instr.$method($item);
112+
let needs_recurse = instr.$method($item);
113+
recurse = recurse || needs_recurse;
108114
}
109115
if recurse {
110116
$item.visit_mut_children_with($self);
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ make_test!(expr_cjs, false);
2727

2828
make_test!(class_method_cjs, false);
2929

30+
make_test!(multiple_class_method_cjs, false);
31+
3032
make_test!(object_method_cjs, false);
3133

3234
make_test!(constructor_cjs, false);
@@ -36,3 +38,8 @@ make_test!(constructor_mjs, true);
3638
make_test!(polyfill_mjs, true);
3739

3840
make_test!(polyfill_cjs, false);
41+
42+
make_test!(index_cjs, false);
43+
44+
make_test!(no_index_cjs, false);
45+
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)