Skip to content

Commit

Permalink
Merge pull request #37638 from ebullient/mp-scope
Browse files Browse the repository at this point in the history
Add scope tag to all injected MP Metrics
  • Loading branch information
gsmet authored Dec 11, 2023
2 parents 3c1d1a0 + c7f9229 commit 788f9ac
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Meter> 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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -83,14 +86,32 @@ void callPrimeGen_4() {
}

@Test
@Order(8)
@Order(6)
void callMessage() {
given()
.when().get("/message")
.then()
.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() {
Expand All @@ -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
Expand All @@ -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();
}
}

0 comments on commit 788f9ac

Please sign in to comment.