content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.vertx.v5_0.client;
  
  import io.opentelemetry.context.Context;
  import io.opentelemetry.context.Scope;
  import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
  import io.opentelemetry.instrumentation.api.util.VirtualField;
  import io.opentelemetry.javaagent.instrumentation.vertx.client.VertxClientInstrumenterFactory;
  import io.vertx.core.Future;
  import io.vertx.core.http.HttpClientRequest;
  import io.vertx.core.http.HttpClientResponse;
  import io.vertx.core.net.HostAndPort;
  import java.util.concurrent.CompletableFuture;
  
  public final class VertxClientSingletons {
  
    private static final Instrumenter<HttpClientRequest, HttpClientResponse> INSTRUMENTER =
        VertxClientInstrumenterFactory.create(
            "io.opentelemetry.vertx-http-client-5.0", new Vertx5HttpAttributesGetter());
  
    public static Instrumenter<HttpClientRequest, HttpClientResponse> instrumenter() {
      return INSTRUMENTER;
    }
  
    private static final VirtualField<HttpClientRequest, HostAndPort> authorityField =
        VirtualField.find(HttpClientRequest.class, HostAndPort.class);
  
    public static void setAuthority(HttpClientRequest request, HostAndPort authority) {
      authorityField.set(request, authority);
    }
  
    public static HostAndPort getAuthority(HttpClientRequest request) {
      return authorityField.get(request);
    }
  
    public static <T> Future<T> wrapFuture(Future<T> future) {
      Context context = Context.current();
      CompletableFuture<T> result = new CompletableFuture<>();
      future
          .toCompletionStage()
          .whenComplete(
              (value, throwable) -> {
                try (Scope ignore = context.makeCurrent()) {
                  if (throwable != null) {
                    result.completeExceptionally(throwable);
                  } else {
                    result.complete(value);
                  }
                }
              });
      return Future.fromCompletionStage(result);
    }
  
    private VertxClientSingletons() {}
  }
  