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 ;
105use 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 ) ]
3110pub enum InstrumentationOperator {
@@ -36,6 +15,7 @@ pub enum InstrumentationOperator {
3615}
3716
3817impl 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 ) ]
6030pub 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 ) ]
6940pub struct Config {
7041 pub instrumentations : Vec < InstrumentationConfig > ,
7142 pub dc_module : String ,
7243}
7344
7445impl 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- }
0 commit comments