All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.javaagent.instrumentation.vertx.client.ExceptionHandlerWrapper Maven / Gradle / Ivy

The newest version!
/*
 * 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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy