1818
1919import com .splunk .opentelemetry .javaagent .bootstrap .nocode .NocodeExpression ;
2020import com .splunk .opentelemetry .javaagent .bootstrap .nocode .NocodeRules ;
21+ import io .opentelemetry .api .incubator .config .DeclarativeConfigProperties ;
2122import io .opentelemetry .api .trace .SpanKind ;
2223import io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers ;
2324import java .io .IOException ;
3334import java .util .Locale ;
3435import java .util .Map ;
3536import java .util .logging .Logger ;
37+ import java .util .stream .Collectors ;
3638import net .bytebuddy .description .NamedElement ;
3739import net .bytebuddy .description .method .MethodDescription ;
3840import net .bytebuddy .description .type .TypeDescription ;
@@ -58,47 +60,69 @@ static List<NocodeRules.Rule> parseFromString(String yaml) throws IOException {
5860 return new YamlParser (new StringReader (yaml )).getInstrumentationRules ();
5961 }
6062
63+ static List <NocodeRules .Rule > parseFromDeclarativeConfig (
64+ List <DeclarativeConfigProperties > ruleNodes ) {
65+ List <Map <String , Object >> rulesList =
66+ ruleNodes .stream ().map (DeclarativeConfigProperties ::toMap ).collect (Collectors .toList ());
67+
68+ return new YamlParser (rulesList ).getInstrumentationRules ();
69+ }
70+
6171 private YamlParser (Reader reader ) throws IOException {
6272 instrumentationRules = Collections .unmodifiableList (new ArrayList <>(loadUnsafe (reader )));
6373 }
6474
75+ private YamlParser (List <Map <String , Object >> ruleListNode ) {
76+ instrumentationRules = Collections .unmodifiableList (loadUnsafe (ruleListNode ));
77+ }
78+
6579 private List <NocodeRules .Rule > getInstrumentationRules () {
6680 return instrumentationRules ;
6781 }
6882
83+ @ SuppressWarnings ("unchecked" )
84+ private List <NocodeRules .Rule > loadUnsafe (List <Map <String , Object >> ruleListNode ) {
85+ List <NocodeRules .Rule > answer = new ArrayList <>();
86+
87+ for (Map <String , Object > yamlRule : ruleListNode ) {
88+ ElementMatcher <TypeDescription > classMatcher = classMatcher (yamlRule .get ("class" ));
89+ ElementMatcher <MethodDescription > methodMatcher = methodMatcher (yamlRule .get ("method" ));
90+ NocodeExpression spanName = toExpression (yamlRule .get ("span_name" ));
91+ SpanKind spanKind = null ;
92+ if (yamlRule .get ("span_kind" ) != null ) {
93+ String spanKindString = yamlRule .get ("span_kind" ).toString ();
94+ try {
95+ spanKind = SpanKind .valueOf (spanKindString .toUpperCase (Locale .ROOT ));
96+ } catch (IllegalArgumentException exception ) {
97+ logger .warning ("Invalid span kind " + spanKindString );
98+ }
99+ }
100+ NocodeExpression spanStatus = toExpression (yamlRule .get ("span_status" ));
101+
102+ Map <String , NocodeExpression > ruleAttributes = new HashMap <>();
103+ List <Map <String , Object >> attrs = (List <Map <String , Object >>) yamlRule .get ("attributes" );
104+ if (attrs != null ) {
105+ for (Map <String , Object > attr : attrs ) {
106+ ruleAttributes .put (attr .get ("key" ).toString (), toExpression (attr .get ("value" )));
107+ }
108+ }
109+ answer .add (
110+ new RuleImpl (
111+ classMatcher , methodMatcher , spanName , spanKind , spanStatus , ruleAttributes ));
112+ }
113+
114+ return answer ;
115+ }
116+
69117 @ SuppressWarnings ("unchecked" )
70118 private List <NocodeRules .Rule > loadUnsafe (Reader reader ) throws IOException {
71119 List <NocodeRules .Rule > answer = new ArrayList <>();
72120 Load load = new Load (LoadSettings .builder ().build ());
73121 Iterable <Object > parsedYaml = load .loadAllFromReader (reader );
122+
74123 for (Object yamlBit : parsedYaml ) {
75124 List <Map <String , Object >> rulesMap = (List <Map <String , Object >>) yamlBit ;
76- for (Map <String , Object > yamlRule : rulesMap ) {
77- ElementMatcher <TypeDescription > classMatcher = classMatcher (yamlRule .get ("class" ));
78- ElementMatcher <MethodDescription > methodMatcher = methodMatcher (yamlRule .get ("method" ));
79- NocodeExpression spanName = toExpression (yamlRule .get ("span_name" ));
80- SpanKind spanKind = null ;
81- if (yamlRule .get ("span_kind" ) != null ) {
82- String spanKindString = yamlRule .get ("span_kind" ).toString ();
83- try {
84- spanKind = SpanKind .valueOf (spanKindString .toUpperCase (Locale .ROOT ));
85- } catch (IllegalArgumentException exception ) {
86- logger .warning ("Invalid span kind " + spanKindString );
87- }
88- }
89- NocodeExpression spanStatus = toExpression (yamlRule .get ("span_status" ));
90-
91- Map <String , NocodeExpression > ruleAttributes = new HashMap <>();
92- List <Map <String , Object >> attrs = (List <Map <String , Object >>) yamlRule .get ("attributes" );
93- if (attrs != null ) {
94- for (Map <String , Object > attr : attrs ) {
95- ruleAttributes .put (attr .get ("key" ).toString (), toExpression (attr .get ("value" )));
96- }
97- }
98- answer .add (
99- new RuleImpl (
100- classMatcher , methodMatcher , spanName , spanKind , spanStatus , ruleAttributes ));
101- }
125+ answer .addAll (loadUnsafe (rulesMap ));
102126 }
103127
104128 return answer ;
0 commit comments