blob: 0859e7dbb34e99a672f6c41840553479de959be5 [file] [log] [blame] [view]
Bogdan Drutua79694b2017-08-29 23:56:25 -07001# OpenCensus - A stats collection and distributed tracing framework
Bogdan Drutue9be1f22017-09-30 16:35:39 -04002[![Gitter chat][gitter-image]][gitter-url]
3[![Maven Central][maven-image]][maven-url]
Bogdan Drutu6e1ab482017-11-30 17:17:58 -08004[![Javadocs][javadoc-image]][javadoc-url]
Bogdan Drutue9be1f22017-09-30 16:35:39 -04005[![Build Status][travis-image]][travis-url]
6[![Windows Build Status][appveyor-image]][appveyor-url]
Kristen Kozakf4180852018-02-05 12:31:14 -08007[![Coverage Status][codecov-image]][codecov-url]
Bogdan Drutue9be1f22017-09-30 16:35:39 -04008
Dino Oliva6712e0f2016-05-20 16:39:02 -07009
Fabian Lange17a1a1a2018-02-09 14:33:30 -050010OpenCensus is a toolkit for collecting application performance and behavior data. It currently
Bogdan Drutua79694b2017-08-29 23:56:25 -070011includes 3 apis: stats, tracing and tags.
12
Yang Song8ff06672018-06-29 12:07:53 -070013The library is in [Beta](#versioning) stage and APIs are expected to be mostly stable. The
14library is expected to move to [GA](#versioning) stage after v1.0.0 major release.
Bogdan Drutua79694b2017-08-29 23:56:25 -070015
16Please join [gitter](https://gitter.im/census-instrumentation/Lobby) for help or feedback on this
17project.
18
Yang Song0d8f5f52018-07-19 15:40:54 -070019## OpenCensus Quickstart for Libraries
Bogdan Drutua79694b2017-08-29 23:56:25 -070020
21Integrating OpenCensus with a new library means recording stats or traces and propagating context.
Yang Song0d8f5f52018-07-19 15:40:54 -070022For application integration please see [Quickstart for Applications](https://github.com/census-instrumentation/opencensus-java#quickstart-for-applications).
Bogdan Drutua79694b2017-08-29 23:56:25 -070023
Reiley Yang9437d452018-06-21 14:57:50 -070024The full quick start example can also be found on the [OpenCensus website](https://opencensus.io/java/index.html).
Yang Song35bf9572018-03-06 19:44:03 -080025
Bogdan Drutua79694b2017-08-29 23:56:25 -070026### Add the dependencies to your project
27
28For Maven add to your `pom.xml`:
29```xml
30<dependencies>
31 <dependency>
32 <groupId>io.opencensus</groupId>
33 <artifactId>opencensus-api</artifactId>
sebrighta5d27392018-09-18 12:04:27 -070034 <version>0.16.1</version>
Bogdan Drutua79694b2017-08-29 23:56:25 -070035 </dependency>
36</dependencies>
37```
38
39For Gradle add to your dependencies:
40```gradle
sebrighta5d27392018-09-18 12:04:27 -070041compile 'io.opencensus:opencensus-api:0.16.1'
Bogdan Drutua79694b2017-08-29 23:56:25 -070042```
43
Yang Song299b08f2018-07-10 15:14:57 -070044For Bazel add the following lines to the WORKSPACE file:
45```
46maven_jar(
47 name = "io_opencensus_opencensus_api",
48 artifact = "io.opencensus:opencensus-api:0.15.0",
49 sha1 = "9a098392b287d7924660837f4eba0ce252013683",
50)
51```
52Then targets can specify `@io_opencensus_opencensus_api//jar` as a dependency to depend on this jar:
53```bazel
54deps = [
55 "@io_opencensus_opencensus_api//jar",
56]
57```
58You may also need to import the transitive dependencies. See [generate external dependencies from
59Maven projects](https://docs.bazel.build/versions/master/generate-workspace.html).
60
Bogdan Drutua79694b2017-08-29 23:56:25 -070061### Hello "OpenCensus" trace events
62
63Here's an example of creating a Span and record some trace annotations. Notice that recording the
64annotations is possible because we propagate scope. 3rd parties libraries like SLF4J can integrate
65the same way.
66
67```java
Kristen Kozakda272152018-07-11 12:30:58 -070068import io.opencensus.common.Scope;
69import io.opencensus.trace.Tracer;
70import io.opencensus.trace.Tracing;
71import io.opencensus.trace.samplers.Samplers;
72
Bogdan Drutua79694b2017-08-29 23:56:25 -070073public final class MyClassWithTracing {
Yang Song35bf9572018-03-06 19:44:03 -080074 private static final Tracer tracer = Tracing.getTracer();
75
Bogdan Drutua79694b2017-08-29 23:56:25 -070076 public static void doWork() {
Kristen Kozakda272152018-07-11 12:30:58 -070077 // Create a child Span of the current Span. Always record events for this span and force it to
78 // be sampled. This makes it easier to try out the example, but unless you have a clear use
Yang Songefa417a2018-04-02 16:49:22 -070079 // case, you don't need to explicitly set record events or sampler.
Kristen Kozakda272152018-07-11 12:30:58 -070080 try (Scope ss =
81 tracer
82 .spanBuilder("MyChildWorkSpan")
83 .setRecordEvents(true)
84 .setSampler(Samplers.alwaysSample())
85 .startScopedSpan()) {
Bogdan Drutua79694b2017-08-29 23:56:25 -070086 doInitialWork();
87 tracer.getCurrentSpan().addAnnotation("Finished initial work");
88 doFinalWork();
89 }
90 }
Fabian Lange17a1a1a2018-02-09 14:33:30 -050091
Bogdan Drutua79694b2017-08-29 23:56:25 -070092 private static void doInitialWork() {
93 // ...
94 tracer.getCurrentSpan().addAnnotation("Important.");
95 // ...
96 }
Fabian Lange17a1a1a2018-02-09 14:33:30 -050097
Bogdan Drutua79694b2017-08-29 23:56:25 -070098 private static void doFinalWork() {
99 // ...
100 tracer.getCurrentSpan().addAnnotation("More important.");
101 // ...
102 }
103}
104```
105
106### Hello "OpenCensus" stats events
107
Yang Song35bf9572018-03-06 19:44:03 -0800108Here's an example on
109 * defining TagKey, Measure and View,
110 * registering a view,
111 * putting TagKey and TagValue into a scoped TagContext,
112 * recording stats against current TagContext,
113 * getting ViewData.
114
115
116For the complete example, see
117[here](https://github.com/census-instrumentation/opencensus-java/blob/master/examples/src/main/java/io/opencensus/examples/helloworld/QuickStart.java).
118
119```java
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700120import io.opencensus.common.Scope;
121import io.opencensus.stats.Aggregation;
122import io.opencensus.stats.BucketBoundaries;
123import io.opencensus.stats.Measure.MeasureLong;
124import io.opencensus.stats.Stats;
125import io.opencensus.stats.StatsRecorder;
126import io.opencensus.stats.View;
127import io.opencensus.stats.ViewData;
128import io.opencensus.stats.ViewManager;
129import io.opencensus.tags.TagKey;
130import io.opencensus.tags.TagValue;
131import io.opencensus.tags.Tagger;
132import io.opencensus.tags.Tags;
133import java.util.Arrays;
134import java.util.Collections;
135
136public final class MyClassWithStats {
Yang Song35bf9572018-03-06 19:44:03 -0800137 private static final Tagger tagger = Tags.getTagger();
138 private static final ViewManager viewManager = Stats.getViewManager();
easye45afa12018-03-15 17:26:26 +1100139 private static final StatsRecorder statsRecorder = Stats.getStatsRecorder();
Yang Song35bf9572018-03-06 19:44:03 -0800140
141 // frontendKey allows us to break down the recorded data
Jean de Klerk93c9c5b2018-03-26 19:58:19 +0200142 private static final TagKey FRONTEND_KEY = TagKey.create("myorg_keys_frontend");
Yang Song35bf9572018-03-06 19:44:03 -0800143
144 // videoSize will measure the size of processed videos.
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700145 private static final MeasureLong VIDEO_SIZE =
146 MeasureLong.create("my.org/measure/video_size", "size of processed videos", "By");
Yang Song35bf9572018-03-06 19:44:03 -0800147
148 // Create view to see the processed video size distribution broken down by frontend.
149 // The view has bucket boundaries (0, 256, 65536) that will group measure values into
150 // histogram buckets.
151 private static final View.Name VIDEO_SIZE_VIEW_NAME = View.Name.create("my.org/views/video_size");
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700152 private static final View VIDEO_SIZE_VIEW =
153 View.create(
154 VIDEO_SIZE_VIEW_NAME,
155 "processed video size over time",
156 VIDEO_SIZE,
157 Aggregation.Distribution.create(
158 BucketBoundaries.create(Arrays.asList(0.0, 256.0, 65536.0))),
159 Collections.singletonList(FRONTEND_KEY));
Yang Song35bf9572018-03-06 19:44:03 -0800160
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700161 public static void initialize() {
Yang Song35bf9572018-03-06 19:44:03 -0800162 // ...
163 viewManager.registerView(VIDEO_SIZE_VIEW);
164 }
165
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700166 public static void processVideo() {
Yang Song35bf9572018-03-06 19:44:03 -0800167 try (Scope scopedTags =
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700168 tagger
169 .currentBuilder()
170 .put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5"))
171 .buildScoped()) {
Yang Song35bf9572018-03-06 19:44:03 -0800172 // Processing video.
173 // ...
174
175 // Record the processed video size.
176 statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25648).record();
177 }
178 }
179
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700180 public static void printStats() {
Yang Song35bf9572018-03-06 19:44:03 -0800181 ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME);
182 System.out.println(
Kristen Kozakb3e35a22018-07-11 12:31:30 -0700183 String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData));
Yang Song35bf9572018-03-06 19:44:03 -0800184 }
185}
186```
Bogdan Drutua79694b2017-08-29 23:56:25 -0700187
Yang Song0d8f5f52018-07-19 15:40:54 -0700188## OpenCensus Quickstart for Applications
Bogdan Drutua79694b2017-08-29 23:56:25 -0700189
Fabian Lange17a1a1a2018-02-09 14:33:30 -0500190Besides recording tracing/stats events the application also need to link the implementation,
Bogdan Drutua79694b2017-08-29 23:56:25 -0700191setup exporters, and debugging [Z-Pages](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages).
192
193### Add the dependencies to your project
194
195For Maven add to your `pom.xml`:
196```xml
197<dependencies>
198 <dependency>
199 <groupId>io.opencensus</groupId>
200 <artifactId>opencensus-api</artifactId>
sebrighta5d27392018-09-18 12:04:27 -0700201 <version>0.16.1</version>
Bogdan Drutua79694b2017-08-29 23:56:25 -0700202 </dependency>
203 <dependency>
204 <groupId>io.opencensus</groupId>
205 <artifactId>opencensus-impl</artifactId>
sebrighta5d27392018-09-18 12:04:27 -0700206 <version>0.16.1</version>
Bogdan Drutua79694b2017-08-29 23:56:25 -0700207 <scope>runtime</scope>
208 </dependency>
209</dependencies>
210```
211
212For Gradle add to your dependencies:
213```gradle
sebrighta5d27392018-09-18 12:04:27 -0700214compile 'io.opencensus:opencensus-api:0.16.1'
215runtime 'io.opencensus:opencensus-impl:0.16.1'
Bogdan Drutua79694b2017-08-29 23:56:25 -0700216```
217
Yang Song299b08f2018-07-10 15:14:57 -0700218For Bazel add the following lines to the WORKSPACE file:
219```
220maven_jar(
221 name = "io_opencensus_opencensus_api",
222 artifact = "io.opencensus:opencensus-api:0.15.0",
223 sha1 = "9a098392b287d7924660837f4eba0ce252013683",
224)
225
226maven_jar(
227 name = "io_opencensus_opencensus_impl_core",
228 artifact = "io.opencensus:opencensus-impl-core:0.15.0",
229 sha1 = "36c775926ba1e54af7c37d0503cfb99d986f6229",
230)
231
232maven_jar(
233 name = "io_opencensus_opencensus_impl",
234 artifact = "io.opencensus:opencensus-impl:0.15.0",
235 sha1 = "d7bf0d7ee5a0594f840271c11c9f8d6f754f35d6",
236)
237```
238Then add the following lines to BUILD.bazel file:
239```bazel
240deps = [
241 "@io_opencensus_opencensus_api//jar",
242]
243runtime_deps = [
244 "@io_opencensus_opencensus_impl_core//jar",
245 "@io_opencensus_opencensus_impl//jar",
246]
247```
248Again you may need to import the transitive dependencies. See [generate external dependencies from
249Maven projects](https://docs.bazel.build/versions/master/generate-workspace.html).
250
Bogdan Drutua79694b2017-08-29 23:56:25 -0700251### How to setup exporters?
252
253#### Trace exporters
Fabian Lange17a1a1a2018-02-09 14:33:30 -0500254* [Instana][TraceExporterInstana]
Yang Song9b6eb8c2018-03-16 11:00:43 -0700255* [Jaeger][TraceExporterJaeger]
Bogdan Drutu19bad532017-11-02 09:53:08 +1100256* [Logging][TraceExporterLogging]
257* [Stackdriver][TraceExporterStackdriver]
258* [Zipkin][TraceExporterZipkin]
Bogdan Drutua79694b2017-08-29 23:56:25 -0700259
260#### Stats exporters
Yang Song195946a2017-11-09 16:00:10 -0800261* [Stackdriver][StatsExporterStackdriver]
Maxime Petazzoni64d05832018-01-17 09:09:13 -0800262* [SignalFx][StatsExporterSignalFx]
Yang Song8df5b602018-02-16 18:38:04 -0800263* [Prometheus][StatsExporterPrometheus]
Bogdan Drutua79694b2017-08-29 23:56:25 -0700264
265### How to setup debugging Z-Pages?
266
Fabian Lange17a1a1a2018-02-09 14:33:30 -0500267If the application owner wants to export in-process tracing and stats data via HTML debugging pages
Bogdan Drutua79694b2017-08-29 23:56:25 -0700268see this [link](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages#quickstart).
Bogdan Drutufe821602017-08-11 18:50:37 +0300269
Yang Song8ff06672018-06-29 12:07:53 -0700270## Versioning
271
272This library follows [Semantic Versioning][semver].
273
274**GA**: Libraries defined at a GA quality level are stable, and will not introduce
275backwards-incompatible changes in any minor or patch releases. We will address issues and requests
276with the highest priority. If we were to make a backwards-incompatible changes on an API, we will
277first mark the existing API as deprecated and keep it for 18 months before removing it.
278
279**Beta**: Libraries defined at a Beta quality level are expected to be mostly stable and we're
280working towards their release candidate. We will address issues and requests with a higher priority.
281There may be backwards incompatible changes in a minor version release, though not in a patch
282release. If an element is part of an API that is only meant to be used by exporters or other
283opencensus libraries, then there is no deprecation period. Otherwise, we will deprecate it for 18
284months before removing it, if possible.
285
Bogdan Drutufe821602017-08-11 18:50:37 +0300286[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master
287[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java
288[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true
Kristen Kozakf8919632017-10-03 12:42:20 -0700289[appveyor-url]: https://ci.appveyor.com/project/opencensusjavateam/opencensus-java/branch/master
Bogdan Drutu6e1ab482017-11-30 17:17:58 -0800290[javadoc-image]: https://www.javadoc.io/badge/io.opencensus/opencensus-api.svg
291[javadoc-url]: https://www.javadoc.io/doc/io.opencensus/opencensus-api
Bogdan Drutufe821602017-08-11 18:50:37 +0300292[maven-image]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-api/badge.svg
Bogdan Drutue9be1f22017-09-30 16:35:39 -0400293[maven-url]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-api
Bogdan Drutue9be1f22017-09-30 16:35:39 -0400294[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
Bogdan Drutu19bad532017-11-02 09:53:08 +1100295[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
Kristen Kozakf4180852018-02-05 12:31:14 -0800296[codecov-image]: https://codecov.io/gh/census-instrumentation/opencensus-java/branch/master/graph/badge.svg
297[codecov-url]: https://codecov.io/gh/census-instrumentation/opencensus-java/branch/master/
Yang Song8ff06672018-06-29 12:07:53 -0700298[semver]: http://semver.org/
Fabian Lange17a1a1a2018-02-09 14:33:30 -0500299[TraceExporterInstana]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/instana#quickstart
Yang Song9b6eb8c2018-03-16 11:00:43 -0700300[TraceExporterJaeger]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/jaeger#quickstart
Bogdan Drutu19bad532017-11-02 09:53:08 +1100301[TraceExporterLogging]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/logging#quickstart
302[TraceExporterStackdriver]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/stackdriver#quickstart
303[TraceExporterZipkin]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/trace/zipkin#quickstart
Yang Song195946a2017-11-09 16:00:10 -0800304[StatsExporterStackdriver]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/stackdriver#quickstart
Maxime Petazzoni64d05832018-01-17 09:09:13 -0800305[StatsExporterSignalFx]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/signalfx#quickstart
Yang Song35bf9572018-03-06 19:44:03 -0800306[StatsExporterPrometheus]: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/prometheus#quickstart