io.opentelemetry.javaagent.instrumentation.vertx.client.ExceptionHandlerWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-vertx-http-client-common Show documentation
Show all versions of opentelemetry-javaagent-vertx-http-client-common Show documentation
Instrumentation of Java libraries using OpenTelemetry.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.vertx.client;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
public class ExceptionHandlerWrapper implements Handler {
private final Instrumenter instrumenter;
private final HttpClientRequest request;
private final VirtualField virtualField;
private final Handler handler;
private ExceptionHandlerWrapper(
Instrumenter instrumenter,
HttpClientRequest request,
VirtualField virtualField,
Handler handler) {
this.instrumenter = instrumenter;
this.request = request;
this.virtualField = virtualField;
this.handler = handler;
}
public static Handler wrap(
Instrumenter instrumenter,
HttpClientRequest request,
VirtualField virtualField,
Handler handler) {
if (handler instanceof ExceptionHandlerWrapper) {
return handler;
}
return new ExceptionHandlerWrapper(instrumenter, request, virtualField, handler);
}
@Override
public void handle(Throwable throwable) {
Contexts contexts = virtualField.get(request);
if (contexts == null) {
callHandler(throwable);
return;
}
instrumenter.end(contexts.context, request, null, throwable);
try (Scope ignored = contexts.parentContext.makeCurrent()) {
callHandler(throwable);
}
}
private void callHandler(Throwable throwable) {
handler.handle(throwable);
}
}