diff --git a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/mpmetrics/MetricRegistryAdapter.java b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/mpmetrics/MetricRegistryAdapter.java index be354011e3b89..4bffa61ee7830 100644 --- a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/mpmetrics/MetricRegistryAdapter.java +++ b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/mpmetrics/MetricRegistryAdapter.java @@ -90,13 +90,13 @@ public Counter counter(Metadata metadata, Tag... tags) { Counter interceptorCounter(Metadata metadata, String... tags) { return internalCounter(internalGetMetadata(metadata, MetricType.COUNTER), - new MetricDescriptor(metadata.getName(), tags)); + new MetricDescriptor(metadata.getName(), scopeTags(tags))); } Counter injectedCounter(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalCounter( internalGetMetadata(annotation.name(), MetricType.COUNTER).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } CounterAdapter internalCounter(MpMetadata metadata, MetricDescriptor id) { @@ -138,13 +138,13 @@ public ConcurrentGauge concurrentGauge(Metadata metadata, Tag... tags) { ConcurrentGaugeImpl interceptorConcurrentGauge(Metadata metadata, String... tags) { return internalConcurrentGauge(internalGetMetadata(metadata, MetricType.CONCURRENT_GAUGE), - new MetricDescriptor(metadata.getName(), tags)); + new MetricDescriptor(metadata.getName(), scopeTags(tags))); } ConcurrentGaugeImpl injectedConcurrentGauge(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalConcurrentGauge( internalGetMetadata(annotation.name(), MetricType.CONCURRENT_GAUGE).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } ConcurrentGaugeImpl internalConcurrentGauge(MpMetadata metadata, MetricDescriptor id) { @@ -276,7 +276,7 @@ public Histogram histogram(Metadata metadata, Tag... tags) { HistogramAdapter injectedHistogram(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalHistogram( internalGetMetadata(annotation.name(), MetricType.HISTOGRAM).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } HistogramAdapter internalHistogram(MpMetadata metadata, MetricDescriptor id) { @@ -319,7 +319,7 @@ public Meter meter(Metadata metadata, Tag... tags) { MeterAdapter injectedMeter(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalMeter( internalGetMetadata(annotation.name(), MetricType.METERED).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } MeterAdapter internalMeter(MpMetadata metadata, MetricDescriptor id) { @@ -363,12 +363,12 @@ public Timer timer(Metadata metadata, Tag... tags) { TimerAdapter injectedTimer(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalTimer( internalGetMetadata(annotation.name(), MetricType.TIMER).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } TimerAdapter interceptorTimer(Metadata metadata, String... tags) { return internalTimer(internalGetMetadata(metadata, MetricType.TIMER), - new MetricDescriptor(metadata.getName(), tags)); + new MetricDescriptor(metadata.getName(), scopeTags(tags))); } TimerAdapter internalTimer(MpMetadata metadata, MetricDescriptor id) { @@ -465,7 +465,7 @@ public Metadata getMetadata(String name) { TimerAdapter injectedSimpleTimer(org.eclipse.microprofile.metrics.annotation.Metric annotation) { return internalSimpleTimer( internalGetMetadata(annotation.name(), MetricType.SIMPLE_TIMER).merge(annotation), - new MetricDescriptor(annotation.name(), annotation.tags())); + new MetricDescriptor(annotation.name(), scopeTags(annotation.tags()))); } TimerAdapter internalSimpleTimer(MpMetadata metadata, MetricDescriptor id) { @@ -657,14 +657,23 @@ public Type getType() { return null; } + Tags scopeTags() { + return Tags.of("scope", this.type.getName()); + } + Tags scopeTags(Tag... tags) { - Tags out = Tags.of("scope", this.type.getName()); + Tags out = scopeTags(); for (Tag t : tags) { out = out.and(t.getTagName(), t.getTagValue()); } return out; } + Tags scopeTags(String... tags) { + Tags in = Tags.of(tags); + return scopeTags().and(in); + } + private MpMetadata internalGetMetadata(String name, MetricType type) { MpMetadata result = metadataMap.computeIfAbsent(name, k -> new MpMetadata(name, type)); if (result.type != type) { diff --git a/integration-tests/micrometer-mp-metrics/src/main/java/io/quarkus/it/micrometer/mpmetrics/MessageResource.java b/integration-tests/micrometer-mp-metrics/src/main/java/io/quarkus/it/micrometer/mpmetrics/MessageResource.java index 2105c7ef33831..6ce3276f3f970 100644 --- a/integration-tests/micrometer-mp-metrics/src/main/java/io/quarkus/it/micrometer/mpmetrics/MessageResource.java +++ b/integration-tests/micrometer-mp-metrics/src/main/java/io/quarkus/it/micrometer/mpmetrics/MessageResource.java @@ -1,34 +1,60 @@ package io.quarkus.it.micrometer.mpmetrics; +import java.util.Collection; +import java.util.Objects; + import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.PathParam; +import org.eclipse.microprofile.metrics.Counter; +import org.eclipse.microprofile.metrics.annotation.Metric; + +import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.search.Search; @Path("/message") public class MessageResource { private final MeterRegistry registry; + private final Counter first; + private final Counter second; - public MessageResource(MeterRegistry registry) { + public MessageResource(MeterRegistry registry, + @Metric(name = "first-counter") final Counter first, + @Metric(name = "second-counter") final Counter second) { this.registry = registry; + this.first = Objects.requireNonNull(first); + this.second = Objects.requireNonNull(second); } @GET public String message() { + first.inc(); + second.inc(); return registry.getClass().getName(); } @GET @Path("fail") public String fail() { + first.inc(); throw new NullPointerException("Failed on purpose"); } @GET @Path("item/{id}") public String item(@PathParam("id") String id) { + second.inc(); return "return message with id " + id; } + + @GET + @Path("mpmetrics") + public String metrics() { + Collection meters = Search.in(registry).name(s -> s.contains("mpmetrics")).meters(); + meters.addAll(Search.in(registry).name(s -> s.endsWith("-counter")).meters()); + return meters.stream().allMatch(x -> x.getId().getTag("scope") != null) ? "OK" : "FAIL"; + } } diff --git a/integration-tests/micrometer-mp-metrics/src/test/java/io/quarkus/it/micrometer/mpmetrics/MPMetricsTest.java b/integration-tests/micrometer-mp-metrics/src/test/java/io/quarkus/it/micrometer/mpmetrics/MPMetricsTest.java index 7d47a0f85cba2..d7c2e9fc02344 100644 --- a/integration-tests/micrometer-mp-metrics/src/test/java/io/quarkus/it/micrometer/mpmetrics/MPMetricsTest.java +++ b/integration-tests/micrometer-mp-metrics/src/test/java/io/quarkus/it/micrometer/mpmetrics/MPMetricsTest.java @@ -70,7 +70,10 @@ void validateMetricsOutput_1() { "io_quarkus_it_micrometer_mpmetrics_PrimeResource_highestPrimeNumberSoFar2{scope=\"application\"} 887.0")) // the counter associated with a timed method should have been removed - .body(not(containsString("io_quarkus_it_micrometer_mpmetrics_PrimeResource_checkPrime"))); + .body(not(containsString("io_quarkus_it_micrometer_mpmetrics_PrimeResource_checkPrime"))) + + // no calls to /message + .body(not(containsString("/message"))); } @Test @@ -83,7 +86,7 @@ void callPrimeGen_4() { } @Test - @Order(8) + @Order(6) void callMessage() { given() .when().get("/message") @@ -91,6 +94,24 @@ void callMessage() { .statusCode(200); } + @Test + @Order(7) + void callMessageFail() { + given() + .when().get("/message/fail") + .then() + .statusCode(500); + } + + @Test + @Order(8) + void callMessageId() { + given() + .when().get("/message/item/35") + .then() + .statusCode(200); + } + @Test @Order(9) void validateMetricsOutput_2() { @@ -106,7 +127,10 @@ void validateMetricsOutput_2() { "highestPrimeNumberSoFar 887.0")) .body(containsString( "io_quarkus_it_micrometer_mpmetrics_InjectedInstance_notPrime_total{scope=\"application\"}")) - .body(not(containsString("/message"))); + .body(containsString( + "first_counter_total{scope=\"application\"}")) + .body(containsString( + "second_counter_total{scope=\"application\"}")); } @Test @@ -123,4 +147,13 @@ void validateJsonOutput() { Matchers.equalTo(887.0f)); } + @Test + @Order(11) + void meters() { + given() + .when().get("/message/mpmetrics") + .then() + .statusCode(200) + .log().body(); + } }