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

prevent unmatched urls to pollute metrics #626

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

import java.util.Optional;

import static io.micronaut.configuration.metrics.binder.web.WebMetricsPublisher.URI_NO_ROUTE_MATCH;
import static io.micronaut.core.util.StringUtils.FALSE;
import static io.micronaut.http.HttpAttributes.URI_TEMPLATE;

/**
* Registers the timers and meters for each request.
*
* <p>The default is to intercept all paths /**, but using the
* property micronaut.metrics.http.path, this can be changed.</p>
* property micronaut.metrics.http.path, this can be changed.</p>
*
* @author Christian Oestreich
* @author graemerocher
Expand All @@ -58,7 +59,7 @@ public ServerRequestMeterRegistryFilter(MeterRegistry meterRegistry) {

private String resolvePath(HttpRequest<?> request) {
Optional<String> route = request.getAttribute(URI_TEMPLATE, String.class);
return route.orElseGet(request::getPath);
return route.orElse(URI_NO_ROUTE_MATCH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class WebMetricsPublisher<T extends HttpResponse<?>> extends Flux<T> {

private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");
private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
static final String URI_NO_ROUTE_MATCH = "NO_ROUTE_MATCH";
private static final String UNKNOWN = "UNKNOWN";
private static final String METHOD = "method";
private static final String STATUS = "status";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.micronaut.configuration.metrics

import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.PathVariable
import io.micronaut.http.annotation.Status
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@Property(name = "spec.name", value = "RoutePathVariableNotFoundMetricsSpec")
@MicronautTest
class NotAcceptableRouteWithPathVariableDoesNotPolluteMetricsSpec extends Specification {

@Inject
@Client("/")
HttpClient httpClient

void 'sending multiple requests to a route with a path variable does not pollute the metrics uri'() {
given:
BlockingHttpClient client = httpClient.toBlocking()

when:
client.exchange(HttpRequest.GET("/books/1"))

then:
noExceptionThrown()

when:
client.exchange(HttpRequest.GET("/books/2"))

then:
noExceptionThrown()

when:
List<String> uris = fetchHttpServerRequestMetricsUris(client)

then:
noExceptionThrown()
uris == ['/books/{id}']

when:
client.exchange(HttpRequest.GET("/books/1").accept(MediaType.TEXT_PLAIN))

then:
HttpClientResponseException e = thrown()
HttpStatus.NOT_ACCEPTABLE == e.status

when:
client.exchange(HttpRequest.GET("/books/2").accept(MediaType.TEXT_PLAIN))

then:
e = thrown()
HttpStatus.NOT_ACCEPTABLE == e.status

when:
uris = fetchHttpServerRequestMetricsUris(client)

then:
noExceptionThrown()
['NO_ROUTE_MATCH', '/metrics/{name}','/books/{id}'] == uris
}

private static List<String> fetchHttpServerRequestMetricsUris(BlockingHttpClient client) {
fetchHttpServerRequestMetrics(client)["availableTags"].find { it.tag == "uri" }?.values
}

private static Map<String, Object> fetchHttpServerRequestMetrics(BlockingHttpClient client) {
client.retrieve(HttpRequest.GET("/metrics/http.server.requests"), Argument.mapOf(String, Object));
}

@Requires(property = "spec.name", value = "RoutePathVariableNotFoundMetricsSpec")
@Controller
static class BookController {
@Get("/books/{id}")
@Status(HttpStatus.OK)
void index(@PathVariable String id) {
}
}
}