1> **Warning** 2> 3> OpenCensus and OpenTracing have merged to form [OpenTelemetry](https://opentelemetry.io), which serves as the next major version of OpenCensus and OpenTracing. 4> 5> OpenTelemetry has now reached feature parity with OpenCensus, with tracing and metrics SDKs available in .NET, Golang, Java, NodeJS, and Python. **All OpenCensus Github repositories, except [census-instrumentation/opencensus-python](https://github.com/census-instrumentation/opencensus-python), will be archived on July 31st, 2023**. We encourage users to migrate to OpenTelemetry by this date. 6> 7> To help you gradually migrate your instrumentation to OpenTelemetry, bridges are available in Java, Go, Python, and JS. [**Read the full blog post to learn more**](https://opentelemetry.io/blog/2023/sunsetting-opencensus/). 8 9# OpenCensus - A stats collection and distributed tracing framework 10[![Gitter chat][gitter-image]][gitter-url] 11[![Maven Central][maven-image]][maven-url] 12[![Javadocs][javadoc-image]][javadoc-url] 13[![Build Status][travis-image]][travis-url] 14[![Windows Build Status][appveyor-image]][appveyor-url] 15[![Coverage Status][codecov-image]][codecov-url] 16 17> :exclamation: The [opencensus-contrib-log-correlation-log4j2](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/log_correlation/stackdriver) 18> Java client library is part of the OpenCensus project. 19> [CVE-2021-44228](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44228) 20> and [CVE-2021-45046](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-45046) disclosed 21> security vulnerabilities in the Apache Log4j 2 version 2.15 or below. The recent version 22> v0.28.3 depends on Log4j 2.11.1. A number of previous versions also depend on vulnerable 23> Log4j versions. 24> 25> :exclamation: We merged several fixes and published a release that depends on a safe version of 26> Log4j (2.16). **We strongly encourage customers who depend on the 27> opencensus-contrib-log-correlation-log4j2 library to upgrade to the latest 28> release [(v0.30.0)](https://repo1.maven.org/maven2/io/opencensus/opencensus-contrib-log-correlation-log4j2/0.30.0/).** 29 30OpenCensus is a toolkit for collecting application performance and behavior data. It currently 31includes 3 apis: stats, tracing and tags. 32 33The library is in [Beta](#versioning) stage and APIs are expected to be mostly stable. 34 35Please join [gitter](https://gitter.im/census-instrumentation/Lobby) for help or feedback on this 36project. 37 38**OpenCensus and OpenTracing have merged to form OpenTelemetry, which serves as the next major version of OpenCensus and OpenTracing. OpenTelemetry will offer backwards compatibility with existing OpenCensus integrations, and we will continue to make security patches to existing OpenCensus libraries for two years. Read more about the merger [here](https://medium.com/opentracing/a-roadmap-to-convergence-b074e5815289).** 39 40## OpenCensus Quickstart for Libraries 41 42Integrating OpenCensus with a new library means recording stats or traces and propagating context. 43For application integration please see [Quickstart for Applications](https://github.com/census-instrumentation/opencensus-java#quickstart-for-applications). 44 45The full quick start example can also be found on the [OpenCensus website](https://opencensus.io/quickstart/java/). 46 47### Add the dependencies to your project 48 49For Maven add to your `pom.xml`: 50```xml 51<dependencies> 52 <dependency> 53 <groupId>io.opencensus</groupId> 54 <artifactId>opencensus-api</artifactId> 55 <version>0.30.0</version> 56 </dependency> 57</dependencies> 58``` 59 60For Gradle add to your dependencies: 61```groovy 62compile 'io.opencensus:opencensus-api:0.30.0' 63``` 64 65### Hello "OpenCensus" trace events 66 67Here's an example of creating a Span and record some trace annotations. Notice that recording the 68annotations is possible because we propagate scope. 3rd parties libraries like SLF4J can integrate 69the same way. 70 71```java 72import io.opencensus.common.Scope; 73import io.opencensus.trace.Tracer; 74import io.opencensus.trace.Tracing; 75import io.opencensus.trace.samplers.Samplers; 76 77public final class MyClassWithTracing { 78 private static final Tracer tracer = Tracing.getTracer(); 79 80 public static void doWork() { 81 // Create a child Span of the current Span. Always record events for this span and force it to 82 // be sampled. This makes it easier to try out the example, but unless you have a clear use 83 // case, you don't need to explicitly set record events or sampler. 84 try (Scope ss = 85 tracer 86 .spanBuilder("MyChildWorkSpan") 87 .setRecordEvents(true) 88 .setSampler(Samplers.alwaysSample()) 89 .startScopedSpan()) { 90 doInitialWork(); 91 tracer.getCurrentSpan().addAnnotation("Finished initial work"); 92 doFinalWork(); 93 } 94 } 95 96 private static void doInitialWork() { 97 // ... 98 tracer.getCurrentSpan().addAnnotation("Important."); 99 // ... 100 } 101 102 private static void doFinalWork() { 103 // ... 104 tracer.getCurrentSpan().addAnnotation("More important."); 105 // ... 106 } 107} 108``` 109 110### Hello "OpenCensus" stats events 111 112Here's an example on 113 * defining TagKey, Measure and View, 114 * registering a view, 115 * putting TagKey and TagValue into a scoped TagContext, 116 * recording stats against current TagContext, 117 * getting ViewData. 118 119 120For the complete example, see 121[here](https://github.com/census-instrumentation/opencensus-java/blob/master/examples/src/main/java/io/opencensus/examples/helloworld/QuickStart.java). 122 123```java 124import io.opencensus.common.Scope; 125import io.opencensus.stats.Aggregation; 126import io.opencensus.stats.BucketBoundaries; 127import io.opencensus.stats.Measure.MeasureLong; 128import io.opencensus.stats.Stats; 129import io.opencensus.stats.StatsRecorder; 130import io.opencensus.stats.View; 131import io.opencensus.stats.ViewData; 132import io.opencensus.stats.ViewManager; 133import io.opencensus.tags.TagKey; 134import io.opencensus.tags.TagValue; 135import io.opencensus.tags.Tagger; 136import io.opencensus.tags.Tags; 137import java.util.Arrays; 138import java.util.Collections; 139 140public final class MyClassWithStats { 141 private static final Tagger tagger = Tags.getTagger(); 142 private static final ViewManager viewManager = Stats.getViewManager(); 143 private static final StatsRecorder statsRecorder = Stats.getStatsRecorder(); 144 145 // frontendKey allows us to break down the recorded data 146 private static final TagKey FRONTEND_KEY = TagKey.create("myorg_keys_frontend"); 147 148 // videoSize will measure the size of processed videos. 149 private static final MeasureLong VIDEO_SIZE = 150 MeasureLong.create("my.org/measure/video_size", "size of processed videos", "By"); 151 152 // Create view to see the processed video size distribution broken down by frontend. 153 // The view has bucket boundaries (0, 256, 65536) that will group measure values into 154 // histogram buckets. 155 private static final View.Name VIDEO_SIZE_VIEW_NAME = View.Name.create("my.org/views/video_size"); 156 private static final View VIDEO_SIZE_VIEW = 157 View.create( 158 VIDEO_SIZE_VIEW_NAME, 159 "processed video size over time", 160 VIDEO_SIZE, 161 Aggregation.Distribution.create( 162 BucketBoundaries.create(Arrays.asList(0.0, 256.0, 65536.0))), 163 Collections.singletonList(FRONTEND_KEY)); 164 165 public static void initialize() { 166 // ... 167 viewManager.registerView(VIDEO_SIZE_VIEW); 168 } 169 170 public static void processVideo() { 171 try (Scope scopedTags = 172 tagger 173 .currentBuilder() 174 .put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5")) 175 .buildScoped()) { 176 // Processing video. 177 // ... 178 179 // Record the processed video size. 180 statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25648).record(); 181 } 182 } 183 184 public static void printStats() { 185 ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME); 186 System.out.println( 187 String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData)); 188 } 189} 190``` 191 192## OpenCensus Quickstart for Applications 193 194Besides recording tracing/stats events the application also need to link the implementation, 195setup exporters, and debugging [Z-Pages](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages). 196 197### Add the dependencies to your project 198 199For Maven add to your `pom.xml`: 200```xml 201<dependencies> 202 <dependency> 203 <groupId>io.opencensus</groupId> 204 <artifactId>opencensus-api</artifactId> 205 <version>0.30.0</version> 206 </dependency> 207 <dependency> 208 <groupId>io.opencensus</groupId> 209 <artifactId>opencensus-impl</artifactId> 210 <version>0.30.0</version> 211 <scope>runtime</scope> 212 </dependency> 213</dependencies> 214``` 215 216For Gradle add to your dependencies: 217```groovy 218compile 'io.opencensus:opencensus-api:0.30.0' 219runtime 'io.opencensus:opencensus-impl:0.30.0' 220``` 221 222### How to setup exporters? 223 224#### Trace exporters 225* [Instana][TraceExporterInstana] 226* [Jaeger][TraceExporterJaeger] 227* [Logging][TraceExporterLogging] 228* [Stackdriver][TraceExporterStackdriver] 229* [Zipkin][TraceExporterZipkin] 230 231#### Stats exporters 232* [Stackdriver][StatsExporterStackdriver] 233* [SignalFx][StatsExporterSignalFx] 234* [Prometheus][StatsExporterPrometheus] 235 236### How to setup debugging Z-Pages? 237 238If the application owner wants to export in-process tracing and stats data via HTML debugging pages 239see this [link](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages#quickstart). 240 241## Versioning 242 243This library follows [Semantic Versioning][semver]. 244 245**GA**: Libraries defined at a GA quality level are stable, and will not introduce 246backwards-incompatible changes in any minor or patch releases. We will address issues and requests 247with the highest priority. If we were to make a backwards-incompatible changes on an API, we will 248first mark the existing API as deprecated and keep it for 18 months before removing it. 249 250**Beta**: Libraries defined at a Beta quality level are expected to be mostly stable and we're 251working towards their release candidate. We will address issues and requests with a higher priority. 252There may be backwards incompatible changes in a minor version release, though not in a patch 253release. If an element is part of an API that is only meant to be used by exporters or other 254opencensus libraries, then there is no deprecation period. Otherwise, we will deprecate it for 18 255months before removing it, if possible. 256 257[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master 258[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java 259[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true 260[appveyor-url]: https://ci.appveyor.com/project/opencensusjavateam/opencensus-java/branch/master 261[javadoc-image]: https://www.javadoc.io/badge/io.opencensus/opencensus-api.svg 262[javadoc-url]: https://www.javadoc.io/doc/io.opencensus/opencensus-api 263[maven-image]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-api/badge.svg 264[maven-url]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-api 265[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg 266[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge 267[codecov-image]: https://codecov.io/gh/census-instrumentation/opencensus-java/branch/master/graph/badge.svg 268[codecov-url]: https://codecov.io/gh/census-instrumentation/opencensus-java/branch/master/ 269[semver]: http://semver.org/ 270[TraceExporterInstana]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/instana#quickstart 271[TraceExporterJaeger]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/jaeger#quickstart 272[TraceExporterLogging]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/logging#quickstart 273[TraceExporterStackdriver]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/stackdriver#quickstart 274[TraceExporterZipkin]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/zipkin#quickstart 275[StatsExporterStackdriver]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/stackdriver#quickstart 276[StatsExporterSignalFx]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/signalfx#quickstart 277[StatsExporterPrometheus]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/prometheus#quickstart 278