Class TracedHttpClient

java.lang.Object
codes.grow.packages.skywalking.tracing.TracedHttpClient

public final class TracedHttpClient extends Object
Wraps a HttpClient: every request runs in a client span and carries the propagation headers. The span records method, redacted URL, server address/port and the response status (4xx/5xx = error).

 TracedHttpClient http = TracedHttpClient.wrap(HttpClient.newHttpClient());
 HttpResponse<String> res = http.send(request, HttpResponse.BodyHandlers.ofString());
 
  • Method Details

    • wrap

      public static TracedHttpClient wrap(HttpClient delegate)
      Wraps a client using the helpers' tracer and propagators.
      Parameters:
      delegate - client
      Returns:
      traced client
    • wrap

      public static TracedHttpClient wrap(HttpClient delegate, io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.ContextPropagators propagators)
      Wraps a client with an explicit tracer and propagators.
      Parameters:
      delegate - client
      tracer - tracer (null for the default)
      propagators - propagators (null for the default)
      Returns:
      traced client
    • delegate

      public HttpClient delegate()
      The wrapped client.
      Returns:
      client
    • send

      public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> handler) throws IOException, InterruptedException
      Sends synchronously in a client span.
      Type Parameters:
      T - body type
      Parameters:
      request - request
      handler - body handler
      Returns:
      response
      Throws:
      IOException - on I/O errors
      InterruptedException - when interrupted
    • sendAsync

      public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> handler)
      Sends asynchronously in a client span that ends when the response arrives.
      Type Parameters:
      T - body type
      Parameters:
      request - request
      handler - body handler
      Returns:
      future response