Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -68,23 +68,13 @@ public final class AsyncIoByteCounterExec implements AsyncExecChainHandler {
private final ObservingOptions opts;
private final MetricConfig mc;

private final Counter.Builder reqBuilder;
private final Counter.Builder respBuilder;

public AsyncIoByteCounterExec(final MeterRegistry meterRegistry,
final ObservingOptions opts,
final MetricConfig mc) {
this.meterRegistry = Args.notNull(meterRegistry, "meterRegistry");
this.opts = Args.notNull(opts, "observingOptions");
this.mc = Args.notNull(mc, "metricConfig");

this.reqBuilder = Counter.builder(mc.prefix + ".request.bytes")
.description("HTTP request payload size")
.baseUnit("bytes");

this.respBuilder = Counter.builder(mc.prefix + ".response.bytes")
.description("HTTP response payload size")
.baseUnit("bytes");
// builders are created per request to avoid tag accumulation
}

@Override
Expand Down Expand Up @@ -146,11 +136,23 @@ private void record() {
final List<Tag> tags = buildTags(request.getMethod(), status, protocol, target, uri);

if (reqBytes >= 0) {
reqBuilder.tags(tags).tags(mc.commonTags).register(meterRegistry).increment(reqBytes);
Counter.builder(mc.prefix + ".request.bytes")
.description("HTTP request payload size")
.baseUnit("bytes")
.tags(tags)
.tags(mc.commonTags)
.register(meterRegistry)
.increment(reqBytes);
}
final long rb = respLen.get();
if (rb >= 0) {
respBuilder.tags(tags).tags(mc.commonTags).register(meterRegistry).increment(rb);
Counter.builder(mc.prefix + ".response.bytes")
.description("HTTP response payload size")
.baseUnit("bytes")
.tags(tags)
.tags(mc.commonTags)
.register(meterRegistry)
.increment(rb);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
package org.apache.hc.client5.http.observation.interceptors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
Expand Down Expand Up @@ -109,4 +114,56 @@ void countsAsyncRequestAndResponseBytes() throws Exception {
assertNotNull(meters.find(mc.prefix + ".response.bytes").counter());
assertTrue(meters.find(mc.prefix + ".response.bytes").counter().count() > 0.0);
}

@Test
void doesNotCarryTagsOverToLaterRequests() throws Exception {
server = ServerBootstrap.bootstrap()
.setListenerPort(0)
.register("localhost", "/a", (request, response, context) -> {
response.setCode(HttpStatus.SC_OK);
response.setEntity(new StringEntity("AAAA", ContentType.TEXT_PLAIN));
})
.register("localhost", "/b", (request, response, context) -> {
response.setCode(HttpStatus.SC_NOT_FOUND);
response.setEntity(new StringEntity("BB", ContentType.TEXT_PLAIN));
})
.create();
server.start();
final int port = server.getLocalPort();

final MeterRegistry meters = new SimpleMeterRegistry();
final MetricConfig mc = MetricConfig.builder().prefix("test").build();
// adds a tag for /a only
final ObservingOptions opts = ObservingOptions.builder()
.metrics(EnumSet.of(ObservingOptions.MetricSet.IO))
.tagCustomizer((tags, method, status, protocol, target, uri) -> {
final List<Tag> out = new ArrayList<>(tags);
if (uri.startsWith("/a")) {
out.add(Tag.of("only_for_a", "yes"));
}
return out;
})
.build();

final HttpAsyncClientBuilder b = HttpAsyncClients.custom();
b.addExecInterceptorFirst("io", new AsyncIoByteCounterExec(meters, opts, mc));

try (final CloseableHttpAsyncClient client = b.build()) {
client.start();
client.execute(SimpleRequestBuilder.get("http://localhost:" + port + "/a").build(), null)
.get(20, TimeUnit.SECONDS);
client.execute(SimpleRequestBuilder.get("http://localhost:" + port + "/b").build(), null)
.get(20, TimeUnit.SECONDS);
} finally {
server.stop();
}

final Counter a = meters.find(mc.prefix + ".response.bytes").tag("status", "200").counter();
final Counter b404 = meters.find(mc.prefix + ".response.bytes").tag("status", "404").counter();
assertNotNull(a);
assertNotNull(b404);
assertEquals("yes", a.getId().getTag("only_for_a"));
assertFalse(b404.getId().getTags().stream().anyMatch(t -> t.getKey().equals("only_for_a")),
"tags of /b: " + b404.getId().getTags());
}
}
Loading