Skip to content
Open
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
12 changes: 8 additions & 4 deletions src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,19 @@ impl Instrumentation {
// `visit_mut_children_with`.

pub fn visit_mut_module(&mut self, node: &mut Module) -> bool {
node.body
.insert(1, ModuleItem::Stmt(self.create_tracing_channel()));
let channel_element = ModuleItem::Stmt(self.create_tracing_channel());
if !node.body.iter().any(|item| item == &channel_element) {
node.body.insert(1, channel_element);
}
true
}

pub fn visit_mut_script(&mut self, node: &mut Script) -> bool {
let start_index = get_script_start_index(node);
node.body
.insert(start_index + 1, self.create_tracing_channel());
let channel_element = self.create_tracing_channel();
if !node.body.iter().any(|item| item == &channel_element) {
node.body.insert(start_index + 1, channel_element);
}
true
}

Expand Down
2 changes: 2 additions & 0 deletions tests/instrumentor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ mod multiple_load_cjs;
mod object_method_cjs;
mod polyfill_cjs;
mod polyfill_mjs;
mod single_channel_multiple_usages_cjs;
mod single_channel_multiple_usages_mjs;
9 changes: 9 additions & 0 deletions tests/single_channel_multiple_usages_cjs/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async function foo () {
return 'foo'
}

async function bar () {
return 'bar'
}

module.exports = { foo, bar };
25 changes: 25 additions & 0 deletions tests/single_channel_multiple_usages_cjs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::common::*;
use orchestrion_js::*;

#[test]
fn same_channel_multiple_usages_cjs() {
transpile_and_test(
file!(),
false,
Config::new(
vec![
InstrumentationConfig::new(
"method_call",
test_module_matcher(),
FunctionQuery::function_declaration("foo", FunctionKind::Async),
),
InstrumentationConfig::new(
"method_call",
test_module_matcher(),
FunctionQuery::function_declaration("bar", FunctionKind::Async),
),
],
None,
),
);
}
26 changes: 26 additions & 0 deletions tests/single_channel_multiple_usages_cjs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
**/
const { foo, bar } = require('./instrumented.js');
const { assert, getContext } = require('../common/preamble.js');
const context = getContext('orchestrion:undici:method_call');
(async () => {
const result = await foo();
assert.strictEqual(result, 'foo');
assert.deepStrictEqual(context, {
start: true,
end: true,
asyncStart: 'foo',
asyncEnd: 'foo'
});

const result2 = await bar();
assert.strictEqual(result2, 'bar');
assert.deepStrictEqual(context, {
start: true,
end: true,
asyncStart: 'bar',
asyncEnd: 'bar'
});
})();
9 changes: 9 additions & 0 deletions tests/single_channel_multiple_usages_mjs/mod.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async function foo () {
return 'foo'
}

async function bar () {
return 'bar'
}

export { foo, bar };
25 changes: 25 additions & 0 deletions tests/single_channel_multiple_usages_mjs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::common::*;
use orchestrion_js::*;

#[test]
fn same_channel_multiple_usages_mjs() {
transpile_and_test(
file!(),
true,
Config::new(
vec![
InstrumentationConfig::new(
"method_call",
test_module_matcher(),
FunctionQuery::function_declaration("foo", FunctionKind::Async),
),
InstrumentationConfig::new(
"method_call",
test_module_matcher(),
FunctionQuery::function_declaration("bar", FunctionKind::Async),
),
],
None,
),
);
}
24 changes: 24 additions & 0 deletions tests/single_channel_multiple_usages_mjs/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
**/
import { foo, bar } from './instrumented.mjs';
import { assert, getContext } from '../common/preamble.js';
const context = getContext('orchestrion:undici:method_call');
const result = await foo();
assert.strictEqual(result, 'foo');
assert.deepStrictEqual(context, {
start: true,
end: true,
asyncStart: 'foo',
asyncEnd: 'foo'
});

const result2 = await bar();
assert.strictEqual(result2, 'bar');
assert.deepStrictEqual(context, {
start: true,
end: true,
asyncStart: 'bar',
asyncEnd: 'bar'
});