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

Commit cdb3151

Browse files
Webpack builds for opencensus-web-all JS bundles (#22)
1 parent 7dbec3d commit cdb3151

17 files changed

Lines changed: 1796 additions & 437 deletions

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

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

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

2020
delete webpackConfig.entry;

packages/opencensus-web-all/package-lock.json

Lines changed: 1398 additions & 426 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-all/package.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "build/src/index.js",
66
"types": "build/src/index.d.ts",
77
"scripts": {
8-
"build": "webpack",
8+
"build:prod": "webpack --config ./webpack/prod-bundles.config.js",
9+
"build:dev": "webpack --config ./webpack/dev-bundles.config.js",
10+
"start:webpack-server": "webpack-dev-server --config ./webpack/dev-bundles.config.js",
911
"test": "karma start --single-run",
1012
"test:start": "karma start",
1113
"clean": "rimraf build/*",
@@ -55,13 +57,18 @@
5557
"karma-webpack": "^4.0.0-rc.5",
5658
"rimraf": "^2.6.2",
5759
"ts-loader": "^5.1.0",
58-
"typescript": "^3.1.6",
60+
"tslib": "^1.9.3",
61+
"typescript": "^3.3.3333",
5962
"webpack": "^4.18.0",
60-
"webpack-cli": "^3.1.0"
63+
"webpack-cli": "^3.1.0",
64+
"webpack-dev-server": "^3.2.1"
6165
},
6266
"dependencies": {
6367
"@opencensus/web-core": "^0.0.1",
64-
"@opencensus/web-instrumentation-perf": "^0.0.1",
65-
"@opencensus/web-exporter-ocagent": "^0.0.1"
66-
}
68+
"@opencensus/web-exporter-ocagent": "^0.0.1",
69+
"@opencensus/web-instrumentation-perf": "^0.0.1"
70+
},
71+
"sideEffects": [
72+
"./src/entrypoints/*.ts"
73+
]
6774
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Entrypoints for OpenCensus Web JS Bundles
2+
3+
This folder contains the top-level entrypoints for JS script bundles for
4+
the OpenCensus Web project.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 {exportRootSpanAfterLoadEvent} from '../export-initial-load';
18+
19+
exportRootSpanAfterLoadEvent();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 './instrument-initial-load';
18+
import './export-initial-load';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 {recordLongTasks} from '@opencensus/web-instrumentation-perf';
18+
19+
if (window.performance) {
20+
if (performance.setResourceTimingBufferSize) {
21+
// Set a larger performance buffer size to make sure events aren't lost.
22+
performance.setResourceTimingBufferSize(1000);
23+
}
24+
recordLongTasks();
25+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 {adjustPerfTimeOrigin, tracing} from '@opencensus/web-core';
18+
import {OCAgentExporter} from '@opencensus/web-exporter-ocagent';
19+
import {clearPerfEntries, getInitialLoadRootSpan, getPerfEntries} from '@opencensus/web-instrumentation-perf';
20+
21+
import {OpenCensusWebConfig, WindowWithOcwGlobals} from './types';
22+
23+
const windowWithOcwGlobals = window as WindowWithOcwGlobals;
24+
25+
/**
26+
* How long to wait after `load` event to export initial load spans. This allows
27+
* time for any other post-load handlers to run first so that the work to export
28+
* spans does not slow down the user experience.
29+
*/
30+
const WAIT_TIME_AFTER_LOAD_MS = 2000; // 2 seconds
31+
32+
/** Trace endpoint in the OC agent. */
33+
const TRACE_ENDPOINT = '/v1/trace';
34+
35+
/**
36+
* Waits until after the document `load` event fires, and then uses the
37+
* `window.ocwConfig` settings to configure an OpenCensus agent exporter and
38+
* export the spans for the initial page load.
39+
*/
40+
export function exportRootSpanAfterLoadEvent() {
41+
const config = windowWithOcwGlobals.ocwConfig;
42+
if (!config || !config.agent || !config.sampled) {
43+
console.log('Not configured to export page load spans.');
44+
return;
45+
}
46+
47+
tracing.registerExporter(
48+
new OCAgentExporter({agentEndpoint: `${config.agent}${TRACE_ENDPOINT}`}));
49+
50+
if (document.readyState === 'complete') {
51+
exportInitialLoadSpans(config);
52+
} else {
53+
window.addEventListener('load', () => {
54+
exportInitialLoadSpans(config);
55+
});
56+
}
57+
}
58+
59+
function exportInitialLoadSpans(config: OpenCensusWebConfig) {
60+
setTimeout(() => {
61+
const perfEntries = getPerfEntries();
62+
63+
// Adjust the performance time origin with server time if we have it.
64+
if (perfEntries.navigationTiming && config.reqStartTime &&
65+
config.reqDuration) {
66+
adjustPerfTimeOrigin(
67+
config.reqStartTime, config.reqDuration,
68+
perfEntries.navigationTiming);
69+
}
70+
71+
const root = getInitialLoadRootSpan(
72+
tracing.tracer, perfEntries, config.spanId, config.traceId);
73+
74+
clearPerfEntries();
75+
// Notify that the span has ended to trigger export.
76+
tracing.tracer.onEndSpan(root);
77+
}, WAIT_TIME_AFTER_LOAD_MS);
78+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
// Re-export symbols from the main OpenCensus Web libraries.
18+
export * from '@opencensus/web-core';
19+
export * from '@opencensus/web-exporter-ocagent';
20+
export * from '@opencensus/web-instrumentation-perf';
21+
22+
export {exportRootSpanAfterLoadEvent} from './export-initial-load';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 {PerformanceLongTaskTiming, WindowWithLongTasks} from '@opencensus/web-instrumentation-perf';
18+
19+
/** OpenCensus Web configuration that can be set on `window`. */
20+
export declare interface OpenCensusWebConfig {
21+
/**
22+
* Whether the initial load root span should be sampled for trace. This is
23+
* only a hint and should not be used to enforce sampling since clients could
24+
* simply ignore this field.
25+
*/
26+
sampled?: boolean;
27+
/** Trace ID for the initial load spans. */
28+
traceId?: string;
29+
/** Span ID for the initial load fetch client span. */
30+
spanId?: string;
31+
/**
32+
* Start time of server fetch request for initial navigation HTML in server
33+
* time epoch milliseconds. This is used to correct for clock skew between
34+
* client and server before exporting spans.
35+
*/
36+
reqStartTime?: number;
37+
/**
38+
* Duration of the server fetch request for initial HTML in server
39+
* milliseconds. This is also used to correct for clock skew.
40+
*/
41+
reqDuration?: number;
42+
/**
43+
* HTTP root URL of the agent endpoint to write traces to.
44+
* Example 'https://my-oc-agent-deployment.com:55678'
45+
*/
46+
agent?: string;
47+
}
48+
49+
/**
50+
* Type for the `window` object with the variables OpenCensus Web interacts
51+
* with.
52+
*/
53+
export declare interface WindowWithOcwGlobals extends WindowWithLongTasks {
54+
ocwLt?: PerformanceLongTaskTiming[];
55+
ocwConfig?: OpenCensusWebConfig;
56+
}

0 commit comments

Comments
 (0)