From d6eb1a3172660644c41c3d7cdafe38138eca4917 Mon Sep 17 00:00:00 2001 From: Sam Brenner Date: Thu, 12 Jun 2025 22:33:55 -0400 Subject: [PATCH 1/4] do not insert the same channel multiple times --- src/instrumentation.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/instrumentation.rs b/src/instrumentation.rs index f2d0247..6161a30 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -186,15 +186,22 @@ 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 = self.create_tracing_channel(); + let channel_module_item = ModuleItem::Stmt(channel_element.clone()); + if !node.body.iter().any(|item| item == &channel_module_item) { + node.body + .insert(1, ModuleItem::Stmt(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 } From e5c018339e85ff843d30b05836324683e2958e34 Mon Sep 17 00:00:00 2001 From: Sam Brenner Date: Thu, 12 Jun 2025 22:34:21 -0400 Subject: [PATCH 2/4] attempt reasonable tests --- tests/instrumentor_test.rs | 2 ++ .../single_channel_multiple_usages_cjs/mod.js | 9 +++++++ .../single_channel_multiple_usages_cjs/mod.rs | 25 ++++++++++++++++++ .../test.js | 26 +++++++++++++++++++ .../mod.mjs | 9 +++++++ .../single_channel_multiple_usages_mjs/mod.rs | 25 ++++++++++++++++++ .../test.mjs | 24 +++++++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 tests/single_channel_multiple_usages_cjs/mod.js create mode 100644 tests/single_channel_multiple_usages_cjs/mod.rs create mode 100644 tests/single_channel_multiple_usages_cjs/test.js create mode 100644 tests/single_channel_multiple_usages_mjs/mod.mjs create mode 100644 tests/single_channel_multiple_usages_mjs/mod.rs create mode 100644 tests/single_channel_multiple_usages_mjs/test.mjs diff --git a/tests/instrumentor_test.rs b/tests/instrumentor_test.rs index ec5b9fd..840f092 100644 --- a/tests/instrumentor_test.rs +++ b/tests/instrumentor_test.rs @@ -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; diff --git a/tests/single_channel_multiple_usages_cjs/mod.js b/tests/single_channel_multiple_usages_cjs/mod.js new file mode 100644 index 0000000..0d4b23b --- /dev/null +++ b/tests/single_channel_multiple_usages_cjs/mod.js @@ -0,0 +1,9 @@ +async function foo () { + return 'foo' +} + +async function bar () { + return 'bar' +} + +module.exports = { foo, bar }; \ No newline at end of file diff --git a/tests/single_channel_multiple_usages_cjs/mod.rs b/tests/single_channel_multiple_usages_cjs/mod.rs new file mode 100644 index 0000000..0f46779 --- /dev/null +++ b/tests/single_channel_multiple_usages_cjs/mod.rs @@ -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, + ), + ); +} \ No newline at end of file diff --git a/tests/single_channel_multiple_usages_cjs/test.js b/tests/single_channel_multiple_usages_cjs/test.js new file mode 100644 index 0000000..0012a47 --- /dev/null +++ b/tests/single_channel_multiple_usages_cjs/test.js @@ -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' + }); +})(); \ No newline at end of file diff --git a/tests/single_channel_multiple_usages_mjs/mod.mjs b/tests/single_channel_multiple_usages_mjs/mod.mjs new file mode 100644 index 0000000..ced4f48 --- /dev/null +++ b/tests/single_channel_multiple_usages_mjs/mod.mjs @@ -0,0 +1,9 @@ +async function foo () { + return 'foo' +} + +async function bar () { + return 'bar' +} + +export { foo, bar }; \ No newline at end of file diff --git a/tests/single_channel_multiple_usages_mjs/mod.rs b/tests/single_channel_multiple_usages_mjs/mod.rs new file mode 100644 index 0000000..08e0ed9 --- /dev/null +++ b/tests/single_channel_multiple_usages_mjs/mod.rs @@ -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, + ), + ); +} \ No newline at end of file diff --git a/tests/single_channel_multiple_usages_mjs/test.mjs b/tests/single_channel_multiple_usages_mjs/test.mjs new file mode 100644 index 0000000..9c98fec --- /dev/null +++ b/tests/single_channel_multiple_usages_mjs/test.mjs @@ -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' +}); \ No newline at end of file From 98922875e8ef1434607de93f821c6e58f40c07fc Mon Sep 17 00:00:00 2001 From: Sam Brenner Date: Thu, 12 Jun 2025 22:39:52 -0400 Subject: [PATCH 3/4] fmt --- src/instrumentation.rs | 6 +-- .../single_channel_multiple_usages_cjs/mod.rs | 40 +++++++++---------- .../single_channel_multiple_usages_mjs/mod.rs | 40 +++++++++---------- 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/instrumentation.rs b/src/instrumentation.rs index 6161a30..d08f7ef 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -189,8 +189,7 @@ impl Instrumentation { let channel_element = self.create_tracing_channel(); let channel_module_item = ModuleItem::Stmt(channel_element.clone()); if !node.body.iter().any(|item| item == &channel_module_item) { - node.body - .insert(1, ModuleItem::Stmt(channel_element)); + node.body.insert(1, ModuleItem::Stmt(channel_element)); } true } @@ -199,8 +198,7 @@ impl Instrumentation { let start_index = get_script_start_index(node); let channel_element = self.create_tracing_channel(); if !node.body.iter().any(|item| item == &channel_element) { - node.body - .insert(start_index + 1, channel_element); + node.body.insert(start_index + 1, channel_element); } true } diff --git a/tests/single_channel_multiple_usages_cjs/mod.rs b/tests/single_channel_multiple_usages_cjs/mod.rs index 0f46779..62dc5f2 100644 --- a/tests/single_channel_multiple_usages_cjs/mod.rs +++ b/tests/single_channel_multiple_usages_cjs/mod.rs @@ -3,23 +3,23 @@ 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, - ), - ); -} \ No newline at end of file + 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, + ), + ); +} diff --git a/tests/single_channel_multiple_usages_mjs/mod.rs b/tests/single_channel_multiple_usages_mjs/mod.rs index 08e0ed9..55d6373 100644 --- a/tests/single_channel_multiple_usages_mjs/mod.rs +++ b/tests/single_channel_multiple_usages_mjs/mod.rs @@ -3,23 +3,23 @@ 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, - ), - ); -} \ No newline at end of file + 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, + ), + ); +} From 7a8c3a3c303f625429ecf4cd82c8e18648df224d Mon Sep 17 00:00:00 2001 From: Sam Brenner Date: Thu, 12 Jun 2025 22:44:20 -0400 Subject: [PATCH 4/4] clean up --- src/instrumentation.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/instrumentation.rs b/src/instrumentation.rs index d08f7ef..cdfee49 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -186,10 +186,9 @@ impl Instrumentation { // `visit_mut_children_with`. pub fn visit_mut_module(&mut self, node: &mut Module) -> bool { - let channel_element = self.create_tracing_channel(); - let channel_module_item = ModuleItem::Stmt(channel_element.clone()); - if !node.body.iter().any(|item| item == &channel_module_item) { - node.body.insert(1, ModuleItem::Stmt(channel_element)); + 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 }