Skip to content

Commit 2c9ed63

Browse files
authored
chore: splitting BasePlugin into browser and node (#981)
1 parent 35cda77 commit 2c9ed63

File tree

11 files changed

+170
-31
lines changed

11 files changed

+170
-31
lines changed

packages/opentelemetry-core/karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2019, OpenTelemetry Authors
2+
* Copyright 2020, OpenTelemetry Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,6 @@ const karmaBaseConfig = require('../../karma.base');
1919

2020
module.exports = (config) => {
2121
config.set(Object.assign({}, karmaBaseConfig, {
22-
webpack: karmaWebpackConfig
22+
webpack: karmaWebpackConfig,
2323
}))
2424
};

packages/opentelemetry-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"types": "build/src/index.d.ts",
1111
"repository": "open-telemetry/opentelemetry-js",
1212
"scripts": {
13-
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
13+
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts --exclude 'test/platform/browser/**/*.ts'",
1414
"test:browser": "nyc karma start --single-run",
1515
"tdd": "npm run tdd:node",
1616
"tdd:node": "npm run test -- --watch-extensions ts --watch",

packages/opentelemetry-core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export * from './context/propagation/composite';
2626
export * from './context/propagation/HttpTraceContext';
2727
export * from './context/propagation/types';
2828
export * from './platform';
29-
export * from './trace/instrumentation/BasePlugin';
3029
export * from './trace/NoRecordingSpan';
3130
export * from './trace/sampler/ProbabilitySampler';
3231
export * from './trace/spancontext-utils';
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry 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+
* https://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 {
18+
Tracer,
19+
Plugin,
20+
Logger,
21+
PluginConfig,
22+
TracerProvider,
23+
PluginInternalFiles,
24+
} from '@opentelemetry/api';
25+
26+
/** This class represent the base to patch plugin. */
27+
export abstract class BaseAbstractPlugin<T> implements Plugin<T> {
28+
abstract readonly moduleName: string; // required for internalFilesExports
29+
supportedVersions?: string[];
30+
readonly version?: string; // required for internalFilesExports
31+
32+
protected readonly _basedir?: string; // required for internalFilesExports
33+
protected _config!: PluginConfig;
34+
protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports
35+
protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports
36+
protected _logger!: Logger;
37+
protected _moduleExports!: T;
38+
protected _tracer!: Tracer;
39+
40+
constructor(
41+
protected readonly _tracerName: string,
42+
protected readonly _tracerVersion?: string
43+
) {}
44+
45+
disable(): void {
46+
this.unpatch();
47+
}
48+
49+
abstract enable(
50+
moduleExports: T,
51+
tracerProvider: TracerProvider,
52+
logger: Logger,
53+
config?: PluginConfig
54+
): T;
55+
56+
protected abstract patch(): T;
57+
protected abstract unpatch(): void;
58+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry 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+
* https://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 {
18+
Logger,
19+
Plugin,
20+
PluginConfig,
21+
TracerProvider,
22+
} from '@opentelemetry/api';
23+
import { BaseAbstractPlugin } from '../BaseAbstractPlugin';
24+
25+
/** This class represent the base to patch plugin. */
26+
export abstract class BasePlugin<T> extends BaseAbstractPlugin<T>
27+
implements Plugin<T> {
28+
enable(
29+
moduleExports: T,
30+
tracerProvider: TracerProvider,
31+
logger: Logger,
32+
config?: PluginConfig
33+
): T {
34+
this._moduleExports = moduleExports;
35+
this._tracer = tracerProvider.getTracer(
36+
this._tracerName,
37+
this._tracerVersion
38+
);
39+
this._logger = logger;
40+
if (config) this._config = config;
41+
return this.patch();
42+
}
43+
}

packages/opentelemetry-core/src/platform/browser/index.ts

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

17+
export * from './BasePlugin';
1718
export * from './hex-to-base64';
1819
export * from './id';
1920
export * from './performance';

packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts renamed to packages/opentelemetry-core/src/platform/node/BasePlugin.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import {
18-
Tracer,
1918
Plugin,
2019
Logger,
2120
PluginConfig,
@@ -25,26 +24,11 @@ import {
2524
} from '@opentelemetry/api';
2625
import * as semver from 'semver';
2726
import * as path from 'path';
27+
import { BaseAbstractPlugin } from '../BaseAbstractPlugin';
2828

2929
/** This class represent the base to patch plugin. */
30-
export abstract class BasePlugin<T> implements Plugin<T> {
31-
supportedVersions?: string[];
32-
abstract readonly moduleName: string; // required for internalFilesExports
33-
readonly version?: string; // required for internalFilesExports
34-
protected readonly _basedir?: string; // required for internalFilesExports
35-
36-
protected _moduleExports!: T;
37-
protected _tracer!: Tracer;
38-
protected _logger!: Logger;
39-
protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports
40-
protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports
41-
protected _config!: PluginConfig;
42-
43-
constructor(
44-
protected readonly _tracerName: string,
45-
protected readonly _tracerVersion?: string
46-
) {}
47-
30+
export abstract class BasePlugin<T> extends BaseAbstractPlugin<T>
31+
implements Plugin<T> {
4832
enable(
4933
moduleExports: T,
5034
tracerProvider: TracerProvider,
@@ -147,5 +131,6 @@ export abstract class BasePlugin<T> implements Plugin<T> {
147131
}
148132

149133
protected abstract patch(): T;
134+
150135
protected abstract unpatch(): void;
151136
}

packages/opentelemetry-core/src/platform/node/index.ts

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

17-
export * from './sdk-info';
17+
export * from './BasePlugin';
1818
export * from './id';
1919
export * from './performance';
20+
export * from './sdk-info';
2021
export * from './timer-util';
2122
export * from './hex-to-base64';

packages/opentelemetry-core/test/index-webpack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2019, OpenTelemetry Authors
2+
* Copyright 2020, OpenTelemetry Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,13 @@
1616

1717
// This file is the webpack entry point for the browser Karma tests. It requires
1818
// all modules ending in "test" from the current folder and all its subfolders.
19-
const testsContext = require.context('.', true, /test$/);
20-
testsContext.keys().forEach(testsContext);
19+
const testsContextCommon = require.context('.', true, /test$/);
20+
testsContextCommon.keys().forEach(key => {
21+
if (key.indexOf('./platform/BasePlugin.test') >= 0) {
22+
return function() {};
23+
}
24+
return testsContextCommon(key);
25+
});
2126

2227
const srcContext = require.context('.', true, /src$/);
2328
srcContext.keys().forEach(srcContext);

packages/opentelemetry-core/test/trace/BasePlugin.test.ts renamed to packages/opentelemetry-core/test/platform/BasePlugin.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ import { NoopTracerProvider } from '@opentelemetry/api';
1818
import * as assert from 'assert';
1919
import * as path from 'path';
2020
import { BasePlugin, NoopLogger } from '../../src';
21-
import * as types from './fixtures/test-package/foo/bar/internal';
21+
import * as types from '../trace/fixtures/test-package/foo/bar/internal';
2222

2323
const provider = new NoopTracerProvider();
2424
const logger = new NoopLogger();
25-
2625
describe('BasePlugin', () => {
2726
describe('internalFilesLoader', () => {
2827
it('should load internally exported files', () => {
29-
const testPackage = require('./fixtures/test-package');
28+
const testPackage = require('../trace/fixtures/test-package');
3029
const plugin = new TestPlugin();
3130
assert.doesNotThrow(() => {
3231
plugin.enable(testPackage, provider, logger);
@@ -81,4 +80,4 @@ class TestPlugin extends BasePlugin<{ [key: string]: Function }> {
8180
protected unpatch(): void {}
8281
}
8382

84-
const basedir = path.dirname(require.resolve('./fixtures/test-package'));
83+
const basedir = path.dirname(require.resolve('../trace/fixtures/test-package'));

0 commit comments

Comments
 (0)