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

chore: splitting BasePlugin into browser and node #981

Merged
merged 8 commits into from
May 12, 2020
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
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/karma.conf.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,6 +19,6 @@ const karmaBaseConfig = require('../../karma.base');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
webpack: karmaWebpackConfig,
}))
};
2 changes: 1 addition & 1 deletion packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious to know, why do we have to do this? Also, do you think we should add this to other common packages like api, tracing etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are only dedicated tests for browser then this is the way to avoid testing them in mocha. Some of our packages already have things like this, for example collector has common tests and then separate for node and browser.

"test:browser": "nyc karma start --single-run",
"tdd": "npm run tdd:node",
"tdd:node": "npm run test -- --watch-extensions ts --watch",
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
58 changes: 58 additions & 0 deletions packages/opentelemetry-core/src/platform/BaseAbstractPlugin.ts
Original file line number Diff line number Diff line change
@@ -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<T> implements Plugin<T> {
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;
dyladan marked this conversation as resolved.
Show resolved Hide resolved
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;
}
43 changes: 43 additions & 0 deletions packages/opentelemetry-core/src/platform/browser/BasePlugin.ts
Original file line number Diff line number Diff line change
@@ -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<T> extends BaseAbstractPlugin<T>
implements Plugin<T> {
enable(
moduleExports: T,
tracerProvider: TracerProvider,
logger: Logger,
config?: PluginConfig
): T {
this._moduleExports = moduleExports;
this._tracer = tracerProvider.getTracer(
dyladan marked this conversation as resolved.
Show resolved Hide resolved
this._tracerName,
this._tracerVersion
);
this._logger = logger;
if (config) this._config = config;
return this.patch();
}
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

export * from './BasePlugin';
export * from './hex-to-base64';
export * from './id';
export * from './performance';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {
Tracer,
Plugin,
Logger,
PluginConfig,
Expand All @@ -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<T> implements Plugin<T> {
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<T> extends BaseAbstractPlugin<T>
implements Plugin<T> {
enable(
moduleExports: T,
tracerProvider: TracerProvider,
Expand Down Expand Up @@ -147,5 +131,6 @@ export abstract class BasePlugin<T> implements Plugin<T> {
}

protected abstract patch(): T;

protected abstract unpatch(): void;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
11 changes: 8 additions & 3 deletions packages/opentelemetry-core/test/index-webpack.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'));
Original file line number Diff line number Diff line change
@@ -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 {}
}