diff --git a/packages/opentelemetry-core/karma.conf.js b/packages/opentelemetry-core/karma.conf.js index 7183aab033..7b8c9678db 100644 --- a/packages/opentelemetry-core/karma.conf.js +++ b/packages/opentelemetry-core/karma.conf.js @@ -1,5 +1,5 @@ /*! - * Copyright 2019, OpenTelemetry Authors + * Copyright 2020, OpenTelemetry Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,6 @@ const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: karmaWebpackConfig + webpack: karmaWebpackConfig, })) }; diff --git a/packages/opentelemetry-core/package.json b/packages/opentelemetry-core/package.json index 149e6f2824..33cadce020 100644 --- a/packages/opentelemetry-core/package.json +++ b/packages/opentelemetry-core/package.json @@ -10,7 +10,7 @@ "types": "build/src/index.d.ts", "repository": "open-telemetry/opentelemetry-js", "scripts": { - "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts", + "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts --exclude 'test/platform/browser/**/*.ts'", "test:browser": "nyc karma start --single-run", "tdd": "npm run tdd:node", "tdd:node": "npm run test -- --watch-extensions ts --watch", diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index 46f1baf8be..7e561a52bb 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -26,7 +26,6 @@ export * from './context/propagation/composite'; export * from './context/propagation/HttpTraceContext'; export * from './context/propagation/types'; export * from './platform'; -export * from './trace/instrumentation/BasePlugin'; export * from './trace/NoRecordingSpan'; export * from './trace/sampler/ProbabilitySampler'; export * from './trace/spancontext-utils'; diff --git a/packages/opentelemetry-core/src/platform/BaseAbstractPlugin.ts b/packages/opentelemetry-core/src/platform/BaseAbstractPlugin.ts new file mode 100644 index 0000000000..57ec582d2d --- /dev/null +++ b/packages/opentelemetry-core/src/platform/BaseAbstractPlugin.ts @@ -0,0 +1,58 @@ +/*! + * Copyright 2020, 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. + */ + +import { + Tracer, + Plugin, + Logger, + PluginConfig, + TracerProvider, + PluginInternalFiles, +} from '@opentelemetry/api'; + +/** This class represent the base to patch plugin. */ +export abstract class BaseAbstractPlugin implements Plugin { + abstract readonly moduleName: string; // required for internalFilesExports + supportedVersions?: string[]; + readonly version?: string; // required for internalFilesExports + + protected readonly _basedir?: string; // required for internalFilesExports + protected _config!: PluginConfig; + protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports + protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports + protected _logger!: Logger; + protected _moduleExports!: T; + protected _tracer!: Tracer; + + constructor( + protected readonly _tracerName: string, + protected readonly _tracerVersion?: string + ) {} + + disable(): void { + this.unpatch(); + } + + abstract enable( + moduleExports: T, + tracerProvider: TracerProvider, + logger: Logger, + config?: PluginConfig + ): T; + + protected abstract patch(): T; + protected abstract unpatch(): void; +} diff --git a/packages/opentelemetry-core/src/platform/browser/BasePlugin.ts b/packages/opentelemetry-core/src/platform/browser/BasePlugin.ts new file mode 100644 index 0000000000..f9b99d05cb --- /dev/null +++ b/packages/opentelemetry-core/src/platform/browser/BasePlugin.ts @@ -0,0 +1,43 @@ +/*! + * Copyright 2020, 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. + */ + +import { + Logger, + Plugin, + PluginConfig, + TracerProvider, +} from '@opentelemetry/api'; +import { BaseAbstractPlugin } from '../BaseAbstractPlugin'; + +/** This class represent the base to patch plugin. */ +export abstract class BasePlugin extends BaseAbstractPlugin + implements Plugin { + enable( + moduleExports: T, + tracerProvider: TracerProvider, + logger: Logger, + config?: PluginConfig + ): T { + this._moduleExports = moduleExports; + this._tracer = tracerProvider.getTracer( + this._tracerName, + this._tracerVersion + ); + this._logger = logger; + if (config) this._config = config; + return this.patch(); + } +} diff --git a/packages/opentelemetry-core/src/platform/browser/index.ts b/packages/opentelemetry-core/src/platform/browser/index.ts index 36164efce5..15f8e3412d 100644 --- a/packages/opentelemetry-core/src/platform/browser/index.ts +++ b/packages/opentelemetry-core/src/platform/browser/index.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export * from './BasePlugin'; export * from './hex-to-base64'; export * from './id'; export * from './performance'; diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/platform/node/BasePlugin.ts similarity index 84% rename from packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts rename to packages/opentelemetry-core/src/platform/node/BasePlugin.ts index 601939df36..f3b713bf67 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/platform/node/BasePlugin.ts @@ -15,7 +15,6 @@ */ import { - Tracer, Plugin, Logger, PluginConfig, @@ -25,26 +24,11 @@ import { } from '@opentelemetry/api'; import * as semver from 'semver'; import * as path from 'path'; +import { BaseAbstractPlugin } from '../BaseAbstractPlugin'; /** This class represent the base to patch plugin. */ -export abstract class BasePlugin implements Plugin { - supportedVersions?: string[]; - abstract readonly moduleName: string; // required for internalFilesExports - readonly version?: string; // required for internalFilesExports - protected readonly _basedir?: string; // required for internalFilesExports - - protected _moduleExports!: T; - protected _tracer!: Tracer; - protected _logger!: Logger; - protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports - protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports - protected _config!: PluginConfig; - - constructor( - protected readonly _tracerName: string, - protected readonly _tracerVersion?: string - ) {} - +export abstract class BasePlugin extends BaseAbstractPlugin + implements Plugin { enable( moduleExports: T, tracerProvider: TracerProvider, @@ -147,5 +131,6 @@ export abstract class BasePlugin implements Plugin { } protected abstract patch(): T; + protected abstract unpatch(): void; } diff --git a/packages/opentelemetry-core/src/platform/node/index.ts b/packages/opentelemetry-core/src/platform/node/index.ts index 47a879b04d..71f289fdf5 100644 --- a/packages/opentelemetry-core/src/platform/node/index.ts +++ b/packages/opentelemetry-core/src/platform/node/index.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -export * from './sdk-info'; +export * from './BasePlugin'; export * from './id'; export * from './performance'; +export * from './sdk-info'; export * from './timer-util'; export * from './hex-to-base64'; diff --git a/packages/opentelemetry-core/test/index-webpack.ts b/packages/opentelemetry-core/test/index-webpack.ts index 7731f09091..2c89bb37ee 100644 --- a/packages/opentelemetry-core/test/index-webpack.ts +++ b/packages/opentelemetry-core/test/index-webpack.ts @@ -1,5 +1,5 @@ /*! - * Copyright 2019, OpenTelemetry Authors + * Copyright 2020, OpenTelemetry Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,13 @@ // This file is the webpack entry point for the browser Karma tests. It requires // all modules ending in "test" from the current folder and all its subfolders. -const testsContext = require.context('.', true, /test$/); -testsContext.keys().forEach(testsContext); +const testsContextCommon = require.context('.', true, /test$/); +testsContextCommon.keys().forEach(key => { + if (key.indexOf('./platform/BasePlugin.test') >= 0) { + return function() {}; + } + return testsContextCommon(key); +}); const srcContext = require.context('.', true, /src$/); srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-core/test/trace/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts similarity index 91% rename from packages/opentelemetry-core/test/trace/BasePlugin.test.ts rename to packages/opentelemetry-core/test/platform/BasePlugin.test.ts index 643b7562dd..115c07627c 100644 --- a/packages/opentelemetry-core/test/trace/BasePlugin.test.ts +++ b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts @@ -18,15 +18,14 @@ import { NoopTracerProvider } from '@opentelemetry/api'; import * as assert from 'assert'; import * as path from 'path'; import { BasePlugin, NoopLogger } from '../../src'; -import * as types from './fixtures/test-package/foo/bar/internal'; +import * as types from '../trace/fixtures/test-package/foo/bar/internal'; const provider = new NoopTracerProvider(); const logger = new NoopLogger(); - describe('BasePlugin', () => { describe('internalFilesLoader', () => { it('should load internally exported files', () => { - const testPackage = require('./fixtures/test-package'); + const testPackage = require('../trace/fixtures/test-package'); const plugin = new TestPlugin(); assert.doesNotThrow(() => { plugin.enable(testPackage, provider, logger); @@ -81,4 +80,4 @@ class TestPlugin extends BasePlugin<{ [key: string]: Function }> { protected unpatch(): void {} } -const basedir = path.dirname(require.resolve('./fixtures/test-package')); +const basedir = path.dirname(require.resolve('../trace/fixtures/test-package')); diff --git a/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts new file mode 100644 index 0000000000..cca6a23649 --- /dev/null +++ b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts @@ -0,0 +1,48 @@ +/** + * Copyright 2020, 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. + */ + +import { NOOP_TRACER, NoopTracerProvider } from '@opentelemetry/api'; +import * as assert from 'assert'; +import { BasePlugin, NoopLogger } from '../../../src'; + +const provider = new NoopTracerProvider(); +const logger = new NoopLogger(); +describe('BasePlugin', () => { + describe('enable', () => { + it('should enable plugin', () => { + const moduleExports = { foo: function() {} }; + const plugin = new TestPlugin('foo', '1'); + const patch = plugin.enable(moduleExports, provider, logger); + + assert.strictEqual(plugin['_tracer'], NOOP_TRACER); + assert.strictEqual(plugin['_tracerName'], 'foo'); + assert.strictEqual(plugin['_tracerVersion'], '1'); + assert.strictEqual(plugin['_logger'], logger); + assert.strictEqual(patch, moduleExports); + }); + }); +}); + +class TestPlugin extends BasePlugin<{ [key: string]: Function }> { + readonly moduleName = 'test-package'; + readonly version = '0.1.0'; + + patch(): { [key: string]: Function } { + return this._moduleExports; + } + + protected unpatch(): void {} +}