Skip to content

Commit 91338f0

Browse files
committed
fix formating
1 parent e891dc3 commit 91338f0

File tree

6 files changed

+55
-27
lines changed

6 files changed

+55
-27
lines changed

httpclient5-observation/src/main/java/org/apache/hc/client5/http/observation/MetricConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public Builder addCommonTag(final String k, final String v) {
122122
}
123123

124124
public Builder addCommonTags(final Iterable<Tag> tags) {
125-
for (final Tag t : tags) this.commonTags.add(t);
125+
for (final Tag t : tags) {
126+
this.commonTags.add(t);
127+
}
126128
return this;
127129
}
128130

httpclient5-observation/src/main/java/org/apache/hc/client5/http/observation/impl/ObservationClassicExecInterceptor.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.hc.core5.http.ClassicHttpRequest;
3838
import org.apache.hc.core5.http.ClassicHttpResponse;
3939
import org.apache.hc.core5.http.HttpException;
40+
import org.apache.hc.core5.util.Args;
4041

4142
/**
4243
* Classic (blocking) execution interceptor that emits Micrometer {@link Observation}s
@@ -55,8 +56,8 @@ public final class ObservationClassicExecInterceptor implements ExecChainHandler
5556

5657
public ObservationClassicExecInterceptor(final ObservationRegistry registry,
5758
final ObservingOptions opts) {
58-
this.registry = registry;
59-
this.opts = opts;
59+
this.registry = Args.notNull(registry, "observationRegistry");
60+
this.opts = opts != null ? opts : ObservingOptions.DEFAULT;
6061
}
6162

6263
@Override
@@ -69,17 +70,16 @@ public ClassicHttpResponse execute(final ClassicHttpRequest request,
6970
return chain.proceed(request, scope);
7071
}
7172

72-
final Observation obs;
73-
try {
74-
obs = Observation
75-
.createNotStarted("http.client.request", registry)
76-
.contextualName(request.getMethod() + " " + request.getUri())
77-
.lowCardinalityKeyValue("http.method", request.getMethod())
78-
.lowCardinalityKeyValue("net.peer.name", request.getAuthority().getHostName())
79-
.start();
80-
} catch (final URISyntaxException e) {
81-
throw new RuntimeException(e);
82-
}
73+
final String method = request.getMethod();
74+
final String uriForName = safeUriForName(request);
75+
final String peer = request.getAuthority().getHostName();
76+
77+
final Observation obs = Observation
78+
.createNotStarted("http.client.request", registry)
79+
.contextualName(method + " " + uriForName)
80+
.lowCardinalityKeyValue("http.method", method)
81+
.lowCardinalityKeyValue("net.peer.name", peer)
82+
.start();
8383

8484
ClassicHttpResponse response = null;
8585
Throwable error = null;
@@ -93,10 +93,22 @@ public ClassicHttpResponse execute(final ClassicHttpRequest request,
9393
if (response != null) {
9494
obs.lowCardinalityKeyValue("http.status_code", Integer.toString(response.getCode()));
9595
}
96+
if (opts.tagLevel == ObservingOptions.TagLevel.EXTENDED) {
97+
obs.lowCardinalityKeyValue("http.scheme", scope.route.getTargetHost().getSchemeName())
98+
.lowCardinalityKeyValue("net.peer.name", scope.route.getTargetHost().getHostName());
99+
}
96100
if (error != null) {
97101
obs.error(error);
98102
}
99103
obs.stop();
100104
}
101105
}
106+
107+
private static String safeUriForName(final ClassicHttpRequest req) {
108+
try {
109+
return req.getUri().toString();
110+
} catch (final URISyntaxException e) {
111+
return req.getRequestUri();
112+
}
113+
}
102114
}

httpclient5-observation/src/main/java/org/apache/hc/client5/http/observation/interceptors/AsyncTimerExec.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ public AsyncTimerExec(final MeterRegistry reg, final ObservingOptions opts, fina
7575
this.opts = Args.notNull(opts, "options");
7676
this.mc = mc != null ? mc : MetricConfig.builder().build();
7777

78-
// Guard: only register the inflight gauge once.
79-
if (registry.find(this.mc.prefix + ".inflight").gauge() == null) {
78+
// Tag-aware guard: only register once per (name + tags)
79+
if (registry.find(this.mc.prefix + ".inflight")
80+
.tags("kind", "async")
81+
.tags(this.mc.commonTags)
82+
.gauge() == null) {
8083
Gauge.builder(this.mc.prefix + ".inflight", inflight, AtomicInteger::doubleValue)
8184
.tag("kind", "async")
8285
.tags(this.mc.commonTags)

httpclient5-observation/src/main/java/org/apache/hc/client5/http/observation/interceptors/TimerExec.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,37 @@ public final class TimerExec implements ExecChainHandler {
7373

7474
private final AtomicInteger inflight = new AtomicInteger(0);
7575

76-
/** Back-compat: two-arg ctor. */
76+
/**
77+
* Back-compat: two-arg ctor.
78+
*/
7779
public TimerExec(final MeterRegistry reg, final ObservingOptions cfg) {
7880
this(reg, cfg, null);
7981
}
8082

81-
/** Preferred: honors {@link MetricConfig}. */
83+
/**
84+
* Preferred: honors {@link MetricConfig}.
85+
*/
8286
public TimerExec(final MeterRegistry reg, final ObservingOptions cfg, final MetricConfig mc) {
8387
this.registry = Args.notNull(reg, "registry");
8488
this.cfg = Args.notNull(cfg, "config");
8589
this.mc = mc != null ? mc : MetricConfig.builder().build();
8690

8791
final String base = this.mc.prefix + ".";
88-
this.timerBuilder = Timer.builder(base + "request")
89-
.tags(this.mc.commonTags);
92+
this.timerBuilder = Timer.builder(base + "request").tags(this.mc.commonTags);
9093
if (this.mc.percentiles != null && this.mc.percentiles.length > 0) {
9194
this.timerBuilder.publishPercentiles(this.mc.percentiles);
9295
}
9396
if (this.mc.slo != null) {
9497
this.timerBuilder.serviceLevelObjectives(this.mc.slo);
9598
}
9699

97-
this.counterBuilder = Counter.builder(base + "response")
98-
.tags(this.mc.commonTags);
100+
this.counterBuilder = Counter.builder(base + "response").tags(this.mc.commonTags);
99101

100-
// Guard: only register the inflight gauge once.
101-
if (registry.find(this.mc.prefix + ".inflight").gauge() == null) {
102+
// Tag-aware guard: only register once per (name + tags)
103+
if (registry.find(this.mc.prefix + ".inflight")
104+
.tags("kind", "classic")
105+
.tags(this.mc.commonTags)
106+
.gauge() == null) {
102107
Gauge.builder(this.mc.prefix + ".inflight", inflight, AtomicInteger::doubleValue)
103108
.tag("kind", "classic")
104109
.tags(this.mc.commonTags)

httpclient5-observation/src/test/java/org/apache/hc/client5/http/observation/impl/MeteredTlsStrategyTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public void upgrade(final TransportSecurityLayer sessionLayer,
107107
final Timeout handshakeTimeout,
108108
final FutureCallback<TransportSecurityLayer> callback) {
109109
// immediately complete
110-
if (callback != null) callback.completed(sessionLayer);
110+
if (callback != null) {
111+
callback.completed(sessionLayer);
112+
}
111113
}
112114

113115
@Deprecated
@@ -129,7 +131,9 @@ public void upgrade(final TransportSecurityLayer sessionLayer,
129131
final Object attachment,
130132
final Timeout handshakeTimeout,
131133
final FutureCallback<TransportSecurityLayer> callback) {
132-
if (callback != null) callback.failed(new RuntimeException("boom"));
134+
if (callback != null) {
135+
callback.failed(new RuntimeException("boom"));
136+
}
133137
}
134138

135139
@Deprecated

httpclient5-observation/src/test/java/org/apache/hc/client5/http/observation/impl/ObservationAsyncExecInterceptorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public void onStop(final Observation.Context c) {
7979

8080
@AfterEach
8181
void tearDown() {
82-
if (server != null) server.close(CloseMode.GRACEFUL);
82+
if (server != null) {
83+
server.close(CloseMode.GRACEFUL);
84+
}
8385
}
8486

8587
@Test

0 commit comments

Comments
 (0)