Skip to content

Commit a865c65

Browse files
authored
refactor: update exception handling to use specific exception types (#1843)
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 1f3865f commit a865c65

File tree

7 files changed

+18
-11
lines changed

7 files changed

+18
-11
lines changed

prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private void sendErrorResponseWithStackTrace(Exception requestHandlerException)
112112
httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
113113
httpExchange.sendResponseHeaders(500, stackTrace.length);
114114
httpExchange.getResponseBody().write(stackTrace);
115-
} catch (Exception errorWriterException) {
115+
} catch (IOException errorWriterException) {
116116
// We want to avoid logging so that we don't mess with application logs when the HTTPServer
117117
// is used in a Java agent.
118118
// However, if we can't even send an error response to the client there's nothing we can do

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static Resource getResourceField(AutoConfiguredOpenTelemetrySdk sdk) {
112112
Method method = AutoConfiguredOpenTelemetrySdk.class.getDeclaredMethod("getResource");
113113
method.setAccessible(true);
114114
return (Resource) method.invoke(sdk);
115-
} catch (Exception e) {
115+
} catch (ReflectiveOperationException e) {
116116
throw new RuntimeException(e);
117117
}
118118
}

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.prometheus.metrics.exporter.opentelemetry;
22

33
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
4+
import java.io.IOException;
5+
import java.io.InputStream;
46
import java.util.Properties;
57

68
class PrometheusInstrumentationScope {
@@ -21,8 +23,15 @@ static InstrumentationScopeInfo loadInstrumentationScopeInfo(
2123
String path, String nameKey, String versionKey) {
2224
try {
2325
Properties properties = new Properties();
24-
properties.load(
25-
PrometheusInstrumentationScope.class.getClassLoader().getResourceAsStream(path));
26+
InputStream stream =
27+
PrometheusInstrumentationScope.class.getClassLoader().getResourceAsStream(path);
28+
if (stream == null) {
29+
throw new IllegalStateException(
30+
"Prometheus metrics library initialization error: Failed to read "
31+
+ path
32+
+ " from classpath.");
33+
}
34+
properties.load(stream);
2635
String instrumentationScopeName = properties.getProperty(nameKey);
2736
if (instrumentationScopeName == null) {
2837
throw new IllegalStateException(
@@ -44,7 +53,7 @@ static InstrumentationScopeInfo loadInstrumentationScopeInfo(
4453
return InstrumentationScopeInfo.builder(instrumentationScopeName)
4554
.setVersion(instrumentationScopeVersion)
4655
.build();
47-
} catch (Exception e) {
56+
} catch (IOException e) {
4857
throw new IllegalStateException(
4958
"Prometheus metrics library initialization error: Failed to read "
5059
+ path

prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void loadInstrumentationScopeInfo() {
2222
() ->
2323
PrometheusInstrumentationScope.loadInstrumentationScopeInfo(
2424
"instrumentationScope.properties", "name", "version"))
25-
.havingRootCause()
2625
.withMessage(
2726
"Prometheus metrics library initialization error: name not found in"
2827
+ " instrumentationScope.properties in classpath.");
@@ -32,7 +31,6 @@ void loadInstrumentationScopeInfo() {
3231
() ->
3332
PrometheusInstrumentationScope.loadInstrumentationScopeInfo(
3433
"instrumentationScope.properties", "instrumentationScope.name", "version"))
35-
.havingRootCause()
3634
.withMessage(
3735
"Prometheus metrics library initialization error: version not found in"
3836
+ " instrumentationScope.properties in classpath.");

prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public MetricSnapshots collect() {
226226
.labels(labels)
227227
.value(stats.evictionWeight())
228228
.build());
229-
} catch (Exception e) {
229+
} catch (UnsupportedOperationException e) {
230230
// EvictionWeight metric is unavailable, newer version of Caffeine is needed.
231231
}
232232

prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/internal/AbstractDropwizardExports.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected <K, V> void collectMetricKind(
195195
if (snapshot != null) {
196196
builder.metricSnapshot(snapshot);
197197
}
198-
} catch (Exception e) {
198+
} catch (RuntimeException e) {
199199
if (!invalidMetricHandler.suppressException(metricName, e)) {
200200
throw e;
201201
}

prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ private String vmNativeMemorySummaryInBytesOrEmpty() {
163163
} else {
164164
return summary;
165165
}
166-
} catch (Exception ex) {
167-
// ignore errors
166+
} catch (RuntimeException ex) {
167+
// ignore errors (native memory tracking not enabled or other runtime failures)
168168
isEnabled.set(false);
169169
return "";
170170
}

0 commit comments

Comments
 (0)