Skip to content

Commit 4537686

Browse files
committed
feat: Remove yaml configuration
1 parent d7d7a8b commit 4537686

38 files changed

Lines changed: 773 additions & 750 deletions

Cargo.lock

Lines changed: 333 additions & 280 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ license = "Apache-2.0"
99
# crate-type = ["cdylib"]
1010
# This was originally set as above, but commented to run tests in tests folder.
1111

12-
1312
[profile.release]
1413
lto = true
1514

1615
[dependencies]
17-
nodejs-semver = "4.1.0"
18-
swc = "16.1.0"
19-
swc_core = { version = "16.2.3", features = ["ecma_plugin_transform","ecma_quote"] }
20-
swc_ecma_parser = "10.0.0"
21-
swc_ecma_visit = { version = "8.0.0", features = ["path"] }
22-
yaml-rust2 = "0.10.0"
16+
nodejs-semver = "4"
17+
swc = "21"
18+
swc_core = { version = "22", features = ["ecma_plugin_transform","ecma_quote"] }
19+
swc_ecma_parser = "11"
20+
swc_ecma_visit = { version = "8", features = ["path"] }
2321

2422
[dev-dependencies]
25-
assert_cmd = "2.0.16"
23+
assert_cmd = "2"

src/config.rs

Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,9 @@
22
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
33
* This product includes software developed at Datadog (<https://www.datadoghq.com>/). Copyright 2025 Datadog, Inc.
44
**/
5-
use std::path::PathBuf;
6-
7-
use nodejs_semver::{Range, Version};
8-
9-
use crate::error::OrchestrionError;
105
use crate::function_query::FunctionQuery;
11-
12-
use yaml_rust2::{Yaml, YamlLoader};
13-
14-
macro_rules! get_str {
15-
($property:expr, $name:expr) => {
16-
$property[$name]
17-
.as_str()
18-
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
19-
};
20-
}
21-
22-
macro_rules! get_arr {
23-
($property:expr, $name:expr) => {
24-
$property[$name]
25-
.as_vec()
26-
.ok_or(format!("Invalid config: '{}' must be a array", $name))?
27-
};
28-
}
6+
use nodejs_semver::{Range, Version};
7+
use std::path::PathBuf;
298

309
#[derive(Clone, Debug)]
3110
pub enum InstrumentationOperator {
@@ -36,6 +15,7 @@ pub enum InstrumentationOperator {
3615
}
3716

3817
impl InstrumentationOperator {
18+
#[must_use]
3919
pub fn as_str(&self) -> &'static str {
4020
match self {
4121
InstrumentationOperator::Callback => "traceCallback",
@@ -44,19 +24,9 @@ impl InstrumentationOperator {
4424
InstrumentationOperator::Async => "traceAsync",
4525
}
4626
}
47-
48-
pub fn from_str(s: &str) -> Option<InstrumentationOperator> {
49-
match s {
50-
"traceCallback" => Some(InstrumentationOperator::Callback),
51-
"tracePromise" => Some(InstrumentationOperator::Promise),
52-
"traceSync" => Some(InstrumentationOperator::Sync),
53-
"traceAsync" => Some(InstrumentationOperator::Async),
54-
_ => None,
55-
}
56-
}
5727
}
5828

59-
#[derive(Debug)]
29+
#[derive(Debug, Clone)]
6030
pub struct InstrumentationConfig {
6131
pub module_name: String,
6232
pub version_range: Range,
@@ -66,85 +36,43 @@ pub struct InstrumentationConfig {
6636
pub channel_name: String,
6737
}
6838

39+
#[derive(Debug, Clone)]
6940
pub struct Config {
7041
pub instrumentations: Vec<InstrumentationConfig>,
7142
pub dc_module: String,
7243
}
7344

7445
impl Config {
75-
pub fn from_yaml_data(yaml_str: &str) -> Result<Config, OrchestrionError> {
76-
let docs = YamlLoader::load_from_str(yaml_str)?;
77-
let doc = &docs[0];
78-
79-
let version = doc["version"]
80-
.as_i64()
81-
.ok_or("Invalid config: 'version' must be a number")?;
82-
if version != 1 {
83-
return Err("Invalid config version".into());
46+
#[must_use]
47+
pub fn new(instrumentations: Vec<InstrumentationConfig>, dc_module: String) -> Self {
48+
Self {
49+
instrumentations,
50+
dc_module,
8451
}
85-
86-
let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel");
87-
88-
let configs = InstrumentationConfig::from_yaml(doc)?;
89-
90-
Ok(Config {
91-
instrumentations: configs,
92-
dc_module: dc_module.to_string(),
93-
})
9452
}
95-
}
96-
97-
impl InstrumentationConfig {
98-
pub fn from_yaml(doc: &Yaml) -> Result<Vec<InstrumentationConfig>, OrchestrionError> {
99-
let instrumentations = get_arr!(doc, "instrumentations");
100-
let mut configs = Vec::new();
10153

102-
for instr in instrumentations {
103-
instr
104-
.as_hash()
105-
.ok_or("Invalid config: 'instrumentations' must be a array of objects")?;
106-
configs.push(instr.try_into()?);
54+
#[must_use]
55+
pub fn new_single_with_default_dc_module(instrumentation: InstrumentationConfig) -> Self {
56+
Self {
57+
instrumentations: vec![instrumentation],
58+
dc_module: "diagnostics_channel".to_string(),
10759
}
108-
109-
Ok(configs)
11060
}
61+
}
11162

63+
impl InstrumentationConfig {
64+
#[must_use]
11265
pub fn matches(&self, module_name: &str, version: &str, file_path: &PathBuf) -> bool {
11366
let version: Version = match version.parse() {
11467
Ok(v) => v,
115-
Err(_) => return false,
68+
Err(e) => {
69+
println!("Failed to parse version {version}: {e}");
70+
return false;
71+
}
11672
};
73+
11774
self.module_name == module_name
11875
&& version.satisfies(&self.version_range)
11976
&& self.file_path == *file_path
12077
}
12178
}
122-
123-
impl TryFrom<&Yaml> for InstrumentationConfig {
124-
type Error = OrchestrionError;
125-
126-
fn try_from(instr: &Yaml) -> Result<Self, Self::Error> {
127-
let module_name = get_str!(instr, "module_name");
128-
let version_range = get_str!(instr, "version_range");
129-
let version_range: Range = version_range
130-
.parse()
131-
.map_err(|_| format!("Invalid version range: {version_range}"))?;
132-
let file_path = PathBuf::from(get_str!(instr, "file_path"));
133-
if instr["function_query"].as_hash().is_none() {
134-
return Err("Invalid config: 'function_query' must be a object".into());
135-
}
136-
let function_query = (&instr["function_query"]).try_into()?;
137-
let operator = InstrumentationOperator::from_str(get_str!(instr, "operator"))
138-
.unwrap_or(InstrumentationOperator::Sync);
139-
let channel_name = get_str!(instr, "channel_name");
140-
141-
Ok(InstrumentationConfig {
142-
module_name: module_name.to_string(),
143-
version_range,
144-
file_path,
145-
function_query,
146-
operator,
147-
channel_name: channel_name.to_string(),
148-
})
149-
}
150-
}

src/error.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::fmt::{self, Display, Formatter};
66

77
#[derive(Debug)]
88
pub enum OrchestrionError {
9-
InvalidVersionRange(String),
109
IoError(std::io::Error),
11-
YamlParseError(yaml_rust2::ScanError),
1210
StrError(String),
1311
}
1412

@@ -18,12 +16,6 @@ impl From<std::io::Error> for OrchestrionError {
1816
}
1917
}
2018

21-
impl From<yaml_rust2::ScanError> for OrchestrionError {
22-
fn from(e: yaml_rust2::ScanError) -> Self {
23-
OrchestrionError::YamlParseError(e)
24-
}
25-
}
26-
2719
impl From<String> for OrchestrionError {
2820
fn from(s: String) -> Self {
2921
OrchestrionError::StrError(s)
@@ -39,9 +31,7 @@ impl From<&str> for OrchestrionError {
3931
impl Display for OrchestrionError {
4032
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
4133
match self {
42-
OrchestrionError::InvalidVersionRange(s) => write!(f, "Invalid version range: {s}"),
4334
OrchestrionError::IoError(e) => write!(f, "IO error: {e}"),
44-
OrchestrionError::YamlParseError(e) => write!(f, "YAML parse error: {e}"),
4535
OrchestrionError::StrError(s) => write!(f, "String error: {s}"),
4636
}
4737
}

src/function_query.rs

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,16 @@
22
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
33
* This product includes software developed at Datadog (<https://www.datadoghq.com>/). Copyright 2025 Datadog, Inc.
44
**/
5-
use crate::error::OrchestrionError;
65
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
7-
use yaml_rust2::Yaml;
86

9-
macro_rules! get_str {
10-
($property:expr, $name:expr) => {
11-
$property[$name]
12-
.as_str()
13-
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
14-
};
15-
}
16-
17-
#[derive(Debug)]
7+
#[derive(Debug, Clone)]
188
pub enum FunctionType {
199
FunctionDeclaration,
2010
FunctionExpression,
2111
Method,
2212
}
2313

24-
impl FunctionType {
25-
pub fn from_str(s: &str) -> Option<FunctionType> {
26-
match s {
27-
"decl" => Some(FunctionType::FunctionDeclaration),
28-
"expr" => Some(FunctionType::FunctionExpression),
29-
"method" => Some(FunctionType::Method),
30-
_ => None,
31-
}
32-
}
33-
}
34-
35-
#[derive(Debug)]
14+
#[derive(Debug, Clone)]
3615
pub enum FunctionKind {
3716
Sync,
3817
Async,
@@ -41,14 +20,17 @@ pub enum FunctionKind {
4120
}
4221

4322
impl FunctionKind {
23+
#[must_use]
4424
pub fn is_async(&self) -> bool {
4525
matches!(self, FunctionKind::Async | FunctionKind::AsyncGenerator)
4626
}
4727

28+
#[must_use]
4829
pub fn is_generator(&self) -> bool {
4930
matches!(self, FunctionKind::Generator | FunctionKind::AsyncGenerator)
5031
}
5132

33+
#[must_use]
5234
pub fn matches(&self, func: &Function) -> bool {
5335
match self {
5436
FunctionKind::Sync => !func.is_async && !func.is_generator,
@@ -57,19 +39,9 @@ impl FunctionKind {
5739
FunctionKind::AsyncGenerator => func.is_async && func.is_generator,
5840
}
5941
}
60-
61-
pub fn from_str(s: &str) -> Option<FunctionKind> {
62-
match s {
63-
"sync" => Some(FunctionKind::Sync),
64-
"async" => Some(FunctionKind::Async),
65-
"generator" => Some(FunctionKind::Generator),
66-
"async generator" => Some(FunctionKind::AsyncGenerator),
67-
_ => None,
68-
}
69-
}
7042
}
7143

72-
#[derive(Debug)]
44+
#[derive(Debug, Clone)]
7345
pub struct FunctionQuery {
7446
pub name: String,
7547
pub class: Option<String>,
@@ -113,29 +85,3 @@ impl FunctionQuery {
11385
self.maybe_increment_count(matches_except_count, count)
11486
}
11587
}
116-
117-
impl TryFrom<&Yaml> for FunctionQuery {
118-
type Error = OrchestrionError;
119-
120-
fn try_from(query: &Yaml) -> Result<Self, Self::Error> {
121-
let typ = get_str!(query, "type");
122-
let kind = get_str!(query, "kind");
123-
let name = get_str!(query, "name");
124-
let class = query["class"]
125-
.as_str()
126-
.map(std::string::ToString::to_string);
127-
let index: usize = query["index"].as_i64().unwrap_or(0).try_into().unwrap_or(0);
128-
129-
Ok(FunctionQuery {
130-
name: name.to_string(),
131-
class,
132-
typ: FunctionType::from_str(typ).ok_or(format!(
133-
"Invalid config: 'type' must be one of 'decl', 'expr', or 'method', got '{typ}'"
134-
))?,
135-
kind: FunctionKind::from_str(kind).ok_or(format!(
136-
"Invalid config: 'kind' must be one of 'sync', 'async', 'generator', or 'async generator', got '{kind}'"
137-
))?,
138-
index,
139-
})
140-
}
141-
}

src/instrumentation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ macro_rules! ident {
2727
///
2828
/// [`Instrumentation`]: Instrumentation
2929
/// [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html
30+
#[derive(Debug)]
3031
pub struct Instrumentation {
3132
config: InstrumentationConfig,
3233
count: usize,
3334
is_correct_class: bool,
3435
}
3536

3637
impl Instrumentation {
37-
pub(crate) fn new(config: InstrumentationConfig) -> Self {
38+
#[must_use]
39+
pub fn new(config: InstrumentationConfig) -> Self {
3840
Self {
3941
config,
4042
count: 0,
@@ -230,7 +232,7 @@ impl Instrumentation {
230232
.function_query
231233
.class
232234
.as_ref()
233-
.map_or(true, |class| node.ident.sym.as_ref() == class);
235+
.is_none_or(|class| node.ident.sym.as_ref() == class);
234236
true
235237
}
236238

0 commit comments

Comments
 (0)