5
5
6
6
package io .opentelemetry .instrumentation .docs ;
7
7
8
- import static io .opentelemetry .instrumentation .docs .parsers .GradleParser .parseGradleFile ;
9
-
8
+ import com .fasterxml .jackson .core .JsonProcessingException ;
10
9
import com .fasterxml .jackson .databind .exc .ValueInstantiationException ;
11
- import io .opentelemetry .instrumentation .docs .internal .DependencyInfo ;
12
10
import io .opentelemetry .instrumentation .docs .internal .EmittedMetrics ;
11
+ import io .opentelemetry .instrumentation .docs .internal .InstrumentationMetaData ;
13
12
import io .opentelemetry .instrumentation .docs .internal .InstrumentationModule ;
14
13
import 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 ;
16
18
import io .opentelemetry .instrumentation .docs .utils .FileManager ;
17
19
import io .opentelemetry .instrumentation .docs .utils .InstrumentationPath ;
18
20
import io .opentelemetry .instrumentation .docs .utils .YamlHelper ;
19
21
import java .io .IOException ;
20
- import java .util .ArrayList ;
21
22
import java .util .HashMap ;
22
- import java .util .HashSet ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
import java .util .Set ;
26
26
import java .util .logging .Logger ;
27
+ import javax .annotation .Nullable ;
27
28
29
+ /**
30
+ * Analyzes instrumentation modules by extracting version information, metrics, spans, and metadata
31
+ * from various source files.
32
+ */
28
33
class InstrumentationAnalyzer {
29
34
30
35
private static final Logger logger = Logger .getLogger (InstrumentationAnalyzer .class .getName ());
@@ -36,100 +41,74 @@ class InstrumentationAnalyzer {
36
41
}
37
42
38
43
/**
39
- * Converts a list of {@link InstrumentationPath} into a list of {@link InstrumentationModule},
44
+ * Analyzes all instrumentation modules found in the root directory.
40
45
*
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
43
48
*/
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 );
60
56
}
61
57
62
- return new ArrayList <>( moduleMap . values ()) ;
58
+ return modules ;
63
59
}
64
60
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 ));
67
70
}
68
71
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
+ }
80
86
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 {
94
95
96
+ public static Map <String , List <EmittedMetrics .Metric >> getMetrics (
97
+ InstrumentationModule module , FileManager fileManager ) {
95
98
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 ;
104
106
}
105
- return modules ;
106
- }
107
107
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 ;
132
110
}
133
- module .setTargetVersions (versions );
111
+
112
+ private MetricsProcessor () {}
134
113
}
135
114
}
0 commit comments