Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-trace-web): web worker support #2719

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ jobs:
run: npm run test:browser
- name: Report Coverage
run: npm run codecov:browser
webworker-tests-stable:
runs-on: ubuntu-latest
container:
image: circleci/node:14-browsers
env:
NPM_CONFIG_UNSAFE_PERM: true
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Permission Setup
run: sudo chmod -R 777 /github /__w

- name: restore lerna
uses: actions/cache@v2
id: cache
with:
path: |
node_modules
*/*/node_modules
key: browser-tests-stable-${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('**/package.json') }}

- name: Bootstrap
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --ignore-scripts
npx lerna bootstrap --no-ci --hoist --nohoist='zone.js'

- name: Build 🔧
run: |
npm run compile

- name: Unit tests
run: npm run test:webworker
- name: Report Coverage
run: npm run codecov:webworker
node-tests-experimental:
strategy:
fail-fast: false
Expand Down Expand Up @@ -164,3 +199,42 @@ jobs:
- name: Report Coverage
working-directory: experimental
run: npm run codecov:browser
webworker-tests-experimental:
runs-on: ubuntu-latest
container:
image: circleci/node:14-browsers
env:
NPM_CONFIG_UNSAFE_PERM: true
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Permission Setup
run: sudo chmod -R 777 /github /__w

- name: restore lerna
uses: actions/cache@v2
id: cache
with:
path: |
experimental/node_modules
experimental/*/*/node_modules
key: browser-tests-experimental-${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('**/package.json') }}

- name: Bootstrap
if: steps.cache.outputs.cache-hit != 'true'
working-directory: experimental
run: |
npm install --ignore-scripts
npx lerna bootstrap --no-ci --hoist --nohoist='zone.js'

- name: Build 🔧
working-directory: experimental
run: |
npm run compile

- name: Unit tests
working-directory: experimental
run: npm run test:webworker
- name: Report Coverage
working-directory: experimental
run: npm run codecov:webworker
21 changes: 21 additions & 0 deletions doc/web-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Web API Usages Guidance

The packages of OpenTelemetry that targeting web platforms should be compatible
with the following web environments:

- [Browsing Context](https://developer.mozilla.org/en-US/docs/Glossary/Browsing_context),
- [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers).

As such, the usage of Web API that depends on APIs like [`window`],
[`document`] and [`navigator`] is discouraged.

If the use of the browsing context API is necessary, like adding `onload`
listeners, an alternative code path for Web Worker environment should also be
supported.

It is an exception to above guidance if the package is instrumenting the
browsing context only.

[`window`]: https://developer.mozilla.org/en-US/docs/Web/API/window
[`document`]: https://developer.mozilla.org/en-US/docs/Web/API/Document
[`navigator]: https://developer.mozilla.org/en-US/docs/Web/API/Navigator
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module.exports = {
"env": {
"mocha": true,
"commonjs": true,
"browser": true,
"jquery": true
},
...require('../../../eslint.config.js')
}
4 changes: 3 additions & 1 deletion karma.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = {
},
reporters: ['spec', 'coverage-istanbul'],
files: ['test/index-webpack.ts'],
preprocessors: { 'test/index-webpack.ts': ['webpack'] },
preprocessors: {
'test/index-webpack*.ts': ['webpack']
},
webpackMiddleware: { noInfo: true }
};
27 changes: 27 additions & 0 deletions karma.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const baseConfig = require('./karma.base');

module.exports = {
...baseConfig,
frameworks: ['mocha-webworker'],

files: [{
pattern: 'test/index-webpack.worker.ts',
included: false,
}],
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"version:update": "lerna run version:update",
"test": "lerna run test",
"test:browser": "lerna run test:browser",
"test:webworker": "lerna run test:webworker",
"test:backcompat": "lerna run test:backcompat",
"bootstrap": "lerna bootstrap --hoist --nohoist='zone.js'",
"changelog": "lerna-changelog",
"codecov": "lerna run codecov",
"codecov:browser": "lerna run codecov:browser",
"codecov:webworker": "lerna run codecov:webworker",
"predocs-test": "npm run docs",
"docs": "typedoc && touch docs/.nojekyll",
"docs-deploy": "gh-pages --dotfiles --dist docs",
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-sdk-trace-web/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module.exports = {
"env": {
"mocha": true,
"commonjs": true,
"browser": true,
"jquery": true
},
...require('../../eslint.config.js')
}
24 changes: 24 additions & 0 deletions packages/opentelemetry-sdk-trace-web/karma.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const karmaWebpackConfig = require('../../karma.webpack');
const karmaBaseConfig = require('../../karma.worker');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
}))
};
3 changes: 3 additions & 0 deletions packages/opentelemetry-sdk-trace-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"codecov:webworker": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"version": "node ../../scripts/version-update.js",
"tdd": "karma start",
"test:browser": "nyc karma start --single-run",
"test:webworker": "nyc karma start karma.worker.js --single-run",
"watch": "tsc --build --watch tsconfig.all.json",
"precompile": "lerna run version --scope $(npm pkg get name) --include-dependencies",
"prewatch": "npm run precompile"
Expand Down Expand Up @@ -69,6 +71,7 @@
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-jquery": "0.2.4",
"karma-mocha": "2.0.1",
"karma-mocha-webworker": "1.3.0",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "4.0.2",
"mocha": "7.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function getResource(
}
const sorted = sortResources(filteredResources);

if (parsedSpanUrl.origin !== window.location.origin && sorted.length > 1) {
if (parsedSpanUrl.origin !== location.origin && sorted.length > 1) {
let corsPreFlightRequest: PerformanceResourceTiming | undefined = sorted[0];
let mainRequest: PerformanceResourceTiming = findMainRequest(
sorted,
Expand Down Expand Up @@ -421,7 +421,7 @@ export function shouldPropagateTraceHeaders(
}
const parsedSpanUrl = parseUrl(spanUrl);

if (parsedSpanUrl.origin === window.location.origin) {
if (parsedSpanUrl.origin === location.origin) {
return true;
} else {
return propagateTraceHeaderUrls.some(propagateTraceHeaderUrl =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('StackContextManager', () => {
assert.strictEqual(contextManager.active(), ctx1);
return done();
});
assert.strictEqual(contextManager.active(), window);
assert.strictEqual(contextManager.active(), globalThis);
});

it('should finally restore an old context when context is an object', done => {
Expand All @@ -130,7 +130,7 @@ describe('StackContextManager', () => {
assert.strictEqual(contextManager.active(), ctx1);
return done();
});
assert.strictEqual(contextManager.active(), window);
assert.strictEqual(contextManager.active(), globalThis);
});

it('should forward this, arguments and return value', () => {
Expand Down
13 changes: 9 additions & 4 deletions packages/opentelemetry-sdk-trace-web/test/index-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const testsContext = require.context('.', true, /test$/);
testsContext.keys().forEach(testsContext);

const srcContext = require.context('.', true, /src$/);
srcContext.keys().forEach(srcContext);
{
const testsContext = require.context('./', false, /test$/);
testsContext.keys().forEach(testsContext);
}

{
const testsContext = require.context('./window', false, /test$/);
testsContext.keys().forEach(testsContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const testsContext = require.context('./', false, /test$/);
testsContext.keys().forEach(testsContext);
Loading