Add path mdc filter.

This commit is contained in:
Stephan Schnabel 2021-12-13 14:12:39 +01:00
parent 7403b04efd
commit ce4b75c941
8 changed files with 228 additions and 8 deletions

View file

@ -21,6 +21,7 @@ import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.client.DefaultHttpClientConfiguration;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.filter.HttpServerFilter;
@ -79,9 +80,9 @@ public abstract class AbstractFilterTest extends AbstractTest {
// request
@SneakyThrows
public TestResponse get(Map<String, String> headers) {
public TestResponse get(String path, Map<String, String> headers) {
var request = HttpRequest.GET("/");
var request = HttpRequest.GET(path);
headers.forEach((name, value) -> request.header(name, value));
var configuration = new DefaultHttpClientConfiguration();
configuration.setLoggerName("io.kokuwa.TestClient");
@ -100,8 +101,8 @@ public abstract class AbstractFilterTest extends AbstractTest {
@Slf4j
public static class TestController {
@Get("/")
TestResponse run() {
@Get("/{+path}")
TestResponse run(@PathVariable String path) {
var level = Level.OFF;
if (log.isTraceEnabled()) {
@ -119,7 +120,7 @@ public abstract class AbstractFilterTest extends AbstractTest {
var mdc = MDC.getCopyOfContextMap();
log.info("Found MDC: {}", mdc);
return new TestResponse(level.toString(), mdc == null ? Map.of() : mdc);
return new TestResponse(path, level.toString(), mdc == null ? Map.of() : mdc);
}
}
@ -127,6 +128,7 @@ public abstract class AbstractFilterTest extends AbstractTest {
@NoArgsConstructor
@AllArgsConstructor
public static class TestResponse {
private String path;
private String level;
private Map<String, String> context = Map.of();
}

View file

@ -75,6 +75,6 @@ public class LogLevelServerFilterTest extends AbstractFilterTest {
private void assertLevel(Level expectedLevel, String name, String value) {
var headers = value == null ? Map.<String, String>of() : Map.of(name, value);
assertEquals(expectedLevel.toString(), get(headers).getLevel());
assertEquals(expectedLevel.toString(), get("/level", headers).getLevel());
}
}

View file

@ -63,7 +63,7 @@ public class AuthenticationMdcFilterTest extends AbstractFilterTest {
}
private Map<String, String> getContext(boolean token) {
return get(token
return get("/security", token
? Map.of(HttpHeaders.AUTHORIZATION, token("mySubject", claims -> claims
.issuer("nope")
.claim("azp", "myAzp")

View file

@ -55,6 +55,6 @@ public class HeaderMdcFilterTest extends AbstractFilterTest {
}
private void assertContext(Map<String, String> expectedMdcs, Map<String, String> headers) {
assertEquals(expectedMdcs, get(headers).getContext());
assertEquals(expectedMdcs, get("/header", headers).getContext());
}
}

View file

@ -0,0 +1,104 @@
package io.kokuwa.micronaut.logging.http.mdc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import io.kokuwa.micronaut.logging.http.AbstractFilterTest;
import io.micronaut.context.annotation.Property;
/**
* Test for {@link PathMdcFilter}.
*
* @author Stephan Schnabel
*/
@DisplayName("http: mdc from path")
public class PathMdcFilterTest extends AbstractFilterTest {
@DisplayName("noop: empty configuration")
@Test
void noopEmptyConfiguration() {
assertContext(Map.of(), "/foo/bar");
}
@DisplayName("noop: disabled")
@Test
@Property(name = "logger.http.path.enabled", value = "false")
@Property(name = "logger.http.path.patterns", value = "\\/foo\\/(?<foo>[0-9]+)")
void noopDisabled() {
assertContext(Map.of(), "/foo/123");
}
@DisplayName("noop: misconfigured")
@Test
@Property(name = "logger.http.path.patterns", value = "\\A{")
void noopMisconfigured() {
assertContext(Map.of(), "/foo/123");
}
@DisplayName("noop: no group")
@Test
@Property(name = "logger.http.path.patterns", value = "\\/foo/[0-9]+")
void noopGroups() {
assertContext(Map.of(), "/foo/123");
}
@DisplayName("mdc: mismatch")
@Test
@Property(name = "logger.http.path.patterns", value = "\\/foo\\/(?<foo>[0-9]+)")
void mdcMismatch() {
assertContext(Map.of(), "/nope");
assertContext(Map.of(), "/foo/abc");
}
@DisplayName("mdc: match with single group")
@Test
@Property(name = "logger.http.path.patterns", value = "\\/foo\\/(?<foo>[0-9]+)")
void mdcMatchWithSingleGroup() {
assertContext(Map.of("foo", "123"), "/foo/123");
}
@DisplayName("mdc: match with single group and prefix")
@Test
@Property(name = "logger.http.path.names", value = "foo")
@Property(name = "logger.http.path.patterns", value = "\\/foo\\/(?<foo>[0-9]+)")
@Property(name = "logger.http.path.prefix", value = "path.")
void mdcMatchWithSingleGroupAndPrefix() {
assertContext(Map.of("path.foo", "123"), "/foo/123");
}
@DisplayName("mdc: match with single group and misconfigured")
@Test
@Property(name = "logger.http.path.names", value = "foo")
@Property(name = "logger.http.path.patterns", value = "\\/foo\\/(?<foo>[0-9]+),\\A{")
@Property(name = "logger.http.path.prefix", value = "path.")
void mdcMatchWithSingleGroupAndMisconfigured() {
assertContext(Map.of("path.foo", "123"), "/foo/123");
}
@DisplayName("mdc: match with multiple group")
@Test
@Property(name = "logger.http.path.patterns", value = "/foo/(?<foo>[0-9]+)/bar/(?<bar>[0-9]+)")
void mdcMatchWithmultipleGroup() {
assertContext(Map.of("foo", "123", "bar", "456"), "/foo/123/bar/456");
}
@DisplayName("mdc: test for documentation example")
@Test
@Property(name = "logger.http.path.patterns", value = ""
+ "\\/gateway\\/(?<gatewayId>[a-f0-9\\-]{36}),"
+ "\\/gateway\\/(?<gatewayId>[a-f0-9\\-]{36})\\/configuration\\/(?<config>[a-z]+)")
void mdcMatchExample() {
var uuid = UUID.randomUUID().toString();
assertContext(Map.of("gatewayId", uuid), "/gateway/" + uuid);
assertContext(Map.of("gatewayId", uuid, "config", "abc"), "/gateway/" + uuid + "/configuration/abc");
}
private void assertContext(Map<String, String> expectedMdcs, String path) {
assertEquals(expectedMdcs, get(path, Map.of()).getContext());
}
}