Search in sources :

Example 1 with Clock

use of io.micrometer.core.instrument.Clock in project micrometer by micrometer-metrics.

the class MetricsClientHttpRequestInterceptor method intercept.

@Override
public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
    final String urlTemplate = urlTemplateHolder.get();
    urlTemplateHolder.remove();
    final Clock clock = meterRegistry.config().clock();
    final long startTime = clock.monotonicTime();
    ListenableFuture<ClientHttpResponse> future;
    try {
        future = execution.executeAsync(request, body);
    } catch (IOException e) {
        getTimeBuilder(urlTemplate, request, null).register(meterRegistry).record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS);
        throw e;
    }
    future.addCallback(new ListenableFutureCallback<ClientHttpResponse>() {

        @Override
        public void onSuccess(final ClientHttpResponse response) {
            getTimeBuilder(urlTemplate, request, response).register(meterRegistry).record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS);
        }

        @Override
        public void onFailure(final Throwable ex) {
            getTimeBuilder(urlTemplate, request, null).register(meterRegistry).record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS);
        }
    });
    return future;
}
Also used : IOException(java.io.IOException) Clock(io.micrometer.core.instrument.Clock)

Example 2 with Clock

use of io.micrometer.core.instrument.Clock in project micrometer by micrometer-metrics.

the class MetricsClientHttpRequestInterceptor method intercept.

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    final String urlTemplate = urlTemplateHolder.get();
    urlTemplateHolder.remove();
    final Clock clock = meterRegistry.config().clock();
    final long startTime = clock.monotonicTime();
    ClientHttpResponse response = null;
    try {
        response = execution.execute(request, body);
        return response;
    } finally {
        getTimeBuilder(urlTemplate, request, response).register(this.meterRegistry).record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS);
    }
}
Also used : Clock(io.micrometer.core.instrument.Clock)

Aggregations

Clock (io.micrometer.core.instrument.Clock)2 IOException (java.io.IOException)1