-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstrumentationAnalyzer.java
96 lines (82 loc) · 3.53 KB
/
InstrumentationAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.docs;
import static io.opentelemetry.instrumentation.docs.GradleParser.parseGradleFile;
import io.opentelemetry.instrumentation.docs.utils.FileManager;
import io.opentelemetry.instrumentation.docs.utils.InstrumentationPath;
import io.opentelemetry.instrumentation.docs.utils.YamlHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
class InstrumentationAnalyzer {
private final FileManager fileSearch;
InstrumentationAnalyzer(FileManager fileSearch) {
this.fileSearch = fileSearch;
}
/**
* Converts a list of InstrumentationPath objects into a list of InstrumentationEntity objects.
* Each InstrumentationEntity represents a unique combination of group, namespace, and
* instrumentation name. The types of instrumentation (e.g., library, javaagent) are aggregated
* into a list within each entity.
*
* @param paths the list of InstrumentationPath objects to be converted
* @return a list of InstrumentationEntity objects with aggregated types
*/
public static List<InstrumentationEntity> convertToEntities(List<InstrumentationPath> paths) {
Map<String, InstrumentationEntity> entityMap = new HashMap<>();
for (InstrumentationPath path : paths) {
String key = path.group() + ":" + path.namespace() + ":" + path.instrumentationName();
if (!entityMap.containsKey(key)) {
entityMap.put(
key,
new InstrumentationEntity(
path.srcPath().replace("/javaagent", "").replace("/library", ""),
path.instrumentationName(),
path.namespace(),
path.group()));
}
}
return new ArrayList<>(entityMap.values());
}
/**
* Analyzes the given root directory to find all instrumentation paths and then analyze them.
* Extracts version information from each instrumentation's build.gradle file. Extracts
* information from metadata.yaml files.
*
* @return a list of InstrumentationEntity objects with target versions
*/
List<InstrumentationEntity> analyze() {
List<InstrumentationPath> paths = fileSearch.getInstrumentationPaths();
List<InstrumentationEntity> entities = convertToEntities(paths);
for (InstrumentationEntity entity : entities) {
List<String> gradleFiles = fileSearch.findBuildGradleFiles(entity.getSrcPath());
analyzeVersions(gradleFiles, entity);
String metadataFile = fileSearch.getMetaDataFile(entity.getSrcPath());
if (metadataFile != null) {
entity.setMetadata(YamlHelper.metaDataParser(metadataFile));
}
}
return entities;
}
void analyzeVersions(List<String> files, InstrumentationEntity entity) {
Map<InstrumentationType, Set<String>> versions = new HashMap<>();
for (String file : files) {
String fileContents = fileSearch.readFileToString(file);
if (file.contains("/javaagent/")) {
Set<String> results = parseGradleFile(fileContents, InstrumentationType.JAVAAGENT);
versions
.computeIfAbsent(InstrumentationType.JAVAAGENT, k -> new HashSet<>())
.addAll(results);
} else if (file.contains("/library/")) {
Set<String> results = parseGradleFile(fileContents, InstrumentationType.LIBRARY);
versions.computeIfAbsent(InstrumentationType.LIBRARY, k -> new HashSet<>()).addAll(results);
}
}
entity.setTargetVersions(versions);
}
}