Search in sources :

Example 1 with OcJettyHttpClient

use of io.opencensus.contrib.http.jetty.client.OcJettyHttpClient in project instrumentation-java by census-instrumentation.

the class HelloWorldClient method main.

/**
 * HelloWorldClient sends http request periodically to {@link HelloWorldServer}. These requests
 * are instrumented using opencensus jetty client library to enable tracing and monitoring stats.
 */
public static void main(String[] args) throws Exception {
    BasicConfigurator.configure();
    initTracing();
    initStatsExporter();
    // Create http client that will trace requests. By default trace context is propagated using
    // w3c TraceContext propagator.
    // To use B3 propagation use following
    // OcJettyHttpClient httpClient =
    // new OcJettyHttpClient(
    // new HttpClientTransportOverHTTP(),
    // new SslContextFactory(),
    // null,
    // Tracing.getPropagationComponent().getB3Format());
    OcJettyHttpClient httpClient = new OcJettyHttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(), null, null);
    httpClient.start();
    do {
        HttpRequest request = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request").method(HttpMethod.GET);
        HttpRequest asyncRequest = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request/async").method(HttpMethod.GET);
        HttpRequest postRequest = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request").method(HttpMethod.POST);
        postRequest.content(new StringContentProvider("{\"hello\": \"world\"}"), "application/json");
        if (request == null) {
            logger.info("Request is null");
            break;
        }
        request.send();
        asyncRequest.send();
        postRequest.send();
        try {
            Thread.sleep(15000);
        } catch (Exception e) {
            logger.error("Error while sleeping");
        }
    } while (true);
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) OcJettyHttpClient(io.opencensus.contrib.http.jetty.client.OcJettyHttpClient) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) IOException(java.io.IOException)

Aggregations

OcJettyHttpClient (io.opencensus.contrib.http.jetty.client.OcJettyHttpClient)1 IOException (java.io.IOException)1 HttpRequest (org.eclipse.jetty.client.HttpRequest)1 HttpClientTransportOverHTTP (org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)1 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)1 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)1