55
66package io .opentelemetry .instrumentation .docs ;
77
8- import static io .opentelemetry .instrumentation .docs .parsers .GradleParser .parseGradleFile ;
9-
8+ import com .fasterxml .jackson .core .JsonProcessingException ;
109import com .fasterxml .jackson .databind .exc .ValueInstantiationException ;
11- import io .opentelemetry .instrumentation .docs .internal .DependencyInfo ;
1210import io .opentelemetry .instrumentation .docs .internal .EmittedMetrics ;
11+ import io .opentelemetry .instrumentation .docs .internal .InstrumentationMetaData ;
1312import io .opentelemetry .instrumentation .docs .internal .InstrumentationModule ;
1413import io .opentelemetry .instrumentation .docs .internal .InstrumentationType ;
15- import io .opentelemetry .instrumentation .docs .parsers .MetricParser ;
14+ import io .opentelemetry .instrumentation .docs .parsers .EmittedMetricsParser ;
15+ import io .opentelemetry .instrumentation .docs .parsers .GradleParser ;
16+ import io .opentelemetry .instrumentation .docs .parsers .ModuleParser ;
17+ import io .opentelemetry .instrumentation .docs .parsers .SpanParser ;
1618import io .opentelemetry .instrumentation .docs .utils .FileManager ;
1719import io .opentelemetry .instrumentation .docs .utils .InstrumentationPath ;
1820import io .opentelemetry .instrumentation .docs .utils .YamlHelper ;
1921import java .io .IOException ;
20- import java .util .ArrayList ;
2122import java .util .HashMap ;
22- import java .util .HashSet ;
2323import java .util .List ;
2424import java .util .Map ;
2525import java .util .Set ;
2626import java .util .logging .Logger ;
27+ import javax .annotation .Nullable ;
2728
29+ /**
30+ * Analyzes instrumentation modules by extracting version information, metrics, spans, and metadata
31+ * from various source files.
32+ */
2833class InstrumentationAnalyzer {
2934
3035 private static final Logger logger = Logger .getLogger (InstrumentationAnalyzer .class .getName ());
@@ -36,100 +41,74 @@ class InstrumentationAnalyzer {
3641 }
3742
3843 /**
39- * Converts a list of {@link InstrumentationPath} into a list of {@link InstrumentationModule},
44+ * Analyzes all instrumentation modules found in the root directory.
4045 *
41- * @param paths the list of {@link InstrumentationPath} objects to be converted
42- * @return a list of {@link InstrumentationModule} objects with aggregated types
46+ * @return a list of analyzed {@link InstrumentationModule}
47+ * @throws IOException if file operations fail
4348 */
44- public static List <InstrumentationModule > convertToInstrumentationModules (
45- String rootPath , List <InstrumentationPath > paths ) {
46- Map <String , InstrumentationModule > moduleMap = new HashMap <>();
47-
48- for (InstrumentationPath path : paths ) {
49- String key = path .group () + ":" + path .namespace () + ":" + path .instrumentationName ();
50- if (!moduleMap .containsKey (key )) {
51- moduleMap .put (
52- key ,
53- new InstrumentationModule .Builder ()
54- .srcPath (sanitizePathName (rootPath , path .srcPath ()))
55- .instrumentationName (path .instrumentationName ())
56- .namespace (path .namespace ())
57- .group (path .group ())
58- .build ());
59- }
49+ public List <InstrumentationModule > analyze () throws IOException {
50+ List <InstrumentationPath > paths = fileManager .getInstrumentationPaths ();
51+ List <InstrumentationModule > modules =
52+ ModuleParser .convertToModules (fileManager .rootDir (), paths );
53+
54+ for (InstrumentationModule module : modules ) {
55+ enrichModule (module );
6056 }
6157
62- return new ArrayList <>( moduleMap . values ()) ;
58+ return modules ;
6359 }
6460
65- private static String sanitizePathName (String rootPath , String path ) {
66- return path .replace (rootPath , "" ).replace ("/javaagent" , "" ).replace ("/library" , "" );
61+ private void enrichModule (InstrumentationModule module ) throws IOException {
62+ InstrumentationMetaData metaData = getMetadata (module );
63+ if (metaData != null ) {
64+ module .setMetadata (metaData );
65+ }
66+
67+ module .setTargetVersions (getVersionInformation (module ));
68+ module .setMetrics (MetricsProcessor .getMetrics (module , fileManager ));
69+ module .setSpans (SpanParser .getSpans (module , fileManager ));
6770 }
6871
69- /**
70- * Traverses the given root directory to find all instrumentation paths and then analyzes them.
71- * Extracts version information from each instrumentation's build.gradle file, metric data from
72- * files in the .telemetry directories, and other information from metadata.yaml files.
73- *
74- * @return a list of {@link InstrumentationModule}
75- */
76- List <InstrumentationModule > analyze () throws IOException {
77- List <InstrumentationPath > paths = fileManager .getInstrumentationPaths ();
78- List <InstrumentationModule > modules =
79- convertToInstrumentationModules (fileManager .rootDir (), paths );
72+ @ Nullable
73+ private InstrumentationMetaData getMetadata (InstrumentationModule module )
74+ throws JsonProcessingException {
75+ String metadataFile = fileManager .getMetaDataFile (module .getSrcPath ());
76+ if (metadataFile == null ) {
77+ return null ;
78+ }
79+ try {
80+ return YamlHelper .metaDataParser (metadataFile );
81+ } catch (ValueInstantiationException e ) {
82+ logger .severe ("Error parsing metadata file for " + module .getInstrumentationName ());
83+ throw e ;
84+ }
85+ }
8086
81- for (InstrumentationModule module : modules ) {
82- List <String > gradleFiles = fileManager .findBuildGradleFiles (module .getSrcPath ());
83- analyzeVersions (gradleFiles , module );
84-
85- String metadataFile = fileManager .getMetaDataFile (module .getSrcPath ());
86- if (metadataFile != null ) {
87- try {
88- module .setMetadata (YamlHelper .metaDataParser (metadataFile ));
89- } catch (ValueInstantiationException e ) {
90- logger .severe ("Error parsing metadata file for " + module .getInstrumentationName ());
91- throw e ;
92- }
93- }
87+ private Map <InstrumentationType , Set <String >> getVersionInformation (
88+ InstrumentationModule module ) {
89+ List <String > gradleFiles = fileManager .findBuildGradleFiles (module .getSrcPath ());
90+ return GradleParser .extractVersions (gradleFiles , module );
91+ }
92+
93+ /** Handles processing of metrics data for instrumentation modules. */
94+ static class MetricsProcessor {
9495
96+ public static Map <String , List <EmittedMetrics .Metric >> getMetrics (
97+ InstrumentationModule module , FileManager fileManager ) {
9598 Map <String , EmittedMetrics > metrics =
96- MetricParser .getMetricsFromFiles (fileManager .rootDir (), module .getSrcPath ());
97-
98- for (Map .Entry <String , EmittedMetrics > entry : metrics .entrySet ()) {
99- if (entry .getValue () == null || entry .getValue ().getMetrics () == null ) {
100- continue ;
101- }
102- module .getMetrics ().put (entry .getKey (), entry .getValue ().getMetrics ());
103- }
99+ EmittedMetricsParser .getMetricsFromFiles (fileManager .rootDir (), module .getSrcPath ());
100+
101+ Map <String , List <EmittedMetrics .Metric >> result = new HashMap <>();
102+ metrics .entrySet ().stream ()
103+ .filter (MetricsProcessor ::hasValidMetrics )
104+ .forEach (entry -> result .put (entry .getKey (), entry .getValue ().getMetrics ()));
105+ return result ;
104106 }
105- return modules ;
106- }
107107
108- void analyzeVersions (List <String > files , InstrumentationModule module ) {
109- Map <InstrumentationType , Set <String >> versions = new HashMap <>();
110- for (String file : files ) {
111- String fileContents = FileManager .readFileToString (file );
112- if (fileContents == null ) {
113- continue ;
114- }
115-
116- DependencyInfo results = null ;
117-
118- if (file .contains ("/javaagent/" )) {
119- results = parseGradleFile (fileContents , InstrumentationType .JAVAAGENT );
120- versions
121- .computeIfAbsent (InstrumentationType .JAVAAGENT , k -> new HashSet <>())
122- .addAll (results .versions ());
123- } else if (file .contains ("/library/" )) {
124- results = parseGradleFile (fileContents , InstrumentationType .LIBRARY );
125- versions
126- .computeIfAbsent (InstrumentationType .LIBRARY , k -> new HashSet <>())
127- .addAll (results .versions ());
128- }
129- if (results != null && results .minJavaVersionSupported () != null ) {
130- module .setMinJavaVersion (results .minJavaVersionSupported ());
131- }
108+ private static boolean hasValidMetrics (Map .Entry <String , EmittedMetrics > entry ) {
109+ return entry .getValue () != null && entry .getValue ().getMetrics () != null ;
132110 }
133- module .setTargetVersions (versions );
111+
112+ private MetricsProcessor () {}
134113 }
135114}
0 commit comments