Skip to content

Commit b6c6b16

Browse files
committed
Close leaky resources in azure-resources, jfr-connection
1 parent 0435d65 commit b6c6b16

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

azure-resources/src/main/java/io/opentelemetry/contrib/azure/resource/AzureMetadataService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ static Optional<String> fetchMetadata(URL url) {
4646
.connectTimeout(TIMEOUT)
4747
.readTimeout(TIMEOUT)
4848
.build();
49-
5049
Request request = new Request.Builder().url(url).get().addHeader("Metadata", "true").build();
51-
5250
try (Response response = client.newCall(request).execute()) {
5351
int responseCode = response.code();
5452
if (responseCode != 200) {
@@ -62,11 +60,13 @@ static Optional<String> fetchMetadata(URL url) {
6260
+ response.message());
6361
return Optional.empty();
6462
}
65-
6663
return Optional.of(requireNonNull(response.body()).string());
6764
} catch (IOException e) {
6865
logger.log(Level.FINE, "Failed to fetch Azure VM metadata", e);
6966
return Optional.empty();
67+
} finally {
68+
client.dispatcher().executorService().shutdown();
69+
client.connectionPool().evictAll();
7070
}
7171
}
7272

jfr-connection/src/main/java/io/opentelemetry/contrib/jfr/connection/JfcFileConfiguration.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ public JfcFileConfiguration(InputStream configurationFile) {
3333
}
3434

3535
private static String readConfigurationFile(InputStream inputStream) {
36-
if (inputStream != null) {
37-
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
38-
.lines()
39-
.collect(Collectors.joining());
40-
} else {
36+
if (inputStream == null) {
4137
throw new IllegalArgumentException("Null configuration provided");
4238
}
39+
try (BufferedReader reader =
40+
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
41+
return reader.lines().collect(Collectors.joining());
42+
} catch (IOException e) {
43+
throw new IllegalStateException("Failed to read JFC configuration file", e);
44+
}
4345
}
4446

4547
@Override

0 commit comments

Comments
 (0)