diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java index 3e0322309..df99837cb 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java @@ -112,7 +112,7 @@ private void sendErrorResponseWithStackTrace(Exception requestHandlerException) httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8"); httpExchange.sendResponseHeaders(500, stackTrace.length); httpExchange.getResponseBody().write(stackTrace); - } catch (Exception errorWriterException) { + } catch (IOException errorWriterException) { // We want to avoid logging so that we don't mess with application logs when the HTTPServer // is used in a Java agent. // However, if we can't even send an error response to the client there's nothing we can do diff --git a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java index 4a16ee07e..e0c6a0fa9 100644 --- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java +++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java @@ -112,7 +112,7 @@ static Resource getResourceField(AutoConfiguredOpenTelemetrySdk sdk) { Method method = AutoConfiguredOpenTelemetrySdk.class.getDeclaredMethod("getResource"); method.setAccessible(true); return (Resource) method.invoke(sdk); - } catch (Exception e) { + } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } diff --git a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java index 5f02d38cc..91241295a 100644 --- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java +++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java @@ -1,6 +1,8 @@ package io.prometheus.metrics.exporter.opentelemetry; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import java.io.IOException; +import java.io.InputStream; import java.util.Properties; class PrometheusInstrumentationScope { @@ -21,8 +23,15 @@ static InstrumentationScopeInfo loadInstrumentationScopeInfo( String path, String nameKey, String versionKey) { try { Properties properties = new Properties(); - properties.load( - PrometheusInstrumentationScope.class.getClassLoader().getResourceAsStream(path)); + InputStream stream = + PrometheusInstrumentationScope.class.getClassLoader().getResourceAsStream(path); + if (stream == null) { + throw new IllegalStateException( + "Prometheus metrics library initialization error: Failed to read " + + path + + " from classpath."); + } + properties.load(stream); String instrumentationScopeName = properties.getProperty(nameKey); if (instrumentationScopeName == null) { throw new IllegalStateException( @@ -44,7 +53,7 @@ static InstrumentationScopeInfo loadInstrumentationScopeInfo( return InstrumentationScopeInfo.builder(instrumentationScopeName) .setVersion(instrumentationScopeVersion) .build(); - } catch (Exception e) { + } catch (IOException e) { throw new IllegalStateException( "Prometheus metrics library initialization error: Failed to read " + path diff --git a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java index c50354b24..948dfc498 100644 --- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java +++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java @@ -22,7 +22,6 @@ void loadInstrumentationScopeInfo() { () -> PrometheusInstrumentationScope.loadInstrumentationScopeInfo( "instrumentationScope.properties", "name", "version")) - .havingRootCause() .withMessage( "Prometheus metrics library initialization error: name not found in" + " instrumentationScope.properties in classpath."); @@ -32,7 +31,6 @@ void loadInstrumentationScopeInfo() { () -> PrometheusInstrumentationScope.loadInstrumentationScopeInfo( "instrumentationScope.properties", "instrumentationScope.name", "version")) - .havingRootCause() .withMessage( "Prometheus metrics library initialization error: version not found in" + " instrumentationScope.properties in classpath."); diff --git a/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java b/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java index a1b8782a3..c847e13eb 100644 --- a/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java +++ b/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java @@ -226,7 +226,7 @@ public MetricSnapshots collect() { .labels(labels) .value(stats.evictionWeight()) .build()); - } catch (Exception e) { + } catch (UnsupportedOperationException e) { // EvictionWeight metric is unavailable, newer version of Caffeine is needed. } diff --git a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/internal/AbstractDropwizardExports.java b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/internal/AbstractDropwizardExports.java index 279459b02..df9ea8ffa 100644 --- a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/internal/AbstractDropwizardExports.java +++ b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/internal/AbstractDropwizardExports.java @@ -195,7 +195,7 @@ protected void collectMetricKind( if (snapshot != null) { builder.metricSnapshot(snapshot); } - } catch (Exception e) { + } catch (RuntimeException e) { if (!invalidMetricHandler.suppressException(metricName, e)) { throw e; } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java index f2ae98abf..6b11df38a 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java @@ -163,8 +163,8 @@ private String vmNativeMemorySummaryInBytesOrEmpty() { } else { return summary; } - } catch (Exception ex) { - // ignore errors + } catch (RuntimeException ex) { + // ignore errors (native memory tracking not enabled or other runtime failures) isEnabled.set(false); return ""; }