Skip to content
Merged
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
25 changes: 7 additions & 18 deletions src/function_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 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.
**/
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
use swc_core::ecma::ast::FnDecl;

#[derive(Debug, Clone)]
pub(crate) enum FunctionType {
Expand All @@ -25,14 +25,6 @@ impl FunctionKind {
matches!(self, FunctionKind::Async)
}

#[must_use]
pub fn matches(&self, func: &Function) -> bool {
match self {
FunctionKind::Sync => !func.is_async && !func.is_generator,
FunctionKind::Async => func.is_async && !func.is_generator,
}
}

#[must_use]
pub fn tracing_operator(&self) -> &'static str {
match self {
Expand Down Expand Up @@ -202,22 +194,19 @@ impl FunctionQuery {

pub fn matches_decl(&self, func: &FnDecl, count: &mut usize) -> bool {
let matches_except_count = matches!(self.typ(), FunctionType::FunctionDeclaration)
&& self.kind().matches(&func.function)
&& func.ident.sym == self.name();
self.maybe_increment_count(matches_except_count, count)
}

pub fn matches_expr(&self, func: &FnExpr, count: &mut usize, name: &str) -> bool {
let matches_except_count = matches!(self.typ(), FunctionType::FunctionExpression)
&& self.kind().matches(&func.function)
&& name == self.name();
pub fn matches_expr(&self, count: &mut usize, name: &str) -> bool {
let matches_except_count =
matches!(self.typ(), FunctionType::FunctionExpression) && name == self.name();
self.maybe_increment_count(matches_except_count, count)
}

pub fn matches_method(&self, func: &Function, count: &mut usize, name: &str) -> bool {
let matches_except_count = matches!(self.typ(), FunctionType::Method)
&& self.kind().matches(func)
&& name == self.name();
pub fn matches_method(&self, count: &mut usize, name: &str) -> bool {
let matches_except_count =
matches!(self.typ(), FunctionType::Method) && name == self.name();
self.maybe_increment_count(matches_except_count, count)
}
}
6 changes: 3 additions & 3 deletions src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Instrumentation {
if self
.config
.function_query
.matches_expr(func_expr, &mut self.count, name.as_ref())
.matches_expr(&mut self.count, name.as_ref())
&& func_expr.function.body.is_some()
{
if let Some(body) = func_expr.function.body.as_mut() {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl Instrumentation {
if self
.config
.function_query
.matches_method(&node.function, &mut self.count, name.as_ref())
.matches_method(&mut self.count, name.as_ref())
&& node.function.body.is_some()
{
if let Some(body) = node.function.body.as_mut() {
Expand Down Expand Up @@ -284,7 +284,7 @@ impl Instrumentation {
if self
.config
.function_query
.matches_method(&node.function, &mut self.count, name.as_ref())
.matches_method(&mut self.count, name.as_ref())
&& node.function.body.is_some()
{
if let Some(body) = node.function.body.as_mut() {
Expand Down
10 changes: 10 additions & 0 deletions tests/decl_mjs_mismatched_type/mod.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/**
* 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.
**/
function fetch (url) {
return Promise.resolve(42);
}

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

#[test]
fn decl_mjs_mismatched_type() {
transpile_and_test(
file!(),
true,
Config::new_single(InstrumentationConfig::new(
"fetch_decl",
test_module_matcher(),
FunctionQuery::function_declaration("fetch", FunctionKind::Async),
)),
);
}
15 changes: 15 additions & 0 deletions tests/decl_mjs_mismatched_type/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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 { fetch } from './instrumented.mjs';
import { assert, getContext } from '../common/preamble.js';
const context = getContext('orchestrion:undici:fetch_decl');
const result = await fetch('https://example.com');
assert.strictEqual(result, 42);
assert.deepStrictEqual(context, {
start: true,
end: true,
asyncStart: 42,
asyncEnd: 42
});
1 change: 1 addition & 0 deletions tests/instrumentor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod constructor_cjs;
mod constructor_mjs;
mod decl_cjs;
mod decl_mjs;
mod decl_mjs_mismatched_type;
mod expr_cjs;
mod expr_mjs;
mod index_cjs;
Expand Down