Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use InvalidEndpointRequestException for MetricsEndpoint.metric() #14291

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,11 +34,11 @@
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

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

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

Expand Down
Expand Up @@ -27,7 +27,11 @@
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;

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

Expand All @@ -39,6 +43,9 @@
*/
public class MetricsEndpointTests {

@Rule
public ExpectedException thrown = ExpectedException.none();

private final MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT,
new MockClock());

Expand Down Expand Up @@ -109,6 +116,12 @@ public void metricWithSpaceInTagValue() {
assertThat(getCount(response)).hasValue(2.0);
}

@Test
public void metricWithInvalidTag() {
this.thrown.expect(InvalidEndpointRequestException.class);
this.endpoint.metric("counter", Collections.singletonList("key"));
}

@Test
public void metricPresentInOneRegistryOfACompositeAndNotAnother() {
CompositeMeterRegistry composite = new CompositeMeterRegistry();
Expand Down