|
1 | | -# OpenCensus for Web Browsers |
| 1 | +# OpenCensus - A stats collection and distributed tracing framework |
2 | 2 | [![Gitter chat][gitter-image]][gitter-url] |
| 3 | +[![circleci][circleci-image]][circleci-url] |
| 4 | +[![codecov][codecov-image]][codecov-url] |
| 5 | +[![Known Vulnerabilities][snyk-image]][snyk-url] |
| 6 | +[![Apache License][license-image]][license-url] |
3 | 7 |
|
4 | | -OpenCensus Web is an experimental implementation of OpenCensus, a toolkit for |
5 | | -collecting application performance and behavior monitoring data. |
| 8 | +OpenCensus Web is an implementation of OpenCensus, a toolkit for collecting |
| 9 | +application performance and behavior monitoring data. This library currently |
| 10 | +generates traces for the initial page load of a site in the browser. It supports |
| 11 | +sending the trace sampling decision and trace ID from the web server to the |
| 12 | +browser client to enable latency debugging across the stack. |
6 | 13 |
|
7 | | -The library is in an experimental pre-alpha stage and the API is subject to |
8 | | -change, and is not suitable for production use. |
| 14 | +The library is in alpha stage and the API is subject to change. |
9 | 15 |
|
10 | | -Please join [gitter](https://gitter.im/census-instrumentation/Lobby) for help or feedback on this |
11 | | -project. |
| 16 | +Please join [gitter][gitter-url] for help or feedback on this project. |
12 | 17 |
|
| 18 | +## Usage |
| 19 | + |
| 20 | +**NOTE**: *These usage instructions will likely change as the project matures. |
| 21 | +The goal is to make this easier to use over time!* |
| 22 | + |
| 23 | +See the [examples/initial_load][examples-initial-load-url] folder for a full |
| 24 | +example of how to use OpenCensus Web in an application. Below are |
| 25 | +the steps that are currently needed to use it. |
| 26 | + |
| 27 | +### 1. Deploy the OpenCensus agent to collect traces |
| 28 | + |
| 29 | +The OpenCensus Web library currently only exports traces via the |
| 30 | +[OpenCensus Agent][oc-agent-url], which can then export them to a variety of |
| 31 | +tracing backends such as Zipkin, Jaeger, Stackdriver, DataDog, Honeycomb, |
| 32 | +and AWS X-Ray. See this [example's README][initial-load-example-url] |
| 33 | +for steps to run the agent locally or in the cloud. |
| 34 | + |
| 35 | +When you run the agent in the cloud you will need to expose it to your |
| 36 | +application via the internet. You may also to proxy the agent behind the |
| 37 | +authentication or CSRF protection layer of your application to to only allow |
| 38 | +traces to be written by authenticated end users. |
| 39 | + |
| 40 | +### 2. Use the OpenCensus Web library code in your application |
| 41 | + |
| 42 | +To include the OpenCensus Web library code, clone this repo and the build the JS |
| 43 | +bundles: |
| 44 | +```bash |
| 45 | +git clone https://github.com/census-instrumentation/opencensus-web |
| 46 | +cd packages/opencensus-web-all |
| 47 | +npm run build:prod |
| 48 | +``` |
| 49 | + |
| 50 | +Then you copy the script `./dist/initial-load-all.js` as a static asset |
| 51 | +of your application, and include it in a `<script>` tag, e.g. |
| 52 | +`<script src="/static/initial-load-all.js"></script>` assuming you were serving |
| 53 | +it from the `/static` folder of your site. |
| 54 | + |
| 55 | +Once the OpenCensus Web packages are released to NPM, you will also be able to |
| 56 | +include them in your JavaScript build pipeline as an imported dependency. |
| 57 | + |
| 58 | +In order to indicate the endpoint of the OpenCensus Agent so that traces can be |
| 59 | +written, you will need to include a snippet of JavaScript that sets the |
| 60 | +`ocAgent` global variable, for instance: |
| 61 | + |
| 62 | +```html |
| 63 | +<script>ocAgent = 'https://example.com/my-opencensus-agent-endpoint'</script> |
| 64 | +``` |
| 65 | + |
| 66 | +### 3. Optional: Send a trace parent and sampling decision from your server |
| 67 | + |
| 68 | +Currently, the OpenCensus Web will sample all requests by default. This is |
| 69 | +useful for experimentation with the library but is not appropriate for a real |
| 70 | +application. |
| 71 | + |
| 72 | +OpenCensus Web also supports connecting the server side spans for the initial |
| 73 | +HTML load with the client side span for the load from the browser's timing API. |
| 74 | + |
| 75 | +Because the browser does not send a trace context header for the initial page |
| 76 | +navigation, the server needs to fake a trace context header in a middleware and |
| 77 | +then send that trace context header back to the client as a global `traceparent` |
| 78 | +variable. The `traceparent` variable should be in the |
| 79 | +[trace context W3C draft format][trace-context-url]. |
| 80 | + |
| 81 | +To see a full example of how the middleware to generate a trace context header |
| 82 | +and send it back to the client, see the |
| 83 | +[server.go][initial-load-example-server-go] file and the |
| 84 | +[index.html][initial-load-example-index-html] template in the |
| 85 | +[examples/initial_load][initial-load-example-url] folder. |
| 86 | + |
| 87 | +## Packages |
| 88 | + |
| 89 | +The OpenCensus web library is composed of the following packages: |
| 90 | + |
| 91 | +- **[@opencensus/web-types][package-web-types]**. This has automatically copied types from the [@opencensus/core][package-core] package of [OpenCensus Node][opencensus-node]. The types are copied because the `@opencensus/core` package has Node-specific dependencies that make it hard to depend on from browser-specific code. |
| 92 | +- **[@opencensus/web-core][package-web-core]**. This has the core span and tracer implementation for web browsers. Currently the tracer assumes there is only one current root span for a given browser tab. |
| 93 | +- **[@opencensus/web-exporter-ocagent][package-web-exporter-ocagent]**. This handles exporting spans to the [OpenCensus Agent][opencensus-service-url] |
| 94 | +- **[@opencensus/web-instrumentation-perf][package-web-instrumentation-perf]**. This is code to convert the initial load resource waterfall timings from the browser [Navigation Timing API][navigation-timing-url] and [Resource Timing API][resource-timing-url] into the spans for a trace of the overall initial load for a page. |
| 95 | +- **[@opencensus/web-propagation-tracecontext][package-web-propagation-tracecontext]**. This provides utilities to serialize and deserialize a `traceparent` trace context header in the [W3C draft trace context format][trace-context-url] |
| 96 | +- **[@opencensus/web-all][package-web-all]**. This depends on all the other OpenCensus Web libraries and provides top-level functions for instrumenting the initial page load and exporting its spans to the OpenCensus Agent. It also provides WebPack builds for JS bundles that can be used in `<script>` tags. |
| 97 | + |
| 98 | +## Useful links |
| 99 | +- For more information on OpenCensus, visit: <https://opencensus.io/> |
| 100 | +- For help or feedback on this project, join us on [gitter][gitter-url] |
| 101 | + |
| 102 | +## Versioning |
| 103 | + |
| 104 | +This library follows [Semantic Versioning][semver-url]. |
| 105 | + |
| 106 | +**GA**: Libraries defined at a GA quality level are stable, and will not introduce |
| 107 | +backwards-incompatible changes in any minor or patch releases. We will address issues and requests |
| 108 | +with the highest priority. If we were to make a backwards-incompatible changes on an API, we will |
| 109 | +first mark the existing API as deprecated and keep it for 18 months before removing it. |
| 110 | + |
| 111 | +**Beta**: Libraries defined at a Beta quality level are expected to be mostly stable and we're |
| 112 | +working towards their release candidate. We will address issues and requests with a higher priority. |
| 113 | +There may be backwards incompatible changes in a minor version release, though not in a patch |
| 114 | +release. If an element is part of an API that is only meant to be used by exporters or other |
| 115 | +opencensus libraries, then there is no deprecation period. Otherwise, we will deprecate it for 18 |
| 116 | +months before removing it, if possible. |
| 117 | + |
| 118 | +**Alpha**: Libraries defined at an Alpha quality level can be unstable and could cause crashes or data loss. Alpha software may not contain all of the features that are planned for the final version. The API is subject to change. |
| 119 | + |
| 120 | +## License |
| 121 | + |
| 122 | +Apache 2.0 - See [LICENSE][license-url] for more information. |
| 123 | + |
| 124 | +[circleci-image]: https://circleci.com/gh/census-instrumentation/opencensus-web.svg?style=shield |
| 125 | +[circleci-url]: https://circleci.com/gh/census-instrumentation/opencensus-web |
| 126 | +[codecov-image]: https://codecov.io/gh/census-instrumentation/opencensus-web/branch/master/graph/badge.svg |
| 127 | +[codecov-url]: https://codecov.io/gh/census-instrumentation/opencensus-web |
| 128 | +[examples-initial-load-url]: https://github.com/census-instrumentation/opencensus-web/tree/master/examples/initial_load |
13 | 129 | [gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg |
14 | 130 | [gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge |
| 131 | +[initial-load-example-index-html]: https://github.com/census-instrumentation/opencensus-web/blob/master/examples/initial_load/index.html |
| 132 | +[initial-load-example-server-go]: https://github.com/census-instrumentation/opencensus-web/blob/master/examples/initial_load/server.go |
| 133 | +[initial-load-example-url]: https://github.com/census-instrumentation/opencensus-web/tree/master/examples/initial_load |
| 134 | +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat |
| 135 | +[license-url]: https://github.com/census-instrumentation/opencensus-web/blob/master/LICENSE |
| 136 | +[navigation-timing-url]: https://www.w3.org/TR/navigation-timing-2/ |
| 137 | +[oc-agent-url]: https://github.com/census-instrumentation/opencensus-service |
| 138 | +[opencensus-node-url]: https://github.com/census-instrumentation/opencensus-node |
| 139 | +[opencensus-service-url]: https://github.com/census-instrumentation/opencensus-service |
| 140 | +[package-core]: https://github.com/census-instrumentation/opencensus-node/tree/master/packages/opencensus-core |
| 141 | +[package-web-all]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-all |
| 142 | +[package-web-core]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-core |
| 143 | +[package-web-exporter-ocagent]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-exporter-ocagent |
| 144 | +[package-web-instrumentation-perf]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-instrumentation-perf |
| 145 | +[package-web-propagation-tracecontext]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-propagation-tracecontext |
| 146 | +[package-web-types]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-types |
| 147 | +[resource-timing-url]: https://www.w3.org/TR/resource-timing-2/ |
| 148 | +[semver-url]: http://semver.org/ |
| 149 | +[snyk-image]: https://snyk.io/test/github/census-instrumentation/opencensus-web/badge.svg?style=flat |
| 150 | +[snyk-url]: https://snyk.io/test/github/census-instrumentation/opencensus-web |
| 151 | +[trace-context-url]: https://www.w3.org/TR/trace-context/ |
0 commit comments