Name Date Size #Lines LOC

..--

src/H25-Apr-2025-841506

README.mdH A D25-Apr-20253.2 KiB10988

build.gradleH A D25-Apr-2025643 2518

README.md

1# OpenCensus JAX-RS
2[![Build Status][travis-image]][travis-url]
3[![Windows Build Status][appveyor-image]][appveyor-url]
4[![Maven Central][maven-image]][maven-url]
5
6The *OpenCensus JAX-RS for Java* is a container and client filter  for trace instrumentation when using JAX-RS for REST implementation in Java.
7
8## Quickstart
9
10### Add the dependencies to your project
11
12For Maven add to your `pom.xml`:
13```xml
14<dependencies>
15  <dependency>
16    <groupId>io.opencensus</groupId>
17    <artifactId>opencensus-api</artifactId>
18    <version>0.28.3</version>
19  </dependency>
20  <dependency>
21    <groupId>io.opencensus</groupId>
22    <artifactId>opencensus-contrib-http-jaxrs</artifactId>
23    <version>0.28.3</version>
24  </dependency>
25</dependencies>
26```
27
28For Gradle add to your dependencies:
29```groovy
30compile 'io.opencensus:opencensus-api:0.28.3'
31compile 'io.opencensus:opencensus-contrib-http-jaxrs:0.28.3'
32```
33
34### Usage
35
36#### Container Filter
37
38The container filter should be added to the JAX-RS `Application` class and endpoints should be annotated
39with `@Metrics` annotation.
40
41```java
42class MyApplication extends Application {
43  @Override
44  public Set<Class<?>> getClasses() {
45      Set<Class<?>> providers = new HashSet<>(super.getClasses());
46      providers.add(JaxrsContainerFilter.class);
47      return providers;
48  }
49}
50```
51
52It is possible to customize the filter by using the custom constructor. The below will
53use the `B3Format` for context propagation instead of the W3C text context format.
54
55```java
56class MyApplication extends Application {
57  @Override
58  public Set<Object> getSingletons() {
59    Set<Object> singletons = new HashSet<>(super.getSingletons());
60    singletons.add(new JaxrsContainerFilter(
61        new JaxrsContainerExtractor(),
62        Tracing.getPropagationComponent().getB3Format(),
63        /* publicEndpoint= */ true));
64    return singletons;
65  }
66}
67```
68
69```java
70@Metrics
71@Path("/resource")
72class MyResource {
73  @GET
74  public Response resource() {
75    ...
76  }
77}
78```
79
80The annotation may also be applied on method level.
81
82#### Client Filter
83
84Filter should be added to the `WebTarget` instance when using JAX-RS as client.
85
86```java
87WebTarget target = ClientBuilder.newClient().target("endpoint");
88target.register(JaxrsClientFilter.class);
89```
90
91It is possible to customize the filter using the custom constructor. The
92below will use the `B3Format` for context propagation instead of the default W3C
93text context format.
94
95```java
96WebTarget target = ClientBuilder.newClient().target("endpoint");
97target.register(new JaxrsClientFilter(
98    new JaxrsContainerExtractor(),
99    Tracing.getPropagationComponent().getB3Format()));
100```
101
102
103[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master
104[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java
105[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true
106[appveyor-url]: https://ci.appveyor.com/project/opencensusjavateam/opencensus-java/branch/master
107[maven-image]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-contrib-http-jetty-client/badge.svg
108[maven-url]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-contrib-jetty-client
109