11# OpenCensus - A stats collection and distributed tracing framework
22[ ![ Gitter chat] [ gitter-image ]] [ gitter-url ]
3+ [ ![ NPM Published Version] [ npm-img ]] [ npm-url ]
34[ ![ circleci] [ circleci-image ]] [ circleci-url ]
45[ ![ codecov] [ codecov-image ]] [ codecov-url ]
56[ ![ Known Vulnerabilities] [ snyk-image ]] [ snyk-url ]
@@ -15,10 +16,48 @@ The library is in alpha stage and the API is subject to change.
1516
1617Please join [ gitter] [ gitter-url ] for help or feedback on this project.
1718
18- ## Usage
19+ ## Features
20+
21+ ### Trace spans for initial load including server side HTML rendering
22+
23+ Here's a sample trace from OpenCensus Web that was exported to
24+ [ Zipkin] [ zipkin-url ] . Notice that there is an overall ` nav./ ` span for the user
25+ navigation experience until the browser ` load ` event fires. There are also
26+ ` / ` spans for the client and server side measurements of the initial HTML load.
27+ The server side spans also indicates how much time was spent parsing and
28+ rendering the template:
29+
30+ ![ zipkin-waterfall] ( images/zipkin_waterfall.png )
31+
32+ These spans are generated via the browser's
33+ [ Navigation Timing] [ navigation-timing-url ] and
34+ [ Resource Timing] [ resource-timing-url ] APIs. Notice also the ` long js task `
35+ span, which indicates a CPU-bound JavaScript event loop that took more than 50ms
36+ as measured by the [ Long Tasks] [ long-tasks-url ] browser API.
37+
38+ ### Span annotations for DOM and network events
1939
20- ** NOTE** : * These usage instructions will likely change as the project matures.
21- The goal is to make this easier to use over time!*
40+ The OpenCensus Web spans also include detailed annotations for DOM load events
41+ like ` domInteractive ` and ` first-paint ` , as well as network events like
42+ ` domainLookupStart ` and ` secureConnectionStart ` . Here is a similar trace
43+ exported to [ Stckdriver Trace] [ stackdriver-trace-url ] with the annotations
44+ expanded:
45+
46+ ![ stackdriver-trace-events] ( images/stackdriver_trace_events.png )
47+
48+ These annotations also comine from the
49+ [ Resource Timing API] [ resource-timing-url ] . Note that for resources fetched via
50+ CORS, you will need to specify the
51+ [ Timing-Allow-Origin header] [ timing-allow-origin-url ] .
52+
53+ ### Export traces to Zipkin, Jaeger, Stackdriver, DataDog, Honeycomb and more
54+
55+ The OpenCensus Web library exports traces via the
56+ [ OpenCensus Agent] [ oc-agent-url ] , which can then export them to a variety of
57+ trace backends. See the
58+ [ OpenCensus Agent/Collector exporter docs] [ oc-agent-exporter-url ] for details.
59+
60+ ## Usage
2261
2362See the [ examples/initial_load] [ examples-initial-load-url ] folder for a full
2463example of how to use OpenCensus Web in an application. Below are
@@ -27,9 +66,8 @@ the steps that are currently needed to use it.
2766### 1. Deploy the OpenCensus agent to collect traces
2867
2968The 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 ]
69+ [ OpenCensus Agent] [ oc-agent-url ] , which can then export them to a trace backend
70+ (e.g. Zipkin, Stackdriver). See this [ example's README] [ initial-load-example-url ]
3371for steps to run the agent locally or in the cloud.
3472
3573When you run the agent in the cloud you will need to expose it to your
@@ -39,34 +77,51 @@ traces to be written by authenticated end users.
3977
4078### 2. Use the OpenCensus Web library code in your application
4179
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
80+ #### Using via a ` <script> ` tag
81+
82+ You can use OpenCensus Web via a ` <script> ` tag that makes uses of the
83+ <unpkg.com> CDN that serves files from NPM packages.
84+
85+ The script tag should go before the ` </body> ` . You will also need to set the
86+ ` ocAgent ` endpoint in a different ` <script> ` and may also specify the
87+ trace sample rate via an ` ocSampleRate ` global variable as well.
88+ ``` html
89+ ...
90+ <script src =" https://unpkg.com/@opencensus/web-all@0.0.2/dist/initial-load-all.js"
91+ integrity =" sha384-VPY9XX7tiXeLekDPFXkfO2AqNpLTCNOzfXxVghzoVP05PXrG+wtHOW2kOP2ggO9o"
92+ async crossorigin =" anonymous" >
93+ </script >
94+ <script >
95+ // HTTP endpoint of the OpenCensus Agent you ran and exposed to the internet
96+ // in Step 1 above.
97+ ocAgent = ' https://example.com/my-opencensus-agent-endpoint' ;
98+
99+ // Sample all requests for trace, which is useful when testing.
100+ // By default this is set to sample 1/10000 requests.
101+ ocSampleRate = 1.0 ;
102+ </script >
103+ </body >
48104```
49105
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.
106+ You can also host the ` initial-load-all.js ` bundle from your own site. For
107+ docs on how to build the bundle see the
108+ [ @opencensus/web-all readme] [ package-web-all ] .
54109
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.
110+ #### Using as NPM dependency in existing JS build
57111
58- In order to indicate the trace sampling rate and endpoint of the OpenCensus
59- Agent so that traces can be written, you will need to include a snippet of
60- JavaScript that sets the ` ocAgent ` and ` ocSampleRate ` global variables,
61- for instance:
112+ If you already have a JS build pipeline using e.g. webpack or similar, you can
113+ do ` npm install @opencensus/web-all ` , and then set the configuration variables
114+ and trigger the recording and exporting of the initial load spans as follows:
62115
63- ``` html
64- <script >
65- ocAgent = ' https://example.com/my-opencensus-agent-endpoint' ;
66- // Sample all requests for trace, which is useful when testing.
67- // By default this is set to sample 1/10000 requests.
68- ocSampleRate = 1.0 ;
69- </script >
116+ ``` js
117+ import { exportRootSpanAfterLoadEvent } from ' @opencensus/web-all' ;
118+
119+ window .ocAgent = ' https://example.com/my-opencensus-agent-endpoint' ;
120+ window .ocSampleRate = 1.0 ; // Sample at 100% for test only. Default is 1/10000.
121+
122+ // Send the root span and child spans for the initial page load to the
123+ // OpenCensus agent if this request was sampled for trace.
124+ exportRootSpanAfterLoadEvent ();
70125```
71126
72127### 3. Optional: Send a trace parent and sampling decision from your server
@@ -80,7 +135,27 @@ Because the browser does not send a trace context header for the initial page
80135navigation, the server needs to fake a trace context header in a middleware and
81136then send that trace context header back to the client as a global ` traceparent `
82137variable. The ` traceparent ` variable should be in the
83- [ trace context W3C draft format] [ trace-context-url ] .
138+ [ trace context W3C draft format] [ trace-context-url ] . For example:
139+
140+ ``` html
141+ ...
142+ <script src =" https://unpkg.com/@opencensus/web-all@0.0.2/dist/initial-load-all.js"
143+ integrity =" sha384-VPY9XX7tiXeLekDPFXkfO2AqNpLTCNOzfXxVghzoVP05PXrG+wtHOW2kOP2ggO9o"
144+ async crossorigin =" anonymous" >
145+ </script >
146+ <script >
147+ ocAgent = ' https://example.com/my-opencensus-agent-endpoint' ;
148+ // Set the `traceparent` in the server's HTML template code. It should be
149+ // dynamically generated server side to have the server's request trace ID,
150+ // a parent span ID that was set on the server's request span, and the trace
151+ // flags to indicate the server's sampling decision (01 = sampled, 00 = not
152+ // sampled).
153+ traceparent = ' 00-{{ServerTraceId}}-{{ServerParentSpanId}}-{{ServerTraceFlags}}' ;
154+ // We don't need to specify `ocSampleRate` since the trace sampling decision
155+ // is coming from the `traceparent` global variable instead.
156+ </script >
157+ </body >
158+ ```
84159
85160To see a full example of how the middleware to generate a trace context header
86161and send it back to the client, see the
@@ -137,8 +212,13 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
137212[ initial-load-example-url ] : https://github.com/census-instrumentation/opencensus-web/tree/master/examples/initial_load
138213[ license-image ] : https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
139214[ license-url ] : https://github.com/census-instrumentation/opencensus-web/blob/master/LICENSE
215+ [ long-tasks-url ] : https://w3c.github.io/longtasks/
140216[ navigation-timing-url ] : https://www.w3.org/TR/navigation-timing-2/
217+ [ npm-img ] : https://badge.fury.io/js/%40opencensus%2Fweb-all.svg
218+ [ npm-url ] : https://www.npmjs.com/package/@opencensus/web-all
219+ [ oc-agent-exporter-url ] : https://github.com/census-instrumentation/opencensus-service/tree/master/exporter
141220[ oc-agent-url ] : https://github.com/census-instrumentation/opencensus-service
221+ [ oc-web-issue-url ] : https://github.com/census-instrumentation/opencensus-web/issues/new/choose
142222[ opencensus-node-url ] : https://github.com/census-instrumentation/opencensus-node
143223[ opencensus-service-url ] : https://github.com/census-instrumentation/opencensus-service
144224[ package-core ] : https://github.com/census-instrumentation/opencensus-node/tree/master/packages/opencensus-core
@@ -152,4 +232,8 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
152232[ semver-url ] : http://semver.org/
153233[ snyk-image ] : https://snyk.io/test/github/census-instrumentation/opencensus-web/badge.svg?style=flat
154234[ snyk-url ] : https://snyk.io/test/github/census-instrumentation/opencensus-web
235+ [ stackdriver-trace-url ] : https://cloud.google.com/trace/
236+ [ timing-allow-origin-url ] : https://www.w3.org/TR/resource-timing-2/#dfn-timing-allow-origin
155237[ trace-context-url ] : https://www.w3.org/TR/trace-context/
238+ [ tsickle-url ] : https://github.com/angular/tsickle
239+ [ zipkin-url ] : https://zipkin.io/
0 commit comments