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

Commit c0f3a4c

Browse files
authored
Update readme with an example for Jetty Http Client instrumentation. (#1671)
1 parent 30aa312 commit c0f3a4c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

contrib/http_jetty_client/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,55 @@ compile 'io.opencensus:opencensus-api:0.19.0'
3131
compile 'io.opencensus:opencensus-contrib-http-jetty-client:0.19.0'
3232
```
3333

34+
## Instrumenting Jetty Http Client
35+
36+
Users can instrument Jetty Http Client by simply creating HttpClient using wrapper OcJettyHttpClient.
37+
38+
Below is an example of how it is instrumented.
39+
40+
```java
41+
42+
import io.opencensus.contrib.http.jetty.client.OcJettyHttpClient;
43+
import io.opencensus.contrib.http.util.HttpViews;
44+
import io.opencensus.exporter.stats.prometheus.PrometheusStatsCollector;
45+
import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
46+
47+
48+
class Application {
49+
private static void initStatsExporter() throws IOException {
50+
51+
HttpViews.registerAllClientViews();
52+
53+
// Register stats exporter for desired backend.
54+
PrometheusStatsCollector.createAndRegister();
55+
HTTPServer prometheusServer = new HTTPServer(9091, true);
56+
}
57+
58+
public static void main(String[] args) throws Exception {
59+
BasicConfigurator.configure();
60+
61+
// Register trace exporter for desired backend.
62+
LoggingTraceExporter.register();
63+
64+
// Initialize Stats Exporter
65+
initStatsExporter();
66+
67+
OcJettyHttpClient httpClient = new OcJettyHttpClient();
68+
69+
httpClient.start();
70+
71+
HttpRequest request =
72+
(HttpRequest) httpClient.newRequest("http://example.com/").method(HttpMethod.GET);
73+
request.send();
74+
75+
HttpRequest postRequest =
76+
(HttpRequest) httpClient.newRequest("http://example.com/").method(HttpMethod.POST);
77+
postRequest.content(new StringContentProvider("{\"hello\": \"world\"}"), "application/json");
78+
postRequest.send();
79+
}
80+
}
81+
```
82+
3483
[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master
3584
[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java
3685
[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true

0 commit comments

Comments
 (0)