Skip to content

Commit 77a5259

Browse files
committed
Merge pull request #14291 from izeye:metrics-endpoint-metric
* pr/14291: Use InvalidEndpointRequestException for MetricsEndpoint
2 parents 72ebfb0 + 3eef927 commit 77a5259

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
import io.micrometer.core.instrument.Tag;
3535
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
3636

37+
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
3738
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
3839
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
3940
import org.springframework.boot.actuate.endpoint.annotation.Selector;
4041
import org.springframework.lang.Nullable;
41-
import org.springframework.util.Assert;
4242

4343
/**
4444
* An {@link Endpoint} for exposing the metrics held by a {@link MeterRegistry}.
@@ -80,8 +80,6 @@ private String getName(Meter meter) {
8080
@ReadOperation
8181
public MetricResponse metric(@Selector String requiredMetricName,
8282
@Nullable List<String> tag) {
83-
Assert.isTrue(tag == null || tag.stream().allMatch((t) -> t.contains(":")),
84-
"Each tag parameter must be in the form key:value");
8583
List<Tag> tags = parseTags(tag);
8684
List<Meter> meters = new ArrayList<>();
8785
collectMeters(meters, this.registry, requiredMetricName, tags);
@@ -106,6 +104,11 @@ private List<Tag> parseTags(List<String> tags) {
106104

107105
private Tag parseTag(String tag) {
108106
String[] parts = tag.split(":", 2);
107+
if (parts.length != 2) {
108+
throw new InvalidEndpointRequestException(
109+
"Each tag parameter must be in the form 'key:value' but was: " + tag,
110+
"Each tag parameter must be in the form 'key:value'");
111+
}
109112
return Tag.of(parts[0], parts[1]);
110113
}
111114

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
2828
import io.micrometer.core.instrument.simple.SimpleConfig;
2929
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
30+
import org.junit.Rule;
3031
import org.junit.Test;
32+
import org.junit.rules.ExpectedException;
33+
34+
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
3135

3236
import static org.assertj.core.api.Assertions.assertThat;
3337

@@ -39,6 +43,9 @@
3943
*/
4044
public class MetricsEndpointTests {
4145

46+
@Rule
47+
public ExpectedException thrown = ExpectedException.none();
48+
4249
private final MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT,
4350
new MockClock());
4451

@@ -109,6 +116,12 @@ public void metricWithSpaceInTagValue() {
109116
assertThat(getCount(response)).hasValue(2.0);
110117
}
111118

119+
@Test
120+
public void metricWithInvalidTag() {
121+
this.thrown.expect(InvalidEndpointRequestException.class);
122+
this.endpoint.metric("counter", Collections.singletonList("key"));
123+
}
124+
112125
@Test
113126
public void metricPresentInOneRegistryOfACompositeAndNotAnother() {
114127
CompositeMeterRegistry composite = new CompositeMeterRegistry();

0 commit comments

Comments
 (0)