|
9 | 9 |
|
10 | 10 | package org.elasticsearch.ingest.otel; |
11 | 11 |
|
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
12 | 14 | import org.elasticsearch.xcontent.XContentFactory; |
13 | 15 | import org.elasticsearch.xcontent.XContentParser; |
14 | 16 | import org.elasticsearch.xcontent.XContentParserConfiguration; |
@@ -44,6 +46,8 @@ class OTelSemConvWebCrawler { |
44 | 46 |
|
45 | 47 | private static final String GITHUB_TOKEN = System.getenv("GITHUB_TOKEN"); |
46 | 48 |
|
| 49 | + private static final Logger logger = LogManager.getLogger(OTelSemConvWebCrawler.class); |
| 50 | + |
47 | 51 | /** |
48 | 52 | * Relies on GitHub API to crawl the OpenTelemetry semantic conventions repo. |
49 | 53 | * This method blocks until all resource attributes are collected, which may take a while. |
@@ -142,15 +146,15 @@ private static Stream<String> findAllYamlFiles(HttpClient client) { |
142 | 146 | } |
143 | 147 | } |
144 | 148 | } else if (response.statusCode() == 403) { |
145 | | - System.err.println( |
| 149 | + logger.error( |
146 | 150 | "GitHub API rate limit exceeded. " |
147 | 151 | + "Please provide a GitHub token via GITHUB_TOKEN environment variable" |
148 | 152 | ); |
149 | 153 | } else { |
150 | | - System.err.println("GitHub API request failed: HTTP " + response.statusCode()); |
| 154 | + logErrorHttpStatusCode(response); |
151 | 155 | } |
152 | 156 | } catch (IOException e) { |
153 | | - System.err.println("Error processing response: " + e.getMessage()); |
| 157 | + logger.error("Error processing response: {}", e.getMessage()); |
154 | 158 | } |
155 | 159 | return null; |
156 | 160 | }); |
@@ -204,6 +208,10 @@ public boolean tryAdvance(Consumer<? super String> action) { |
204 | 208 | }); |
205 | 209 | } |
206 | 210 |
|
| 211 | + private static void logErrorHttpStatusCode(HttpResponse<InputStream> response) { |
| 212 | + logger.error("GitHub API request failed: HTTP {}", response.statusCode()); |
| 213 | + } |
| 214 | + |
207 | 215 | private static CompletableFuture<Set<String>> extractResourceAttributesFromFile(String fileDownloadUrl, HttpClient httpClient) { |
208 | 216 | return downloadAndParseYaml(fileDownloadUrl, httpClient).thenApply(yamlData -> { |
209 | 217 | Set<String> resourceAttributes = new HashSet<>(); |
@@ -261,21 +269,22 @@ private static HttpRequest createRequest(String currentUrl) { |
261 | 269 | private static CompletableFuture<Map<String, Object>> downloadAndParseYaml(String rawFileUrl, HttpClient client) { |
262 | 270 | HttpRequest request = HttpRequest.newBuilder(URI.create(rawFileUrl)).build(); |
263 | 271 | CompletableFuture<HttpResponse<InputStream>> responseFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()); |
264 | | - return responseFuture.thenApply(response -> { |
265 | | - try ( |
266 | | - InputStream inputStream = response.body(); |
267 | | - XContentParser parser = XContentFactory.xContent(XContentType.YAML) |
268 | | - .createParser(XContentParserConfiguration.EMPTY, inputStream) |
269 | | - ) { |
270 | | - return parser.map(); |
271 | | - } catch (IOException e) { |
272 | | - System.err.println("Error parsing YAML file: " + e.getMessage()); |
273 | | - return Map.of(); |
274 | | - } finally { |
275 | | - if (response.statusCode() != 200) { |
276 | | - System.err.println("GitHub API request failed: HTTP " + response.statusCode()); |
277 | | - } |
| 272 | + return responseFuture.thenApply(OTelSemConvWebCrawler::apply); |
| 273 | + } |
| 274 | + |
| 275 | + private static Map<String, Object> apply(HttpResponse<InputStream> response) { |
| 276 | + try ( |
| 277 | + InputStream inputStream = response.body(); |
| 278 | + XContentParser parser = XContentFactory.xContent(XContentType.YAML).createParser(XContentParserConfiguration.EMPTY, inputStream) |
| 279 | + ) { |
| 280 | + return parser.map(); |
| 281 | + } catch (IOException e) { |
| 282 | + logger.error("Error parsing YAML file: {}", e.getMessage()); |
| 283 | + return Map.of(); |
| 284 | + } finally { |
| 285 | + if (response.statusCode() != 200) { |
| 286 | + logErrorHttpStatusCode(response); |
278 | 287 | } |
279 | | - }); |
| 288 | + } |
280 | 289 | } |
281 | 290 | } |
0 commit comments