From 28e86a5f832e3c517ba858e6b9f5b34407e7e13e Mon Sep 17 00:00:00 2001 From: Dennys Fredericci Date: Wed, 22 Jul 2020 22:55:50 +0200 Subject: [PATCH] Add "host" tag to JettyClientMetrics.(#2054) --- .../instrument/binder/jetty/JettyClientTags.java | 13 +++++++++++++ .../binder/jetty/JettyClientTagsProvider.java | 4 +++- .../binder/jetty/JettyClientMetricsTest.java | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTags.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTags.java index 8273c0ead8..559a8d7f5d 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTags.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTags.java @@ -45,6 +45,8 @@ public final class JettyClientTags { private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN"); + private static final Tag HOST_UNKNOWN = Tag.of("host", "UNKNOWN"); + private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$"); private static final Pattern MULTIPLE_SLASH_PATTERN = Pattern.compile("//+"); @@ -63,6 +65,17 @@ public static Tag method(Request request) { return (request != null) ? Tag.of("method", request.getMethod()) : METHOD_UNKNOWN; } + /** + * Creates a {@code host} tag based on the {@link Request#getHost() + * method} of the given {@code request}. + * + * @param request the request + * @return the host tag derived from request result. + */ + public static Tag host(Request request) { + return (request != null) ? Tag.of("host", request.getHost()) : HOST_UNKNOWN; + } + /** * Creates a {@code status} tag based on the status of the given {@code result}. * diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTagsProvider.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTagsProvider.java index c8141b12bb..3c14cbf87d 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTagsProvider.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTagsProvider.java @@ -37,7 +37,9 @@ public interface JettyClientTagsProvider { * @return tags to associate with metrics recorded for the request */ default Iterable httpRequestTags(Result result) { - return Tags.of(JettyClientTags.method(result.getRequest()), JettyClientTags.uri(result, this::uriPattern), + return Tags.of(JettyClientTags.method(result.getRequest()), + JettyClientTags.host(result.getRequest()), + JettyClientTags.uri(result, this::uriPattern), JettyClientTags.exception(result), JettyClientTags.status(result), JettyClientTags.outcome(result)); } diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyClientMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyClientMetricsTest.java index d2b87538a7..36a03876b6 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyClientMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyClientMetricsTest.java @@ -99,6 +99,7 @@ void successfulRequest() throws Exception { .tag("outcome", "SUCCESS") .tag("status", "200") .tag("uri", "/ok") + .tag("host", "localhost") .timer().count()).isEqualTo(1); } @@ -114,6 +115,7 @@ void requestSize() throws Exception { .tag("outcome", "SUCCESS") .tag("status", "200") .tag("uri", "/ok") + .tag("host", "localhost") .summary().totalAmount()).isEqualTo("123456".length()); } @@ -129,6 +131,7 @@ void serverError() throws Exception { .tag("outcome", "SERVER_ERROR") .tag("status", "500") .tag("uri", "/error") + .tag("host", "localhost") .timer().count()).isEqualTo(1); } @@ -144,6 +147,7 @@ void handlerWrapperUncheckedException() throws Exception { .tag("outcome", "SERVER_ERROR") .tag("status", "500") .tag("uri", "/errorUnchecked") + .tag("host", "localhost") .timer().count()).isEqualTo(1); } @@ -159,6 +163,7 @@ void notFound() throws Exception { .tag("outcome", "CLIENT_ERROR") .tag("status", "404") .tag("uri", "NOT_FOUND") + .tag("host", "localhost") .timer().count()).isEqualTo(1); } }