Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 14d9c91

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Allow sampling for user interaction traces (#144)
1 parent 76fbd7c commit 14d9c91

36 files changed

Lines changed: 57343 additions & 216 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ The OpenCensus web library is composed of the following packages:
172172
- **[@opencensus/web-exporter-ocagent][package-web-exporter-ocagent]**. This handles exporting spans to the [OpenCensus Agent][opencensus-service-url]
173173
- **[@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.
174174
- **[@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]
175+
- **[@opencensus/web-initial-load][package-web-initial-load]**. This package is in charge of generating the initial load page trace, plus the corresponding sampling decision for OpenCensus Web and
176+
storing a global initial load span context to be used by other packages.
175177
- **[@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.
176178

177179
## Useful links
@@ -225,6 +227,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
225227
[package-web-all]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-all
226228
[package-web-core]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-core
227229
[package-web-exporter-ocagent]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-exporter-ocagent
230+
[package-web-initial-load]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-initial-load
228231
[package-web-instrumentation-perf]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-instrumentation-perf
229232
[package-web-propagation-tracecontext]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-propagation-tracecontext
230233
[package-web-types]: https://github.com/census-instrumentation/opencensus-web/tree/master/packages/opencensus-web-types

packages/opencensus-web-all/karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
const webpackConfig = require('./webpack/dev-package.config');
17+
const webpackConfig = require('./webpack.config');
1818
const path = require('path');
1919

2020
delete webpackConfig.entry;
@@ -29,7 +29,7 @@ module.exports = (config) => {
2929
files: ['test/index.ts'],
3030
preprocessors: {'test/index.ts': ['webpack']},
3131
// Use webpack so that tree-shaking will remove all Node.js dependencies of
32-
// the `@opencensus/core` library, since they are not actually used in this
32+
// the `@opencensus/web-types` library, since they are not actually used in this
3333
// package's compiled JS code. Only the TypeScript interfaces from
3434
// `@opecensus/core` are used.
3535
webpack: webpackConfig,

packages/opencensus-web-all/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
"build:prod": "webpack --config ./webpack/prod-bundles.config.js",
99
"build:dev": "webpack --config ./webpack/dev-bundles.config.js",
1010
"start:webpack-server": "webpack-dev-server --config ./webpack/dev-bundles.config.js",
11-
"test": "karma start --single-run",
12-
"test:start": "karma start",
1311
"codecov": "codecov -f coverage/*.json",
1412
"clean": "rimraf build/*",
1513
"check": "gts check",
@@ -73,7 +71,8 @@
7371
"@opencensus/web-core": "^0.0.3",
7472
"@opencensus/web-exporter-ocagent": "^0.0.3",
7573
"@opencensus/web-instrumentation-perf": "^0.0.3",
76-
"@opencensus/web-propagation-tracecontext": "^0.0.3"
74+
"@opencensus/web-propagation-tracecontext": "^0.0.3",
75+
"@opencensus/web-initial-load": "^0.0.3"
7776
},
7877
"sideEffects": [
7978
"./src/entrypoints/*.ts"

packages/opencensus-web-all/src/entrypoints/export-initial-load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { exportRootSpanAfterLoadEvent } from '../export-initial-load';
17+
import { exportRootSpanAfterLoadEvent } from '../index';
1818

1919
exportRootSpanAfterLoadEvent();

packages/opencensus-web-all/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export * from '@opencensus/web-core';
1919
export * from '@opencensus/web-exporter-ocagent';
2020
export * from '@opencensus/web-instrumentation-perf';
2121

22-
export { exportRootSpanAfterLoadEvent } from './export-initial-load';
22+
export { exportRootSpanAfterLoadEvent } from '@opencensus/web-initial-load';

packages/opencensus-web-all/src/initial-load-context.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/opencensus-web-all/src/types.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/opencensus-web-all/test/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
import './test-export-initial-load';
18-
import './test-initial-load-context';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { SpanContext } from '@opencensus/web-types';
18+
19+
const IS_SAMPLED_BIT = 0x1;
20+
21+
/**
22+
* Returns whether sampling hint bit of the intial load span context `options`
23+
* is set.
24+
*/
25+
export function isSampled(spanContext: SpanContext) {
26+
const options = spanContext.options;
27+
if (!options) return false;
28+
return !!(options & IS_SAMPLED_BIT);
29+
}
30+
31+
/**
32+
* Random sampling decision using `Math.random`.
33+
*/
34+
export function makeRandomSamplingDecision(sampleRate: number): number {
35+
// Math.random returns a number in the 0-1 range (inclusive of 0 but not 1).
36+
// That means we should use the strict `<` operator to compare it to the
37+
// sample rate. A value of 1 for `options` indicates trace sampling.
38+
return Math.random() < sampleRate ? 1 : 0;
39+
}

packages/opencensus-web-core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export { RootSpan } from './trace/model/root-span';
1919
export { Span } from './trace/model/span';
2020
export { Tracer } from './trace/model/tracer';
2121
export { Tracing } from './trace/model/tracing';
22-
export * from './trace/model/util';
22+
export { isSampled, makeRandomSamplingDecision } from './common/sampling-util';
23+
export { WindowWithOcwGlobals } from './trace/model/types';
2324
export * from './trace/model/attribute-keys';
2425
export { VERSION } from './version';
2526

0 commit comments

Comments
 (0)