Skip to content

Commit 74f4a41

Browse files
authored
Polish JettyClientMetrics changes (#2036)
1 parent 570333e commit 74f4a41

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientMetrics.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import io.micrometer.core.annotation.Incubating;
1919
import io.micrometer.core.instrument.DistributionSummary;
2020
import io.micrometer.core.instrument.MeterRegistry;
21+
import io.micrometer.core.instrument.Tag;
2122
import io.micrometer.core.instrument.Timer;
2223
import io.micrometer.core.instrument.config.MeterFilter;
24+
import io.micrometer.core.instrument.internal.OnlyOnceLoggingDenyMeterFilter;
2325
import org.eclipse.jetty.client.api.Request;
2426

2527
/**
@@ -43,7 +45,15 @@ protected JettyClientMetrics(MeterRegistry registry, JettyClientTagsProvider tag
4345
this.timingMetricName = timingMetricName;
4446
this.contentSizeMetricName = contentSizeMetricName;
4547

46-
registry.config().meterFilter(MeterFilter.maximumAllowableTags(this.timingMetricName, "uri", maxUriTags, MeterFilter.deny()));
48+
MeterFilter timingMetricDenyFilter = new OnlyOnceLoggingDenyMeterFilter(
49+
() -> String.format("Reached the maximum number of URI tags for '%s'.", timingMetricName));
50+
MeterFilter contentSizeMetricDenyFilter = new OnlyOnceLoggingDenyMeterFilter(
51+
() -> String.format("Reached the maximum number of URI tags for '%s'.", contentSizeMetricName));
52+
registry.config()
53+
.meterFilter(MeterFilter.maximumAllowableTags(
54+
this.timingMetricName, "uri", maxUriTags, timingMetricDenyFilter))
55+
.meterFilter(MeterFilter.maximumAllowableTags(
56+
this.contentSizeMetricName, "uri", maxUriTags, contentSizeMetricDenyFilter));
4757
}
4858

4959
@Override
@@ -52,17 +62,18 @@ public void onQueued(Request request) {
5262

5363
request.onComplete(result -> {
5464
long requestLength = result.getRequest().getContent().getLength();
65+
Iterable<Tag> httpRequestTags = tagsProvider.httpRequestTags(result);
5566
if (requestLength >= 0) {
5667
DistributionSummary.builder(contentSizeMetricName)
5768
.description("Content sizes for Jetty HTTP client requests")
58-
.tags(tagsProvider.httpRequestTags(result))
69+
.tags(httpRequestTags)
5970
.register(registry)
6071
.record(requestLength);
6172
}
6273

6374
sample.stop(Timer.builder(timingMetricName)
6475
.description("Jetty HTTP client request timing")
65-
.tags(tagsProvider.httpRequestTags(result))
76+
.tags(httpRequestTags)
6677
.register(registry));
6778
});
6879
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyClientTags.java

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.micrometer.core.instrument.binder.jetty;
1717

1818
import io.micrometer.core.instrument.Tag;
19+
import io.micrometer.core.instrument.binder.http.Outcome;
1920
import io.micrometer.core.instrument.util.StringUtils;
2021
import org.eclipse.jetty.client.api.Request;
2122
import org.eclipse.jetty.client.api.Response;
@@ -42,20 +43,6 @@ public final class JettyClientTags {
4243

4344
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
4445

45-
private static final Tag STATUS_SERVER_ERROR = Tag.of("status", String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR_500));
46-
47-
private static final Tag OUTCOME_UNKNOWN = Tag.of("outcome", "UNKNOWN");
48-
49-
private static final Tag OUTCOME_INFORMATIONAL = Tag.of("outcome", "INFORMATIONAL");
50-
51-
private static final Tag OUTCOME_SUCCESS = Tag.of("outcome", "SUCCESS");
52-
53-
private static final Tag OUTCOME_REDIRECTION = Tag.of("outcome", "REDIRECTION");
54-
55-
private static final Tag OUTCOME_CLIENT_ERROR = Tag.of("outcome", "CLIENT_ERROR");
56-
57-
private static final Tag OUTCOME_SERVER_ERROR = Tag.of("outcome", "SERVER_ERROR");
58-
5946
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
6047

6148
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
@@ -69,7 +56,7 @@ private JettyClientTags() {
6956
* Creates a {@code method} tag based on the {@link Request#getMethod()
7057
* method} of the given {@code request}.
7158
*
72-
* @param request the container request
59+
* @param request the request
7360
* @return the method tag whose value is a capitalized method (e.g. GET).
7461
*/
7562
public static Tag method(Request request) {
@@ -107,20 +94,16 @@ public static Tag uri(Result result, Function<Result, String> successfulUriPatte
10794
}
10895

10996
String matchingPattern = successfulUriPattern.apply(result);
97+
matchingPattern = MULTIPLE_SLASH_PATTERN.matcher(matchingPattern).replaceAll("/");
11098
if (matchingPattern.equals("/")) {
11199
return URI_ROOT;
112100
}
113-
114-
matchingPattern = MULTIPLE_SLASH_PATTERN.matcher(matchingPattern).replaceAll("/");
115-
if (!matchingPattern.equals("/")) {
116-
matchingPattern = TRAILING_SLASH_PATTERN.matcher(matchingPattern).replaceAll("");
117-
}
118-
101+
matchingPattern = TRAILING_SLASH_PATTERN.matcher(matchingPattern).replaceAll("");
119102
return Tag.of("uri", matchingPattern);
120103
}
121104

122105
/**
123-
* Creates a {@code exception} tag based on the {@link Class#getSimpleName() simple
106+
* Creates an {@code exception} tag based on the {@link Class#getSimpleName() simple
124107
* name} of the class of the given {@code exception}.
125108
*
126109
* @param result the request result
@@ -152,19 +135,7 @@ public static Tag exception(Result result) {
152135
* @return the outcome tag derived from the status of the response
153136
*/
154137
public static Tag outcome(Result result) {
155-
int status = result.getResponse().getStatus();
156-
if (HttpStatus.isInformational(status)) {
157-
return OUTCOME_INFORMATIONAL;
158-
} else if (HttpStatus.isSuccess(status)) {
159-
return OUTCOME_SUCCESS;
160-
} else if (HttpStatus.isRedirection(status)) {
161-
return OUTCOME_REDIRECTION;
162-
} else if (HttpStatus.isClientError(status)) {
163-
return OUTCOME_CLIENT_ERROR;
164-
} else if (HttpStatus.isServerError(status)) {
165-
return OUTCOME_SERVER_ERROR;
166-
}
167-
return OUTCOME_UNKNOWN;
138+
return Outcome.forStatus(result.getResponse().getStatus()).asTag();
168139
}
169140

170141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.micrometer.core.instrument.internal;
18+
19+
import io.micrometer.core.instrument.Meter.Id;
20+
import io.micrometer.core.instrument.config.MeterFilter;
21+
import io.micrometer.core.instrument.config.MeterFilterReply;
22+
import io.micrometer.core.util.internal.logging.InternalLogger;
23+
import io.micrometer.core.util.internal.logging.InternalLoggerFactory;
24+
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
import java.util.function.Supplier;
27+
28+
/**
29+
* {@link MeterFilter} to log only once a warning message and deny a {@link Id Meter.Id}.
30+
*
31+
* @author Jon Schneider
32+
* @author Dmytro Nosan
33+
* @since 1.5.0
34+
*/
35+
public final class OnlyOnceLoggingDenyMeterFilter implements MeterFilter {
36+
37+
private static final InternalLogger logger = InternalLoggerFactory.getInstance(OnlyOnceLoggingDenyMeterFilter.class);
38+
39+
private final AtomicBoolean alreadyWarned = new AtomicBoolean(false);
40+
41+
private final Supplier<String> message;
42+
43+
public OnlyOnceLoggingDenyMeterFilter(Supplier<String> message) {
44+
this.message = message;
45+
}
46+
47+
@Override
48+
public MeterFilterReply accept(Id id) {
49+
if (logger.isWarnEnabled() && this.alreadyWarned.compareAndSet(false, true)) {
50+
logger.warn(this.message.get());
51+
}
52+
return MeterFilterReply.DENY;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)