1 /* 2 * Copyright 2019, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.examples.http.jetty.client; 18 19 import io.opencensus.contrib.http.jetty.client.OcJettyHttpClient; 20 import io.opencensus.contrib.http.util.HttpViews; 21 import io.opencensus.exporter.stats.prometheus.PrometheusStatsCollector; 22 import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter; 23 import io.opencensus.exporter.trace.logging.LoggingTraceExporter; 24 import io.opencensus.trace.Tracing; 25 import io.opencensus.trace.config.TraceConfig; 26 import io.opencensus.trace.samplers.Samplers; 27 import io.prometheus.client.exporter.HTTPServer; 28 import java.io.IOException; 29 import org.apache.log4j.BasicConfigurator; 30 import org.apache.log4j.Level; 31 import org.apache.log4j.Logger; 32 import org.eclipse.jetty.client.HttpRequest; 33 import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP; 34 import org.eclipse.jetty.client.util.StringContentProvider; 35 import org.eclipse.jetty.http.HttpMethod; 36 import org.eclipse.jetty.util.ssl.SslContextFactory; 37 38 /** Sample application that shows how to instrument jetty client. */ 39 public class HelloWorldClient { 40 41 private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName()); 42 initTracing()43 private static void initTracing() { 44 TraceConfig traceConfig = Tracing.getTraceConfig(); 45 Logger.getRootLogger().setLevel(Level.INFO); 46 traceConfig.updateActiveTraceParams( 47 traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build()); 48 49 LoggingTraceExporter.register(); 50 // Register Jaeger Tracing. Refer to https://www.jaegertracing.io/docs/1.8/getting-started/ to 51 // run Jaeger 52 JaegerTraceExporter.createAndRegister("http://localhost:14268/api/traces", "helloworldclient"); 53 } 54 initStatsExporter()55 private static void initStatsExporter() throws IOException { 56 HttpViews.registerAllClientViews(); 57 58 // Register Prometheus exporters and export metrics to a Prometheus HTTPServer. 59 // Refer to https://prometheus.io/ to run Prometheus Server. 60 PrometheusStatsCollector.createAndRegister(); 61 HTTPServer prometheusServer = new HTTPServer(9091, true); 62 } 63 64 /** 65 * HelloWorldClient sends http request periodically to {@link HelloWorldServer}. These requests 66 * are instrumented using opencensus jetty client library to enable tracing and monitoring stats. 67 */ main(String[] args)68 public static void main(String[] args) throws Exception { 69 BasicConfigurator.configure(); 70 71 initTracing(); 72 initStatsExporter(); 73 74 // Create http client that will trace requests. By default trace context is propagated using 75 // w3c TraceContext propagator. 76 // To use B3 propagation use following 77 // OcJettyHttpClient httpClient = 78 // new OcJettyHttpClient( 79 // new HttpClientTransportOverHTTP(), 80 // new SslContextFactory(), 81 // null, 82 // Tracing.getPropagationComponent().getB3Format()); 83 OcJettyHttpClient httpClient = 84 new OcJettyHttpClient( 85 new HttpClientTransportOverHTTP(), new SslContextFactory(), null, null); 86 87 httpClient.start(); 88 89 do { 90 HttpRequest request = 91 (HttpRequest) 92 httpClient 93 .newRequest("http://localhost:8080/helloworld/request") 94 .method(HttpMethod.GET); 95 HttpRequest asyncRequest = 96 (HttpRequest) 97 httpClient 98 .newRequest("http://localhost:8080/helloworld/request/async") 99 .method(HttpMethod.GET); 100 HttpRequest postRequest = 101 (HttpRequest) 102 httpClient 103 .newRequest("http://localhost:8080/helloworld/request") 104 .method(HttpMethod.POST); 105 postRequest.content(new StringContentProvider("{\"hello\": \"world\"}"), "application/json"); 106 107 if (request == null) { 108 logger.info("Request is null"); 109 break; 110 } 111 112 request.send(); 113 asyncRequest.send(); 114 postRequest.send(); 115 try { 116 Thread.sleep(15000); 117 } catch (Exception e) { 118 logger.error("Error while sleeping"); 119 } 120 } while (true); 121 } 122 } 123