/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.example.http;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

public final class HttpClient {

  // it's important to initialize the OpenTelemetry SDK as early in your applications lifecycle as
  // possible.
  private static final OpenTelemetry openTelemetry = ExampleConfiguration.initOpenTelemetry();

  private static final Tracer tracer =
      openTelemetry.getTracer("io.opentelemetry.example.http.HttpClient");
  private static final TextMapPropagator textMapPropagator =
      openTelemetry.getPropagators().getTextMapPropagator();

  // Export traces to log
  // Inject the span context into the request
  private static final TextMapSetter<HttpURLConnection> setter = URLConnection::setRequestProperty;

  private void makeRequest() throws IOException {
    int port = 8080;
    URL url = new URL("http://127.0.0.1:" + port);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    int status = 0;
    StringBuilder content = new StringBuilder();

    // Name convention for the Span is not yet defined.
    // See: https://github.com/open-telemetry/opentelemetry-specification/issues/270
    Span span = tracer.spanBuilder("/").setSpanKind(SpanKind.CLIENT).startSpan();
    try (Scope scope = span.makeCurrent()) {
      span.setAttribute(SemanticAttributes.HTTP_METHOD, "GET");
      span.setAttribute("component", "http");
      /*
       Only one of the following is required:
         - http.url
         - http.scheme, http.host, http.target
         - http.scheme, peer.hostname, peer.port, http.target
         - http.scheme, peer.ip, peer.port, http.target
      */
      span.setAttribute(SemanticAttributes.HTTP_URL, url.toString());

      // Inject the request with the current Context/Span.
      textMapPropagator.inject(Context.current(), con, setter);

      try {
        // Process the request
        con.setRequestMethod("GET");
        status = con.getResponseCode();
        BufferedReader in =
            new BufferedReader(
                new InputStreamReader(con.getInputStream(), Charset.defaultCharset()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
          content.append(inputLine);
        }
        in.close();
      } catch (Exception e) {
        span.setStatus(StatusCode.ERROR, "HTTP Code: " + status);
      }
    } finally {
      span.end();
    }

    // Output the result of the request
    System.out.println("Response Code: " + status);
    System.out.println("Response Msg: " + content);
  }

  /**
   * Main method to run the example.
   *
   * @param args It is not required.
   */
  public static void main(String[] args) {
    HttpClient httpClient = new HttpClient();

    // Perform request every 5s
    Thread t =
        new Thread(
            () -> {
              while (true) {
                try {
                  httpClient.makeRequest();
                  Thread.sleep(5000);
                } catch (Exception e) {
                  System.out.println(e.getMessage());
                }
              }
            });
    t.start();
  }
}

@@ -725,32 +725,89 @@
 pURLConnection;%0A
+import java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 import java.net.
@@ -811,16 +811,16 @@
 et.URL;%0A
-
 import j
@@ -1576,16 +1576,36 @@
 xception
+, URISyntaxException
  %7B%0A    i
@@ -2243,17 +2243,16 @@
 required
-:
 %0A       
@@ -2425,24 +2425,24 @@
 http.target%0A
-
       */%0A   
@@ -2438,16 +2438,330 @@
      */%0A
+      URI uri = url.toURI();%0A      url =%0A          new URI(%0A                  uri.getScheme(),%0A                  null,%0A                  uri.getHost(),%0A                  uri.getPort(),%0A                  uri.getPath(),%0A                  uri.getQuery(),%0A                  uri.getFragment())%0A              .toURL();%0A%0A
       sp

Remove credential info from url in example (#3121)