|
7 | 7 |
|
8 | 8 | import static java.lang.System.exit; |
9 | 9 |
|
10 | | -import io.opentelemetry.instrumentation.docs.utils.FileManager; |
| 10 | +import io.opentelemetry.instrumentation.docs.auditors.DocumentationAuditor; |
| 11 | +import io.opentelemetry.instrumentation.docs.auditors.SupportedLibrariesAuditor; |
| 12 | +import io.opentelemetry.instrumentation.docs.auditors.SuppressionListAuditor; |
11 | 13 | import java.io.IOException; |
12 | | -import java.net.URI; |
13 | 14 | import java.net.http.HttpClient; |
14 | | -import java.net.http.HttpRequest; |
15 | | -import java.net.http.HttpResponse; |
16 | | -import java.util.ArrayList; |
17 | 15 | import java.util.Arrays; |
18 | | -import java.util.HashSet; |
19 | 16 | import java.util.List; |
20 | | -import java.util.Map; |
21 | | -import java.util.Objects; |
22 | | -import java.util.Set; |
23 | | -import java.util.TreeSet; |
| 17 | +import java.util.Optional; |
24 | 18 | import java.util.logging.Logger; |
25 | | -import javax.annotation.Nullable; |
26 | | -import org.yaml.snakeyaml.Yaml; |
27 | 19 |
|
28 | 20 | /** |
29 | | - * This class is responsible for auditing and synchronizing documentation using the instrumentation |
30 | | - * list yaml. |
| 21 | + * This class is responsible for auditing and synchronizing documentation between the source of |
| 22 | + * truth (this repo) and the opentelemetry.io site. |
31 | 23 | */ |
32 | 24 | public class DocSynchronization { |
33 | 25 | private static final Logger logger = Logger.getLogger(DocSynchronization.class.getName()); |
34 | 26 |
|
35 | | - private static final String DOCUMENTATION_DISABLE_LIST = |
36 | | - "https://raw.githubusercontent.com/open-telemetry/opentelemetry.io/refs/heads/main/content/en/docs/zero-code/java/agent/disable.md"; |
| 27 | + private static final List<DocumentationAuditor> AUDITORS = |
| 28 | + List.of(new SuppressionListAuditor(), new SupportedLibrariesAuditor()); |
37 | 29 |
|
38 | | - // Used for consolidating instrumentation groups where we override the key with the value |
39 | | - private static final Map<String, String> INSTRUMENTATION_DISABLE_OVERRIDES = |
40 | | - Map.of("akka-actor-fork-join", "akka-actor"); |
41 | | - |
42 | | - private static final List<String> INSTRUMENTATION_EXCLUSIONS = |
43 | | - List.of("resources", "spring-boot-resources"); |
44 | | - |
45 | | - private DocSynchronization() {} |
46 | | - |
47 | | - /** |
48 | | - * Retrieves contents of the disable page from the main branch of the documentation site. |
49 | | - * |
50 | | - * @return the file content as a string |
51 | | - */ |
52 | | - private static String getDocumentationDisableList(HttpClient client) |
53 | | - throws IOException, InterruptedException { |
54 | | - HttpRequest request = |
55 | | - HttpRequest.newBuilder().uri(URI.create(DOCUMENTATION_DISABLE_LIST)).build(); |
56 | | - |
57 | | - HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
58 | | - if (response.statusCode() >= 200 && response.statusCode() < 300) { |
59 | | - return response.body(); |
60 | | - } |
61 | | - throw new IOException("Failed to fetch instrumentation list: " + response); |
62 | | - } |
63 | | - |
64 | | - @SuppressWarnings("unchecked") |
65 | | - public static List<String> parseInstrumentationList(String fileContent) { |
66 | | - List<String> instrumentationList = new ArrayList<>(); |
67 | | - Yaml yaml = new Yaml(); |
68 | | - Map<String, Object> data = yaml.load(fileContent); |
| 30 | + public static void main(String[] args) { |
| 31 | + HttpClient client = HttpClient.newHttpClient(); |
69 | 32 |
|
70 | | - if (data != null && data.get("libraries") instanceof Map) { |
71 | | - Map<String, List<Map<String, Object>>> libraries = |
72 | | - (Map<String, List<Map<String, Object>>>) data.get("libraries"); |
73 | | - for (List<Map<String, Object>> libraryGroup : libraries.values()) { |
74 | | - for (Map<String, Object> instrumentation : libraryGroup) { |
75 | | - if (instrumentation.get("name") instanceof String) { |
76 | | - instrumentationList.add((String) instrumentation.get("name")); |
| 33 | + try { |
| 34 | + boolean hasFailures = false; |
| 35 | + StringBuilder combinedMessage = new StringBuilder(); |
| 36 | + |
| 37 | + for (DocumentationAuditor auditor : AUDITORS) { |
| 38 | + try { |
| 39 | + logger.info("Running " + auditor.getAuditorName() + "..."); |
| 40 | + Optional<String> result = auditor.performAudit(client); |
| 41 | + |
| 42 | + if (result.isPresent()) { |
| 43 | + hasFailures = true; |
| 44 | + if (!combinedMessage.isEmpty()) { |
| 45 | + combinedMessage.append("\n\n"); |
| 46 | + } |
| 47 | + combinedMessage.append(result.get()); |
77 | 48 | } |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - return instrumentationList; |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Identifies missing items in the instrumentation list that are not present in the documentation |
86 | | - * disable list. Takes into account any overrides specified in INSTRUMENTATION_DISABLE_OVERRIDES |
87 | | - * and excludes items listed in INSTRUMENTATION_EXCLUSIONS. |
88 | | - * |
89 | | - * @param documentationDisabledList a list of items that are documented |
90 | | - * @param instrumentationList a list of instrumentations from the instrumentation list |
91 | | - * @return a list of missing items that should be documented |
92 | | - */ |
93 | | - public static List<String> identifyMissingItems( |
94 | | - List<String> documentationDisabledList, List<String> instrumentationList) { |
95 | | - Set<String> documentationDisabledSet = new HashSet<>(documentationDisabledList); |
96 | | - |
97 | | - Set<String> sanitizedInstrumentationItems = new TreeSet<>(); |
98 | | - for (String item : instrumentationList) { |
99 | | - sanitizedInstrumentationItems.add(item.replaceFirst("-[0-9].*$", "")); |
100 | | - } |
101 | | - |
102 | | - List<String> missingItems = new ArrayList<>(); |
103 | | - for (String item : sanitizedInstrumentationItems) { |
104 | | - if (INSTRUMENTATION_EXCLUSIONS.contains(item)) { |
105 | | - continue; // Skip excluded items |
106 | | - } |
107 | | - String itemToCheck = INSTRUMENTATION_DISABLE_OVERRIDES.getOrDefault(item, item); |
108 | | - boolean found = false; |
109 | | - for (String disabledItem : documentationDisabledSet) { |
110 | | - if (itemToCheck.startsWith(disabledItem)) { |
111 | | - found = true; |
112 | | - break; |
113 | | - } |
114 | | - } |
115 | | - if (!found) { |
116 | | - missingItems.add(item); |
117 | | - } |
118 | | - } |
119 | | - return missingItems; |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Retrieves the instrumentation list yaml file. |
124 | | - * |
125 | | - * @return a string representation of the instrumentation list |
126 | | - */ |
127 | | - @Nullable |
128 | | - private static String getInstrumentationList() { |
129 | | - // Identify path to repo so we can use absolute paths |
130 | | - String baseRepoPath = System.getProperty("basePath"); |
131 | | - if (baseRepoPath == null) { |
132 | | - baseRepoPath = "./"; |
133 | | - } else { |
134 | | - baseRepoPath += "/"; |
135 | | - } |
136 | | - |
137 | | - String file = baseRepoPath + "docs/instrumentation-list.yaml"; |
138 | | - return FileManager.readFileToString(file); |
139 | | - } |
140 | | - |
141 | | - /** |
142 | | - * Parses the documentation disabled list from the file content and turns it into a list of |
143 | | - * instrumentation names. |
144 | | - * |
145 | | - * @param fileContent the content of the disable.md documentation file |
146 | | - * @return a list of instrumentation names that are documented |
147 | | - */ |
148 | | - public static List<String> parseDocumentationDisabledList(String fileContent) { |
149 | | - List<String> instrumentationList = new ArrayList<>(); |
150 | | - String[] lines = fileContent.split("\\R"); |
151 | | - for (String line : lines) { |
152 | | - if (line.trim().startsWith("|")) { |
153 | | - String[] parts = line.split("\\|"); |
154 | | - if (parts.length > 2) { |
155 | | - String potentialName = parts[2].trim(); |
156 | | - if (potentialName.startsWith("`") && potentialName.endsWith("`")) { |
157 | | - String name = potentialName.substring(1, potentialName.length() - 1); |
158 | | - instrumentationList.add(name); |
| 49 | + } catch (IOException | InterruptedException | RuntimeException e) { |
| 50 | + logger.severe("Error running " + auditor.getAuditorName() + ": " + e.getMessage()); |
| 51 | + hasFailures = true; |
| 52 | + if (!combinedMessage.isEmpty()) { |
| 53 | + combinedMessage.append("\n\n"); |
159 | 54 | } |
| 55 | + combinedMessage |
| 56 | + .append("Error in ") |
| 57 | + .append(auditor.getAuditorName()) |
| 58 | + .append(": ") |
| 59 | + .append(e.getMessage()); |
160 | 60 | } |
161 | 61 | } |
162 | | - } |
163 | | - return instrumentationList; |
164 | | - } |
165 | 62 |
|
166 | | - public static void main(String[] args) { |
167 | | - HttpClient client = HttpClient.newHttpClient(); |
168 | | - |
169 | | - try { |
170 | | - String content = getDocumentationDisableList(client); |
171 | | - List<String> disabledList = parseDocumentationDisabledList(content); |
172 | | - |
173 | | - String instrumentationListContent = Objects.requireNonNull(getInstrumentationList()); |
174 | | - List<String> instrumentationList = parseInstrumentationList(instrumentationListContent); |
175 | | - |
176 | | - List<String> missingItems = identifyMissingItems(disabledList, instrumentationList); |
177 | | - |
178 | | - if (missingItems.isEmpty()) { |
179 | | - logger.info("No missing items found."); |
180 | | - } else { |
181 | | - StringBuilder sb = new StringBuilder(); |
182 | | - sb.append("Missing Instrumentation List (") |
183 | | - .append(missingItems.size()) |
184 | | - .append(" item(s) missing):\n"); |
185 | | - missingItems.forEach(item -> sb.append(" - ").append(item).append("\n")); |
186 | | - logger.severe(sb.toString()); |
| 63 | + if (hasFailures) { |
| 64 | + // Add custom markers and "How to Fix" section for GitHub workflow extraction |
| 65 | + StringBuilder finalMessage = new StringBuilder(); |
| 66 | + finalMessage.append("=== AUDIT_FAILURE_START ===\n"); |
| 67 | + finalMessage.append(combinedMessage.toString()); |
| 68 | + finalMessage.append("\n\n## How to Fix\n\n"); |
| 69 | + finalMessage.append( |
| 70 | + "For guidance on updating the OpenTelemetry.io documentation, see: [Documenting Instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/documenting-instrumentation.md#opentelemetryio)"); |
| 71 | + finalMessage.append("\n=== AUDIT_FAILURE_END ==="); |
| 72 | + |
| 73 | + logger.severe(finalMessage.toString()); |
187 | 74 | exit(1); |
| 75 | + } else { |
| 76 | + logger.info("All documentation audits passed successfully."); |
188 | 77 | } |
189 | 78 |
|
190 | | - } catch (IOException | InterruptedException e) { |
191 | | - logger.severe("Error fetching instrumentation list: " + e.getMessage()); |
| 79 | + } catch (RuntimeException e) { |
| 80 | + logger.severe("Error running documentation audits: " + e.getMessage()); |
192 | 81 | logger.severe(Arrays.toString(e.getStackTrace())); |
193 | 82 | exit(1); |
194 | 83 | } |
195 | 84 | } |
| 85 | + |
| 86 | + private DocSynchronization() {} |
196 | 87 | } |
0 commit comments