Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 1a08c36

Browse files
authored
update jetty client and server example to use updated api. (#1847)
* modify jetty server example to use config option. * update jetty client and server example to use updated api. - use new client api https. - use default value for publicEndpoint on server. - Add comment on how to use B3 format on both sides, client and server. * fix typo and fmt. * fix typo.
1 parent 4475dd9 commit 1a08c36

2 files changed

Lines changed: 58 additions & 16 deletions

File tree

examples/src/main/java/io/opencensus/examples/http/jetty/client/HelloWorldClient.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
import org.apache.log4j.Level;
3131
import org.apache.log4j.Logger;
3232
import org.eclipse.jetty.client.HttpRequest;
33+
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
3334
import org.eclipse.jetty.client.util.StringContentProvider;
3435
import org.eclipse.jetty.http.HttpMethod;
36+
import org.eclipse.jetty.util.ssl.SslContextFactory;
3537

3638
/** Sample application that shows how to instrument jetty client. */
3739
public class HelloWorldClient {
40+
3841
private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
3942

4043
private static void initTracing() {
@@ -68,17 +71,37 @@ public static void main(String[] args) throws Exception {
6871
initTracing();
6972
initStatsExporter();
7073

71-
OcJettyHttpClient httpClient = new OcJettyHttpClient();
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);
7286

7387
httpClient.start();
7488

7589
do {
7690
HttpRequest request =
77-
(HttpRequest) httpClient.newRequest("http://localhost:8080/").method(HttpMethod.GET);
91+
(HttpRequest)
92+
httpClient
93+
.newRequest("http://localhost:8080/helloworld/request")
94+
.method(HttpMethod.GET);
7895
HttpRequest asyncRequest =
79-
(HttpRequest) httpClient.newRequest("http://localhost:8080/async").method(HttpMethod.GET);
96+
(HttpRequest)
97+
httpClient
98+
.newRequest("http://localhost:8080/helloworld/request/async")
99+
.method(HttpMethod.GET);
80100
HttpRequest postRequest =
81-
(HttpRequest) httpClient.newRequest("http://localhost:8080/").method(HttpMethod.POST);
101+
(HttpRequest)
102+
httpClient
103+
.newRequest("http://localhost:8080/helloworld/request")
104+
.method(HttpMethod.POST);
82105
postRequest.content(new StringContentProvider("{\"hello\": \"world\"}"), "application/json");
83106

84107
if (request == null) {

examples/src/main/java/io/opencensus/examples/http/jetty/server/HelloWorldServer.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
import org.eclipse.jetty.server.Server;
4545
import org.eclipse.jetty.server.handler.AbstractHandler;
4646
import org.eclipse.jetty.servlet.ServletContextHandler;
47-
import org.eclipse.jetty.servlet.ServletHandler;
47+
import org.eclipse.jetty.servlet.ServletHolder;
4848

4949
/** Sample application that shows how to instrument jetty server. */
5050
public class HelloWorldServer extends AbstractHandler {
51+
5152
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());
5253

5354
public static class HelloServlet extends HttpServlet {
55+
5456
private static String body = "<h1>Hello World Servlet Get</h1>";
5557

5658
private static final long serialVersionUID = 1L;
@@ -179,18 +181,35 @@ private static void initTracing() {
179181
public static void main(String[] args) throws Exception {
180182
initTracing();
181183
initStatsExporter();
182-
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
183-
context.setContextPath("/");
184184

185185
Server server = new Server(8080);
186-
ServletHandler handler = new ServletHandler();
187-
server.setHandler(handler);
188-
189-
handler.addFilterWithMapping(
190-
OcHttpServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
191-
handler.addServletWithMapping(HelloServlet.class, "/*");
192-
193-
server.start();
194-
server.join();
186+
ServletContextHandler contextHandler =
187+
new ServletContextHandler(ServletContextHandler.SESSIONS);
188+
contextHandler.setContextPath("/helloworld");
189+
ServletHolder sh = new ServletHolder(new HelloServlet());
190+
contextHandler.addServlet(sh, "/request/*");
191+
192+
// Enable tracing by adding OcHttpServleFilter for all path
193+
contextHandler.addFilter(OcHttpServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
194+
195+
// Uncomment following line to use B3Format for trace context propagation.
196+
// contextHandler.setAttribute(
197+
// OC_TRACE_PROPAGATOR, Tracing.getPropagationComponent().getB3Format());
198+
199+
// By default publicEndpoint parameter is set to false and incoming trace context is added as
200+
// a parent.
201+
// If the endpoint for http request is public then uncomment following line to set the
202+
// publicEndpoint parameter to true. When set to true incoming trace context is added as a
203+
// parent link instead of as a parent.
204+
//
205+
// contextHandler.setInitParameter(OC_PUBLIC_ENDPOINT, "true");
206+
207+
server.setHandler(contextHandler);
208+
try {
209+
server.start();
210+
server.join();
211+
} catch (Exception e) {
212+
logger.error("Failed to start application", e);
213+
}
195214
}
196215
}

0 commit comments

Comments
 (0)