Repository: quarkusio/quarkus
Issue: Superfluous metrics are being created from SmallRye internal classes

Classes in `io.smallrye.metrics.interceptors` package are annotated in a way that when scanned as application classes, some metrics will be created out of them, but these are internal classes and are not meant to be scanned for metric annotations. This means we register some metrics unnecessarily, which hinders performance and makes the export contain superfluous metrics whose values will always be 0. An excerpt from such bloated OpenMetrics export:

```
# TYPE application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_total counter
application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_total 0.0
# TYPE application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_rate_per_second gauge
application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_rate_per_second 0.0
# TYPE application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_one_min_rate_per_second gauge
application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_one_min_rate_per_second 0.0
# TYPE application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_five_min_rate_per_second gauge
application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_five_min_rate_per_second 0.0
# TYPE application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_fifteen_min_rate_per_second gauge
application_io_smallrye_metrics_interceptors_MeteredInterceptor_meteredMethod_fifteen_min_rate_per_second 0.0
```

This happens for every application that has MP Metrics enabled.

Changed files:
extensions/smallrye-metrics/deployment/src/main/java/io/quarkus/smallrye/metrics/deployment/SmallRyeMetricsProcessor.java
integration-tests/main/src/test/java/io/quarkus/it/main/MetricsTestCase.java

diff --git a/extensions/smallrye-metrics/deployment/src/main/java/io/quarkus/smallrye/metrics/deployment/SmallRyeMetricsProcessor.java b/extensions/smallrye-metrics/deployment/src/main/java/io/quarkus/smallrye/metrics/deployment/SmallRyeMetricsProcessor.java
index 197cd1bffda..13af72d2877 100644
--- a/extensions/smallrye-metrics/deployment/src/main/java/io/quarkus/smallrye/metrics/deployment/SmallRyeMetricsProcessor.java
+++ b/extensions/smallrye-metrics/deployment/src/main/java/io/quarkus/smallrye/metrics/deployment/SmallRyeMetricsProcessor.java
@@ -167,12 +167,16 @@ void registerMetricsFromAnnotatedMethods(SmallRyeMetricsRecorder metrics,
                 switch (metricAnnotationTarget.kind()) {
                     case METHOD: {
                         MethodInfo method = metricAnnotationTarget.asMethod();
-                        collectedMetricsMethods.add(method);
+                        if (!method.declaringClass().name().toString().startsWith("io.smallrye.metrics")) {
+                            collectedMetricsMethods.add(method);
+                        }
                         break;
                     }
                     case CLASS: {
                         ClassInfo clazz = metricAnnotationTarget.asClass();
-                        collectMetricsClassAndSubClasses(index, collectedMetricsClasses, clazz);
+                        if (!clazz.name().toString().startsWith("io.smallrye.metrics")) {
+                            collectMetricsClassAndSubClasses(index, collectedMetricsClasses, clazz);
+                        }
                         break;
                     }
                 }
diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/MetricsTestCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/MetricsTestCase.java
index 4b617105e62..77c03160198 100644
--- a/integration-tests/main/src/test/java/io/quarkus/it/main/MetricsTestCase.java
+++ b/integration-tests/main/src/test/java/io/quarkus/it/main/MetricsTestCase.java
@@ -2,6 +2,7 @@
 
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
 
 import java.util.concurrent.TimeUnit;
 
@@ -149,6 +150,16 @@ public void testEndpointWithMetricsThrowingException() {
                 .statusCode(404);
     }
 
+    /**
+     * Verify that no metrics are created from SmallRye internal classes (for example the
+     * io.smallrye.metrics.interceptors package)
+     */
+    @Test
+    public void testNoMetricsFromSmallRyeInternalClasses() {
+        RestAssured.when().get("/metrics/application").then()
+                .body(not(containsString("io_smallrye_metrics")));
+    }
+
     private void assertMetricExactValue(String name, String val) {
         RestAssured.when().get("/metrics").then()
                 .body(containsString(name + " " + val));