From 67bc7b7d2781adb6b352591013364ef462ea7b4f Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 16 Dec 2019 14:21:03 -0500 Subject: [PATCH 01/15] chore: more advanced plugin configuration --- examples/dns/setup.js | 16 ++-- .../src/trace/instrumentation/BasePlugin.ts | 14 +-- packages/opentelemetry-node/README.md | 5 +- packages/opentelemetry-node/src/NodeTracer.ts | 4 +- packages/opentelemetry-node/src/config.ts | 6 +- .../src/instrumentation/PluginLoader.ts | 45 +++++++++- .../test/NodeTracer.test.ts | 7 +- .../test/instrumentation/PluginLoader.test.ts | 86 +++++++++++++++---- packages/opentelemetry-plugin-dns/README.md | 5 +- packages/opentelemetry-plugin-http/README.md | 3 + packages/opentelemetry-plugin-https/README.md | 3 + .../README.md | 4 + packages/opentelemetry-plugin-redis/README.md | 4 + .../src/trace/instrumentation/Plugin.ts | 31 +++---- 14 files changed, 176 insertions(+), 57 deletions(-) diff --git a/examples/dns/setup.js b/examples/dns/setup.js index 26da571236..2da1601a87 100644 --- a/examples/dns/setup.js +++ b/examples/dns/setup.js @@ -9,15 +9,13 @@ const EXPORTER = process.env.EXPORTER || ''; function setupTracerAndExporters(service) { const tracer = new NodeTracer({ -      plugins: { -          dns: { -            enabled: true, -            path: '@opentelemetry/plugin-dns', - // Avoid dns lookup loop with http zipkin calls - ignoreHostnames: ['localhost'] -        } -      } -  }); + plugins: { + dns: { + enabled: true, + path: '@opentelemetry/plugin-dns', + } + } + }); let exporter; if (EXPORTER.toLowerCase().startsWith('z')) { diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts index 5a58aa6962..67cf630bf2 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts @@ -15,15 +15,15 @@ */ import { - Tracer, - Plugin, Logger, - PluginConfig, + Plugin, PluginInternalFiles, PluginInternalFilesVersion, + PluginOptions, + Tracer, } from '@opentelemetry/types'; -import * as semver from 'semver'; import * as path from 'path'; +import * as semver from 'semver'; /** This class represent the base to patch plugin. */ export abstract class BasePlugin implements Plugin { @@ -37,19 +37,19 @@ export abstract class BasePlugin implements Plugin { protected _logger!: Logger; protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports - protected _config!: PluginConfig; + protected _config: PluginOptions = {}; enable( moduleExports: T, tracer: Tracer, logger: Logger, - config?: PluginConfig + config: PluginOptions ): T { this._moduleExports = moduleExports; this._tracer = tracer; this._logger = logger; this._internalFilesExports = this._loadInternalFilesExports(); - if (config) this._config = config; + this._config = config; return this.patch(); } diff --git a/packages/opentelemetry-node/README.md b/packages/opentelemetry-node/README.md index 8a24c5b0d7..b38ef8c528 100644 --- a/packages/opentelemetry-node/README.md +++ b/packages/opentelemetry-node/README.md @@ -64,8 +64,11 @@ const tracer = new NodeTracer({ // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-http', // http plugin options + options: {}, } - } + }, + // shared plugin options + sharedPluginOptions: {} }); // Initialize the tracer diff --git a/packages/opentelemetry-node/src/NodeTracer.ts b/packages/opentelemetry-node/src/NodeTracer.ts index 86591fd0ac..7ca83fe7fb 100644 --- a/packages/opentelemetry-node/src/NodeTracer.ts +++ b/packages/opentelemetry-node/src/NodeTracer.ts @@ -17,7 +17,7 @@ import { BasicTracer } from '@opentelemetry/tracing'; import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks'; import { PluginLoader } from './instrumentation/PluginLoader'; -import { NodeTracerConfig, DEFAULT_INSTRUMENTATION_PLUGINS } from './config'; +import { NodeTracerConfig } from './config'; /** * This class represents a node tracer with `async_hooks` module. @@ -36,7 +36,7 @@ export class NodeTracer extends BasicTracer { super(Object.assign({ scopeManager: config.scopeManager }, config)); this._pluginLoader = new PluginLoader(this, this.logger); - this._pluginLoader.load(config.plugins || DEFAULT_INSTRUMENTATION_PLUGINS); + this._pluginLoader.load(config); } stop() { diff --git a/packages/opentelemetry-node/src/config.ts b/packages/opentelemetry-node/src/config.ts index 23fe0ff63e..0c4288a74b 100644 --- a/packages/opentelemetry-node/src/config.ts +++ b/packages/opentelemetry-node/src/config.ts @@ -16,13 +16,17 @@ import { Plugins } from './instrumentation/PluginLoader'; import { BasicTracerConfig } from '@opentelemetry/tracing'; +import { PluginOptions } from '@opentelemetry/types'; /** * NodeTracerConfig provides an interface for configuring a Node Tracer. */ export interface NodeTracerConfig extends BasicTracerConfig { - /** Plugins options. */ + /** Plugins configuration for individual modules */ plugins?: Plugins; + + /** Plugin options applied to all plugins */ + sharedPluginOptions?: PluginOptions; } /** List of all default supported plugins */ diff --git a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts index 3eb8881e6f..361c7fe9fe 100644 --- a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts +++ b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -import { Logger, Plugin, Tracer, PluginConfig } from '@opentelemetry/types'; +import { Logger, Plugin, PluginConfig, Tracer } from '@opentelemetry/types'; import * as hook from 'require-in-the-middle'; +import { DEFAULT_INSTRUMENTATION_PLUGINS, NodeTracerConfig } from '../config'; import * as utils from './utils'; // States for the Plugin Loader @@ -65,7 +66,40 @@ export class PluginLoader { * @param Plugins an object whose keys are plugin names and whose * {@link PluginConfig} values indicate several configuration options. */ - load(plugins: Plugins): PluginLoader { + load(tracerConfig: NodeTracerConfig): PluginLoader { + // const plugins = Object.assign({}, DEFAULT_INSTRUMENTATION_PLUGINS, tracerConfig.plugins); + + const configuredPluginModuleNames = Array.from( + new Set([ + ...Object.keys(tracerConfig.plugins || {}), + ...Object.keys(DEFAULT_INSTRUMENTATION_PLUGINS), + ]) + ); + + const plugins: Plugins = {}; + for (const pluginModuleName of configuredPluginModuleNames) { + const defaultConfig = + DEFAULT_INSTRUMENTATION_PLUGINS[pluginModuleName] || {}; + const config = + (tracerConfig.plugins && tracerConfig.plugins[pluginModuleName]) || {}; + + const enabled = + typeof config.enabled === 'boolean' + ? config.enabled + : defaultConfig.enabled; + + plugins[pluginModuleName] = { + path: config.path || defaultConfig.path, + enabled: typeof enabled === 'boolean' ? enabled : true, + options: Object.assign( + {}, + defaultConfig.options, + tracerConfig.sharedPluginOptions, + config.options + ), + }; + } + if (this._hookState === HookState.UNINITIALIZED) { const pluginsToLoad = filterPlugins(plugins); const modulesToHook = Object.keys(pluginsToLoad); @@ -113,9 +147,14 @@ export class PluginLoader { return exports; } + const options = Object.assign( + {}, + tracerConfig.sharedPluginOptions, + config.options + ); this._plugins.push(plugin); // Enable each supported plugin. - return plugin.enable(exports, this.tracer, this.logger, config); + return plugin.enable(exports, this.tracer, this.logger, options); } catch (e) { this.logger.error( `PluginLoader#load: could not load plugin ${modulePath} of module ${name}. Error: ${e.message}` diff --git a/packages/opentelemetry-node/test/NodeTracer.test.ts b/packages/opentelemetry-node/test/NodeTracer.test.ts index 6c0df61de7..d829d0ac52 100644 --- a/packages/opentelemetry-node/test/NodeTracer.test.ts +++ b/packages/opentelemetry-node/test/NodeTracer.test.ts @@ -96,9 +96,10 @@ describe('NodeTracer', () => { 'supported-module': { enabled: true, path: '@opentelemetry/plugin-supported-module', - enhancedDatabaseReporting: false, - ignoreMethods: [], - ignoreUrls: [], + options: { + enhancedDatabaseReporting: false, + ignoreUrls: [], + }, }, }, }); diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index af6ca193e7..57c7290e28 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -30,8 +30,6 @@ const simplePlugins: Plugins = { 'simple-module': { enabled: true, path: '@opentelemetry/plugin-simple-module', - ignoreMethods: [], - ignoreUrls: [], }, }; @@ -39,8 +37,9 @@ const httpPlugins: Plugins = { http: { enabled: true, path: '@opentelemetry/plugin-http-module', - ignoreMethods: [], - ignoreUrls: [], + options: { + httpPluginOverrideOption: 2, + }, }, }; @@ -107,14 +106,14 @@ describe('PluginLoader', () => { it('transitions from UNINITIALIZED to ENABLED', () => { const pluginLoader = new PluginLoader(tracer, logger); - pluginLoader.load(simplePlugins); + pluginLoader.load({ plugins: simplePlugins }); assert.strictEqual(pluginLoader['_hookState'], HookState.ENABLED); pluginLoader.unload(); }); it('transitions from ENABLED to DISABLED', () => { const pluginLoader = new PluginLoader(tracer, logger); - pluginLoader.load(simplePlugins).unload(); + pluginLoader.load({ plugins: simplePlugins }).unload(); assert.strictEqual(pluginLoader['_hookState'], HookState.DISABLED); }); }); @@ -140,7 +139,7 @@ describe('PluginLoader', () => { it('should load a plugin and patch the target modules', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(simplePlugins); + pluginLoader.load({ plugins: simplePlugins }); // The hook is only called the first time the module is loaded. const simpleModule = require('simple-module'); assert.strictEqual(pluginLoader['_plugins'].length, 1); @@ -152,18 +151,75 @@ describe('PluginLoader', () => { it('should load a plugin and patch the core module', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(httpPlugins); + pluginLoader.load({ plugins: httpPlugins }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); }); + + it('should set shared options on plugins', () => { + const pluginLoader = new PluginLoader(tracer, logger); + assert.strictEqual(pluginLoader['_plugins'].length, 0); + pluginLoader.load({ + plugins: httpPlugins, + sharedPluginOptions: { sharedPluginOption: 1 }, + }); + // The hook is only called the first time the module is loaded. + const httpModule = require('http'); + assert.strictEqual(pluginLoader['_plugins'].length, 1); + assert.strictEqual( + (pluginLoader['_plugins'][0] as any)._config.sharedPluginOption, + 1 + ); + assert.strictEqual(httpModule.get(), 'patched'); + pluginLoader.unload(); + }); + + it('should override shared options with explicit ones', () => { + const pluginLoader = new PluginLoader(tracer, logger); + assert.strictEqual(pluginLoader['_plugins'].length, 0); + pluginLoader.load({ + plugins: httpPlugins, + sharedPluginOptions: { httpPluginOverrideOption: 1 }, + }); + // The hook is only called the first time the module is loaded. + const httpModule = require('http'); + assert.strictEqual(pluginLoader['_plugins'].length, 1); + assert.strictEqual( + (pluginLoader['_plugins'][0] as any)._config.httpPluginOverrideOption, + 2 + ); + assert.strictEqual(httpModule.get(), 'patched'); + pluginLoader.unload(); + }); + + it('should merge shared and explicit options', () => { + const pluginLoader = new PluginLoader(tracer, logger); + assert.strictEqual(pluginLoader['_plugins'].length, 0); + pluginLoader.load({ + plugins: httpPlugins, + sharedPluginOptions: { sharedPluginOption: 1 }, + }); + // The hook is only called the first time the module is loaded. + const httpModule = require('http'); + assert.strictEqual(pluginLoader['_plugins'].length, 1); + assert.deepStrictEqual( + (pluginLoader['_plugins'][0] as any)._config, + { + sharedPluginOption: 1, + httpPluginOverrideOption: 2 + } + ); + assert.strictEqual(httpModule.get(), 'patched'); + pluginLoader.unload(); + }); // @TODO: simplify this test once we can load module with custom path it('should not load the plugin when supported versions does not match', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(notSupportedVersionPlugins); + pluginLoader.load({ plugins: notSupportedVersionPlugins }); // The hook is only called the first time the module is loaded. require('notsupported-module'); assert.strictEqual(pluginLoader['_plugins'].length, 0); @@ -173,7 +229,7 @@ describe('PluginLoader', () => { it('should load a plugin and patch the target modules when supported versions match', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(supportedVersionPlugins); + pluginLoader.load({ plugins: supportedVersionPlugins }); // The hook is only called the first time the module is loaded. const simpleModule = require('supported-module'); assert.strictEqual(pluginLoader['_plugins'].length, 1); @@ -185,7 +241,7 @@ describe('PluginLoader', () => { it('should not load a plugin when value is false', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(disablePlugins); + pluginLoader.load({ plugins: disablePlugins }); const simpleModule = require('simple-module'); assert.strictEqual(pluginLoader['_plugins'].length, 0); assert.strictEqual(simpleModule.value(), 0); @@ -196,7 +252,7 @@ describe('PluginLoader', () => { it('should not load a plugin when value is true but path is missing', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(missingPathPlugins); + pluginLoader.load({ plugins: missingPathPlugins }); const simpleModule = require('simple-module'); assert.strictEqual(pluginLoader['_plugins'].length, 0); assert.strictEqual(simpleModule.value(), 0); @@ -207,14 +263,14 @@ describe('PluginLoader', () => { it('should not load a non existing plugin', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(nonexistentPlugins); + pluginLoader.load({ plugins: nonexistentPlugins }); assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.unload(); }); it(`doesn't patch modules for which plugins aren't specified`, () => { const pluginLoader = new PluginLoader(tracer, logger); - pluginLoader.load({}); + pluginLoader.load({ plugins: {} }); assert.strictEqual(require('simple-module').value(), 0); pluginLoader.unload(); }); @@ -224,7 +280,7 @@ describe('PluginLoader', () => { it('should unload the plugins and unpatch the target module when unloads', () => { const pluginLoader = new PluginLoader(tracer, logger); assert.strictEqual(pluginLoader['_plugins'].length, 0); - pluginLoader.load(simplePlugins); + pluginLoader.load({ plugins: simplePlugins }); // The hook is only called the first time the module is loaded. const simpleModule = require('simple-module'); assert.strictEqual(pluginLoader['_plugins'].length, 1); diff --git a/packages/opentelemetry-plugin-dns/README.md b/packages/opentelemetry-plugin-dns/README.md index 6f0c28432c..f5286f05f7 100644 --- a/packages/opentelemetry-plugin-dns/README.md +++ b/packages/opentelemetry-plugin-dns/README.md @@ -27,8 +27,11 @@ const tracer = new NodeTracer({ // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-dns', // dns plugin options + options: {}, } - } + }, + // shared plugin options + sharedPluginOptions: {} }); ``` diff --git a/packages/opentelemetry-plugin-http/README.md b/packages/opentelemetry-plugin-http/README.md index 73b5d4bad9..c035faddcb 100644 --- a/packages/opentelemetry-plugin-http/README.md +++ b/packages/opentelemetry-plugin-http/README.md @@ -31,7 +31,10 @@ const tracer = new NodeTracer({ // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-http', // http plugin options + options: {} } + // shared plugin options + sharedPluginOptions: {} } }); ``` diff --git a/packages/opentelemetry-plugin-https/README.md b/packages/opentelemetry-plugin-https/README.md index c8aff10265..908b1036a7 100644 --- a/packages/opentelemetry-plugin-https/README.md +++ b/packages/opentelemetry-plugin-https/README.md @@ -31,7 +31,10 @@ const tracer = new NodeTracer({ // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-https', // https plugin options + options: {} } + // shared plugin options + sharedPluginOptions: {} } }); ``` diff --git a/packages/opentelemetry-plugin-mongodb-core/README.md b/packages/opentelemetry-plugin-mongodb-core/README.md index d4da15c5d4..3a23141a4f 100644 --- a/packages/opentelemetry-plugin-mongodb-core/README.md +++ b/packages/opentelemetry-plugin-mongodb-core/README.md @@ -29,7 +29,11 @@ const tracer = new NodeTracer({ enabled: true, // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-mongodb-core', + // mongodb-core plugin options + options: {} } + // shared plugin options + sharedPluginOptions: {} } }); ``` diff --git a/packages/opentelemetry-plugin-redis/README.md b/packages/opentelemetry-plugin-redis/README.md index 9c0a44ef44..17f3bdb0bb 100644 --- a/packages/opentelemetry-plugin-redis/README.md +++ b/packages/opentelemetry-plugin-redis/README.md @@ -32,7 +32,11 @@ const tracer = new NodeTracer({ enabled: true, // You may use a package name or absolute path to the file. path: '@opentelemetry/plugin-redis', + // redis plugin options + options: {} } + // shared plugin options + sharedPluginOptions: {} } }); ``` diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index efc63f8edf..ff8222e6ab 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -18,7 +18,6 @@ import { Tracer } from '../tracer'; import { Logger } from '../../common/Logger'; /** Interface Plugin to apply patch. */ -// tslint:disable-next-line:no-any export interface Plugin { /** * Contains all supported versions. @@ -40,7 +39,7 @@ export interface Plugin { moduleExports: T, tracer: Tracer, logger: Logger, - config?: PluginConfig + config?: PluginOptions ): T; /** Method to disable the instrumentation */ @@ -61,29 +60,31 @@ export interface PluginConfig { path?: string; /** - * Request methods that match any string in ignoreMethods will not be traced. + * Configuration options for an individual plugin */ - ignoreMethods?: string[]; + options?: PluginOptions; +} +export interface PluginOptions { /** - * URLs that partially match any regex in ignoreUrls will not be traced. - * In addition, URLs that are _exact matches_ of strings in ignoreUrls will - * also not be traced. + * If true, additional information about query parameters and + * results will be attached (as `attributes`) to spans representing + * database operations. */ - ignoreUrls?: Array; + enhancedDatabaseReporting?: boolean; /** - * List of internal files that need patch and are not exported by - * default. + * URLs that match any regex in ignoreUrls will not be traced. + * In addition, URLs that are _exact matches_ of strings in ignoreUrls will + * also not be traced. */ - internalFilesExports?: PluginInternalFiles; + ignoreUrls?: Array; /** - * If true, additional information about query parameters and - * results will be attached (as `attributes`) to spans representing - * database operations. + * This is an untyped configuration section where plugin authors + * can define their own custom configuration options */ - enhancedDatabaseReporting?: boolean; + [key: string]: any; } export interface PluginInternalFilesVersion { From d14a6b94cbc7a20aaeadcf8e651205aac5505d4d Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 16 Dec 2019 15:15:35 -0500 Subject: [PATCH 02/15] chore: revert required config option --- .../src/trace/instrumentation/BasePlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts index 67cf630bf2..0e1c7d59fe 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts @@ -43,13 +43,13 @@ export abstract class BasePlugin implements Plugin { moduleExports: T, tracer: Tracer, logger: Logger, - config: PluginOptions + config?: PluginOptions ): T { this._moduleExports = moduleExports; this._tracer = tracer; this._logger = logger; this._internalFilesExports = this._loadInternalFilesExports(); - this._config = config; + this._config = config || {}; return this.patch(); } From 2c05f069890c7aa8a59194f47f8a95ef5b8da2ce Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 16 Dec 2019 16:03:31 -0500 Subject: [PATCH 03/15] fix: lint --- .../test/instrumentation/PluginLoader.test.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index 57c7290e28..c67d4f19e6 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -205,13 +205,10 @@ describe('PluginLoader', () => { // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); - assert.deepStrictEqual( - (pluginLoader['_plugins'][0] as any)._config, - { - sharedPluginOption: 1, - httpPluginOverrideOption: 2 - } - ); + assert.deepStrictEqual((pluginLoader['_plugins'][0] as any)._config, { + sharedPluginOption: 1, + httpPluginOverrideOption: 2, + }); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); }); From 0014f5be76aeb1bb232645ccd4b007e67999170a Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 18 Dec 2019 13:57:40 -0500 Subject: [PATCH 04/15] chore: add typing for dns options --- examples/dns/setup.js | 4 ++++ packages/opentelemetry-plugin-dns/src/dns.ts | 5 ++--- packages/opentelemetry-plugin-dns/src/types.ts | 6 ------ packages/opentelemetry-plugin-dns/src/utils.ts | 7 +++---- .../test/functionals/utils.test.ts | 3 +-- .../src/trace/instrumentation/Plugin.ts | 9 +++++++++ 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/examples/dns/setup.js b/examples/dns/setup.js index 2da1601a87..71e0e917f8 100644 --- a/examples/dns/setup.js +++ b/examples/dns/setup.js @@ -13,6 +13,10 @@ function setupTracerAndExporters(service) { dns: { enabled: true, path: '@opentelemetry/plugin-dns', + options: { + // Avoid dns lookup loop with http zipkin calls + ignoreHostnames: ['localhost'] + } } } }); diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index 11eac18399..c78d3fb796 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -18,14 +18,13 @@ import * as shimmer from 'shimmer'; import * as semver from 'semver'; import * as utils from './utils'; import { BasePlugin } from '@opentelemetry/core'; -import { SpanOptions, SpanKind, Span } from '@opentelemetry/types'; +import { PluginOptions, SpanOptions, SpanKind, Span } from '@opentelemetry/types'; import { Dns, LookupPromiseSignature, LookupFunction, LookupFunctionSignature, LookupCallbackSignature, - DnsPluginConfig, } from './types'; import { AttributeNames } from './enums/AttributeNames'; import { AddressFamily } from './enums/AddressFamily'; @@ -36,7 +35,7 @@ import { LookupAddress } from 'dns'; */ export class DnsPlugin extends BasePlugin { readonly component: string; - protected _config!: DnsPluginConfig; + protected _config!: PluginOptions; constructor(readonly moduleName: string, readonly version: string) { super(); diff --git a/packages/opentelemetry-plugin-dns/src/types.ts b/packages/opentelemetry-plugin-dns/src/types.ts index 0f84de4f60..10c32325ce 100644 --- a/packages/opentelemetry-plugin-dns/src/types.ts +++ b/packages/opentelemetry-plugin-dns/src/types.ts @@ -15,12 +15,9 @@ */ import * as dns from 'dns'; -import { PluginConfig } from '@opentelemetry/types'; export type Dns = typeof dns; -export type IgnoreMatcher = string | RegExp | ((url: string) => boolean); - export type LookupFunction = (( hostname: string, family: number, @@ -95,6 +92,3 @@ export type LookupCallbackSignature = LookupSimpleCallback & family: number ) => void); -export interface DnsPluginConfig extends PluginConfig { - ignoreHostnames?: IgnoreMatcher[]; -} diff --git a/packages/opentelemetry-plugin-dns/src/utils.ts b/packages/opentelemetry-plugin-dns/src/utils.ts index d2d1b2ce84..ac8d2cb6c9 100644 --- a/packages/opentelemetry-plugin-dns/src/utils.ts +++ b/packages/opentelemetry-plugin-dns/src/utils.ts @@ -14,11 +14,10 @@ * limitations under the License. */ -import { Span, CanonicalCode, Status, Attributes } from '@opentelemetry/types'; -import { AttributeNames } from './enums/AttributeNames'; -import { AddressFamily } from './enums/AddressFamily'; +import { Attributes, CanonicalCode, IgnoreMatcher, Span, Status } from '@opentelemetry/types'; import * as dns from 'dns'; -import { IgnoreMatcher } from './types'; +import { AddressFamily } from './enums/AddressFamily'; +import { AttributeNames } from './enums/AttributeNames'; /** * Set error attributes on the span passed in params diff --git a/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts index 8bda076750..8335b7dc9c 100644 --- a/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts @@ -16,8 +16,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { CanonicalCode, SpanKind } from '@opentelemetry/types'; -import { IgnoreMatcher } from '../../src/types'; +import { CanonicalCode, IgnoreMatcher, SpanKind } from '@opentelemetry/types'; import * as utils from '../../src/utils'; import { Span, BasicTracer } from '@opentelemetry/tracing'; import { NoopLogger } from '@opentelemetry/core'; diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index ff8222e6ab..9eba3486c2 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -65,6 +65,8 @@ export interface PluginConfig { options?: PluginOptions; } +export type IgnoreMatcher = string | RegExp | ((url: string) => boolean); + export interface PluginOptions { /** * If true, additional information about query parameters and @@ -80,6 +82,13 @@ export interface PluginOptions { */ ignoreUrls?: Array; + /** + * Used by dns plugin. Ignores tracing for host names which match one of + * the configured matchers by either being an exact string match, matching + * a regular expression, or evaluating to true. + */ + ignoreHostnames?: IgnoreMatcher[]; + /** * This is an untyped configuration section where plugin authors * can define their own custom configuration options From 1a39ec5afeb5ab2d38b5b70a0697856bd63d517b Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 18 Dec 2019 14:16:39 -0500 Subject: [PATCH 05/15] fix: lint --- packages/opentelemetry-plugin-dns/src/dns.ts | 7 ++++++- packages/opentelemetry-plugin-dns/src/types.ts | 1 - packages/opentelemetry-plugin-dns/src/utils.ts | 8 +++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index c78d3fb796..f402669713 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -18,7 +18,12 @@ import * as shimmer from 'shimmer'; import * as semver from 'semver'; import * as utils from './utils'; import { BasePlugin } from '@opentelemetry/core'; -import { PluginOptions, SpanOptions, SpanKind, Span } from '@opentelemetry/types'; +import { + PluginOptions, + SpanOptions, + SpanKind, + Span, +} from '@opentelemetry/types'; import { Dns, LookupPromiseSignature, diff --git a/packages/opentelemetry-plugin-dns/src/types.ts b/packages/opentelemetry-plugin-dns/src/types.ts index 10c32325ce..940c517413 100644 --- a/packages/opentelemetry-plugin-dns/src/types.ts +++ b/packages/opentelemetry-plugin-dns/src/types.ts @@ -91,4 +91,3 @@ export type LookupCallbackSignature = LookupSimpleCallback & address: string | dns.LookupAddress[], family: number ) => void); - diff --git a/packages/opentelemetry-plugin-dns/src/utils.ts b/packages/opentelemetry-plugin-dns/src/utils.ts index ac8d2cb6c9..f1388741a7 100644 --- a/packages/opentelemetry-plugin-dns/src/utils.ts +++ b/packages/opentelemetry-plugin-dns/src/utils.ts @@ -14,7 +14,13 @@ * limitations under the License. */ -import { Attributes, CanonicalCode, IgnoreMatcher, Span, Status } from '@opentelemetry/types'; +import { + Attributes, + CanonicalCode, + IgnoreMatcher, + Span, + Status, +} from '@opentelemetry/types'; import * as dns from 'dns'; import { AddressFamily } from './enums/AddressFamily'; import { AttributeNames } from './enums/AttributeNames'; From 9cc8d8451240c651a419d23c061544e447a36e9c Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 30 Dec 2019 17:10:41 -0500 Subject: [PATCH 06/15] chore: refactor config to share a single object shape --- examples/dns/setup.js | 6 +- packages/opentelemetry-node/README.md | 2 +- packages/opentelemetry-node/src/config.ts | 4 +- .../src/instrumentation/PluginLoader.ts | 14 ++-- .../src/instrumentation/utils.ts | 34 ++++++++- .../test/NodeTracer.test.ts | 8 ++- .../test/instrumentation/PluginLoader.test.ts | 26 +++---- packages/opentelemetry-plugin-dns/README.md | 2 +- packages/opentelemetry-plugin-dns/src/dns.ts | 2 +- .../src/documentLoad.ts | 13 ++-- .../test/documentLoad.test.ts | 17 ++--- packages/opentelemetry-plugin-http/README.md | 2 +- .../opentelemetry-plugin-http/src/http.ts | 43 +++-------- .../opentelemetry-plugin-http/src/types.ts | 25 +------ .../opentelemetry-plugin-http/src/utils.ts | 14 ++-- .../test/functionals/http-enable.test.ts | 71 +++++++++--------- .../test/functionals/http-package.test.ts | 19 ++--- .../test/functionals/utils.test.ts | 15 ++-- .../test/integrations/http-enable.test.ts | 26 ++++--- packages/opentelemetry-plugin-https/README.md | 2 +- .../test/functionals/https-enable.test.ts | 72 +++++++++---------- .../test/functionals/https-package.test.ts | 41 ++++++----- .../test/integrations/https-enable.test.ts | 27 ++++--- .../README.md | 2 +- packages/opentelemetry-plugin-redis/README.md | 2 +- .../src/trace/instrumentation/Plugin.ts | 61 ++++++++++++---- 26 files changed, 275 insertions(+), 275 deletions(-) diff --git a/examples/dns/setup.js b/examples/dns/setup.js index 71e0e917f8..04a4ada0b3 100644 --- a/examples/dns/setup.js +++ b/examples/dns/setup.js @@ -14,8 +14,10 @@ function setupTracerAndExporters(service) { enabled: true, path: '@opentelemetry/plugin-dns', options: { - // Avoid dns lookup loop with http zipkin calls - ignoreHostnames: ['localhost'] + dns: { + // Avoid dns lookup loop with http zipkin calls + ignoreHostnames: ['localhost'] + } } } } diff --git a/packages/opentelemetry-node/README.md b/packages/opentelemetry-node/README.md index b38ef8c528..022e119032 100644 --- a/packages/opentelemetry-node/README.md +++ b/packages/opentelemetry-node/README.md @@ -68,7 +68,7 @@ const tracer = new NodeTracer({ } }, // shared plugin options - sharedPluginOptions: {} + options: {} }); // Initialize the tracer diff --git a/packages/opentelemetry-node/src/config.ts b/packages/opentelemetry-node/src/config.ts index 0c4288a74b..08d1d681eb 100644 --- a/packages/opentelemetry-node/src/config.ts +++ b/packages/opentelemetry-node/src/config.ts @@ -26,13 +26,13 @@ export interface NodeTracerConfig extends BasicTracerConfig { plugins?: Plugins; /** Plugin options applied to all plugins */ - sharedPluginOptions?: PluginOptions; + options?: PluginOptions; } /** List of all default supported plugins */ export const DEFAULT_INSTRUMENTATION_PLUGINS: Plugins = { 'mongodb-core': { enabled: true, path: '@opentelemetry/plugin-mongodb-core' }, - dns: { enabled: true, path: '@opentelemetry/plugin-dns' }, + dns: { enabled: true, path: '@opentelemetry/plugin-dns', options: {} }, grpc: { enabled: true, path: '@opentelemetry/plugin-grpc' }, http: { enabled: true, path: '@opentelemetry/plugin-http' }, https: { enabled: true, path: '@opentelemetry/plugin-https' }, diff --git a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts index 361c7fe9fe..6229aa6f85 100644 --- a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts +++ b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts @@ -90,11 +90,10 @@ export class PluginLoader { plugins[pluginModuleName] = { path: config.path || defaultConfig.path, - enabled: typeof enabled === 'boolean' ? enabled : true, - options: Object.assign( - {}, + enabled: enabled !== false, + options: utils.mergeOptions( defaultConfig.options, - tracerConfig.sharedPluginOptions, + tracerConfig.options, config.options ), }; @@ -147,14 +146,9 @@ export class PluginLoader { return exports; } - const options = Object.assign( - {}, - tracerConfig.sharedPluginOptions, - config.options - ); this._plugins.push(plugin); // Enable each supported plugin. - return plugin.enable(exports, this.tracer, this.logger, options); + return plugin.enable(exports, this.tracer, this.logger, config.options); } catch (e) { this.logger.error( `PluginLoader#load: could not load plugin ${modulePath} of module ${name}. Error: ${e.message}` diff --git a/packages/opentelemetry-node/src/instrumentation/utils.ts b/packages/opentelemetry-node/src/instrumentation/utils.ts index 9460de4ad8..4e8cea3a45 100644 --- a/packages/opentelemetry-node/src/instrumentation/utils.ts +++ b/packages/opentelemetry-node/src/instrumentation/utils.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Logger } from '@opentelemetry/types'; +import { Logger, PluginOptions } from '@opentelemetry/types'; import * as path from 'path'; import * as semver from 'semver'; @@ -72,3 +72,35 @@ export function isSupportedVersion( export function searchPathForTest(searchPath: string) { module.paths.push(searchPath); } + +/** + * Merge plugin options. Rightmost options take precedence. + * + * @param options config objects to merge + */ +export function mergeOptions( + ...options: (PluginOptions | undefined)[] +): PluginOptions { + const out: PluginOptions = {}; + for (const config of options) { + if (!config) { + continue; + } + for (const type of keys(config)) { + for (const option of keys(config[type])) { + const value = config[type]![option]!; + + if (!out[type]) { + out[type] = {}; + } + + out[type]![option] = value; + } + } + } + return out; +} + +function keys(obj: T): Array { + return Object.keys(obj) as Array; +} diff --git a/packages/opentelemetry-node/test/NodeTracer.test.ts b/packages/opentelemetry-node/test/NodeTracer.test.ts index d829d0ac52..d7bfa52775 100644 --- a/packages/opentelemetry-node/test/NodeTracer.test.ts +++ b/packages/opentelemetry-node/test/NodeTracer.test.ts @@ -97,8 +97,12 @@ describe('NodeTracer', () => { enabled: true, path: '@opentelemetry/plugin-supported-module', options: { - enhancedDatabaseReporting: false, - ignoreUrls: [], + database: { + enhancedDatabaseReporting: false, + }, + http: { + ignoreOutgoingUrls: [], + } }, }, }, diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index c67d4f19e6..8cefe70e61 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -38,7 +38,9 @@ const httpPlugins: Plugins = { enabled: true, path: '@opentelemetry/plugin-http-module', options: { - httpPluginOverrideOption: 2, + http: { + ignoreOutgoingUrls: ["plugin specific option"] + }, }, }, }; @@ -164,14 +166,14 @@ describe('PluginLoader', () => { assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.load({ plugins: httpPlugins, - sharedPluginOptions: { sharedPluginOption: 1 }, + options: { dns: { ignoreHostnames: ['shared option'] } }, }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); - assert.strictEqual( - (pluginLoader['_plugins'][0] as any)._config.sharedPluginOption, - 1 + assert.deepStrictEqual( + (pluginLoader['_plugins'][0] as any)._config.dns.ignoreHostnames, + ['shared option'] ); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); @@ -182,14 +184,14 @@ describe('PluginLoader', () => { assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.load({ plugins: httpPlugins, - sharedPluginOptions: { httpPluginOverrideOption: 1 }, + options: { http: { ignoreOutgoingUrls: ["shared"] } }, }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); - assert.strictEqual( - (pluginLoader['_plugins'][0] as any)._config.httpPluginOverrideOption, - 2 + assert.deepStrictEqual( + (pluginLoader['_plugins'][0] as any)._config.http.ignoreOutgoingUrls, + ["plugin specific option"] ); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); @@ -200,14 +202,14 @@ describe('PluginLoader', () => { assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.load({ plugins: httpPlugins, - sharedPluginOptions: { sharedPluginOption: 1 }, + options: { dns: { ignoreHostnames: ["shared"] } }, }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.deepStrictEqual((pluginLoader['_plugins'][0] as any)._config, { - sharedPluginOption: 1, - httpPluginOverrideOption: 2, + dns: { ignoreHostnames: ["shared"] }, + http: { ignoreOutgoingUrls: ["plugin specific option"] }, }); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); diff --git a/packages/opentelemetry-plugin-dns/README.md b/packages/opentelemetry-plugin-dns/README.md index f5286f05f7..98924bd108 100644 --- a/packages/opentelemetry-plugin-dns/README.md +++ b/packages/opentelemetry-plugin-dns/README.md @@ -31,7 +31,7 @@ const tracer = new NodeTracer({ } }, // shared plugin options - sharedPluginOptions: {} + options: {} }); ``` diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index f402669713..d742e41ff4 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -109,7 +109,7 @@ export class DnsPlugin extends BasePlugin { ...args: unknown[] ) { if ( - utils.isIgnored(hostname, plugin._config.ignoreHostnames, (e: Error) => + utils.isIgnored(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames, (e: Error) => plugin._logger.error('caught ignoreHostname error: ', e) ) ) { diff --git a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts index 4ca569a54d..966b9753e9 100644 --- a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -14,13 +14,8 @@ * limitations under the License. */ -import { - BasePlugin, - otperformance, - parseTraceParent, - TRACE_PARENT_HEADER, -} from '@opentelemetry/core'; -import { PluginConfig, Span, SpanOptions } from '@opentelemetry/types'; +import { BasePlugin, otperformance, parseTraceParent, TRACE_PARENT_HEADER } from '@opentelemetry/core'; +import { PluginOptions, Span, SpanOptions } from '@opentelemetry/types'; import { AttributeNames } from './enums/AttributeNames'; import { PerformanceTimingNames as PTN } from './enums/PerformanceTimingNames'; import { PerformanceEntries, PerformanceLegacy } from './types'; @@ -33,13 +28,13 @@ export class DocumentLoad extends BasePlugin { readonly component: string = 'document-load'; readonly version: string = '1'; moduleName = this.component; - protected _config!: PluginConfig; + protected _config!: PluginOptions; /** * * @param config */ - constructor(config: PluginConfig = {}) { + constructor(config: PluginOptions = {}) { super(); this._onDocumentLoaded = this._onDocumentLoaded.bind(this); this._config = config; diff --git a/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts b/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts index cb17ceb566..79d2502df3 100644 --- a/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts +++ b/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts @@ -18,22 +18,17 @@ * Can't use Sinon Fake Time here as then cannot stub the performance getEntriesByType with desired metrics */ +import { ConsoleLogger, TRACE_PARENT_HEADER } from '@opentelemetry/core'; +import { BasicTracer, ReadableSpan, SimpleSpanProcessor, SpanExporter } from '@opentelemetry/tracing'; +import { Logger, PluginOptions, TimedEvent } from '@opentelemetry/types'; import * as assert from 'assert'; import * as sinon from 'sinon'; - -import { ConsoleLogger, TRACE_PARENT_HEADER } from '@opentelemetry/core'; -import { - BasicTracer, - ReadableSpan, - SimpleSpanProcessor, - SpanExporter, -} from '@opentelemetry/tracing'; -import { Logger, PluginConfig, TimedEvent } from '@opentelemetry/types'; - import { ExportResult } from '../../opentelemetry-base/build/src'; import { DocumentLoad } from '../src'; import { PerformanceTimingNames as PTN } from '../src/enums/PerformanceTimingNames'; + + export class DummyExporter implements SpanExporter { export( spans: ReadableSpan[], @@ -195,7 +190,7 @@ describe('DocumentLoad Plugin', () => { let moduleExports: any; let tracer: BasicTracer; let logger: Logger; - let config: PluginConfig; + let config: PluginOptions; let spanProcessor: SimpleSpanProcessor; let dummyExporter: DummyExporter; diff --git a/packages/opentelemetry-plugin-http/README.md b/packages/opentelemetry-plugin-http/README.md index c035faddcb..0cda1105b5 100644 --- a/packages/opentelemetry-plugin-http/README.md +++ b/packages/opentelemetry-plugin-http/README.md @@ -34,7 +34,7 @@ const tracer = new NodeTracer({ options: {} } // shared plugin options - sharedPluginOptions: {} + options: {} } }); ``` diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 828a75f989..18f79cd311 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -15,35 +15,14 @@ */ import { BasePlugin, isValid } from '@opentelemetry/core'; -import { - Span, - SpanKind, - SpanOptions, - Attributes, - CanonicalCode, - Status, -} from '@opentelemetry/types'; -import { - ClientRequest, - IncomingMessage, - request, - RequestOptions, - ServerResponse, -} from 'http'; +import { Attributes, CanonicalCode, PluginOptions, Span, SpanKind, SpanOptions, Status } from '@opentelemetry/types'; +import { ClientRequest, IncomingMessage, request, RequestOptions, ServerResponse } from 'http'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; import * as url from 'url'; -import { - HttpPluginConfig, - Http, - Func, - ResponseEndArgs, - ParsedRequestOptions, - HttpRequestArgs, - Err, -} from './types'; -import { Format } from './enums/Format'; import { AttributeNames } from './enums/AttributeNames'; +import { Format } from './enums/Format'; +import { Err, Func, Http, HttpRequestArgs, ParsedRequestOptions, ResponseEndArgs } from './types'; import * as utils from './utils'; /** @@ -51,7 +30,7 @@ import * as utils from './utils'; */ export class HttpPlugin extends BasePlugin { readonly component: string; - protected _config!: HttpPluginConfig; + protected _config!: PluginOptions; /** keep track on spans not ended */ private readonly _spanNotEnded: WeakSet; @@ -229,11 +208,11 @@ export class HttpPlugin extends BasePlugin { span.setStatus(status); - if (this._config.applyCustomAttributesOnSpan) { + if (this._config.http && this._config.http.applyCustomAttributesOnSpan) { this._safeExecute( span, () => - this._config.applyCustomAttributesOnSpan!( + this._config.http!.applyCustomAttributesOnSpan!( span, request, response @@ -291,7 +270,7 @@ export class HttpPlugin extends BasePlugin { if ( utils.isIgnored( pathname, - plugin._config.ignoreIncomingPaths, + plugin._config.http && plugin._config.http.ignoreIncomingPaths, (e: Error) => plugin._logger.error('caught ignoreIncomingPaths error: ', e) ) @@ -363,11 +342,11 @@ export class HttpPlugin extends BasePlugin { .setAttributes(attributes) .setStatus(utils.parseResponseStatus(response.statusCode)); - if (plugin._config.applyCustomAttributesOnSpan) { + if (plugin._config.http && plugin._config.http.applyCustomAttributesOnSpan) { plugin._safeExecute( span, () => - plugin._config.applyCustomAttributesOnSpan!( + plugin._config.http!.applyCustomAttributesOnSpan!( span, request, response @@ -418,7 +397,7 @@ export class HttpPlugin extends BasePlugin { utils.isOpenTelemetryRequest(options) || utils.isIgnored( origin + pathname, - plugin._config.ignoreOutgoingUrls, + plugin._config.http && plugin._config.http.ignoreOutgoingUrls, (e: Error) => plugin._logger.error('caught ignoreOutgoingUrls error: ', e) ) diff --git a/packages/opentelemetry-plugin-http/src/types.ts b/packages/opentelemetry-plugin-http/src/types.ts index e55c358348..6377701201 100644 --- a/packages/opentelemetry-plugin-http/src/types.ts +++ b/packages/opentelemetry-plugin-http/src/types.ts @@ -14,18 +14,10 @@ * limitations under the License. */ -import { Span, PluginConfig } from '@opentelemetry/types'; -import * as url from 'url'; -import { - ClientRequest, - IncomingMessage, - ServerResponse, - request, - get, -} from 'http'; import * as http from 'http'; +import { get, IncomingMessage, request } from 'http'; +import * as url from 'url'; -export type IgnoreMatcher = string | RegExp | ((url: string) => boolean); export type HttpCallback = (res: IncomingMessage) => void; export type RequestFunction = typeof request; export type GetFunction = typeof get; @@ -49,19 +41,6 @@ export type ResponseEndArgs = | [unknown, ((() => void) | undefined)?] | [unknown, string, ((() => void) | undefined)?]; -export interface HttpCustomAttributeFunction { - ( - span: Span, - request: ClientRequest | IncomingMessage, - response: IncomingMessage | ServerResponse - ): void; -} - -export interface HttpPluginConfig extends PluginConfig { - ignoreIncomingPaths?: IgnoreMatcher[]; - ignoreOutgoingUrls?: IgnoreMatcher[]; - applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; -} export interface Err extends Error { errno?: number; diff --git a/packages/opentelemetry-plugin-http/src/utils.ts b/packages/opentelemetry-plugin-http/src/utils.ts index fafcec338d..2d1e5e440f 100644 --- a/packages/opentelemetry-plugin-http/src/utils.ts +++ b/packages/opentelemetry-plugin-http/src/utils.ts @@ -14,17 +14,11 @@ * limitations under the License. */ -import { Status, CanonicalCode, Span } from '@opentelemetry/types'; -import { - RequestOptions, - IncomingMessage, - ClientRequest, - IncomingHttpHeaders, - OutgoingHttpHeaders, -} from 'http'; -import { IgnoreMatcher, Err, ParsedRequestOptions } from './types'; -import { AttributeNames } from './enums/AttributeNames'; +import { CanonicalCode, IgnoreMatcher, Span, Status } from '@opentelemetry/types'; +import { ClientRequest, IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, RequestOptions } from 'http'; import * as url from 'url'; +import { AttributeNames } from './enums/AttributeNames'; +import { Err, ParsedRequestOptions } from './types'; export const OT_REQUEST_HEADER = 'x-opentelemetry-outgoing-request'; /** diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 979d5622e5..68dfca8426 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -14,24 +14,21 @@ * limitations under the License. */ -import { - InMemorySpanExporter, - SimpleSpanProcessor, -} from '@opentelemetry/tracing'; import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { CanonicalCode, Span as ISpan, SpanKind } from '@opentelemetry/types'; +import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { CanonicalCode, PluginOptions, Span as ISpan, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; -import * as path from 'path'; import * as nock from 'nock'; +import * as path from 'path'; +import { AttributeNames } from '../../src/enums/AttributeNames'; import { HttpPlugin, plugin } from '../../src/http'; +import { Http } from '../../src/types'; +import { OT_REQUEST_HEADER } from '../../src/utils'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; import { httpRequest } from '../utils/httpRequest'; -import { OT_REQUEST_HEADER } from '../../src/utils'; -import { HttpPluginConfig, Http } from '../../src/types'; -import { AttributeNames } from '../../src/enums/AttributeNames'; const applyCustomAttributesOnSpanErrorMessage = 'bad applyCustomAttributesOnSpan function'; @@ -89,20 +86,22 @@ describe('HttpPlugin', () => { }); before(() => { - const config: HttpPluginConfig = { - ignoreIncomingPaths: [ - (url: string) => { - throw new Error('bad ignoreIncomingPaths function'); - }, - ], - ignoreOutgoingUrls: [ - (url: string) => { - throw new Error('bad ignoreOutgoingUrls function'); + const config: PluginOptions = { + http: { + ignoreIncomingPaths: [ + (url: string) => { + throw new Error('bad ignoreIncomingPaths function'); + }, + ], + ignoreOutgoingUrls: [ + (url: string) => { + throw new Error('bad ignoreOutgoingUrls function'); + }, + ], + applyCustomAttributesOnSpan: () => { + throw new Error(applyCustomAttributesOnSpanErrorMessage); }, - ], - applyCustomAttributesOnSpan: () => { - throw new Error(applyCustomAttributesOnSpanErrorMessage); - }, + } }; pluginWithBadOptions = new HttpPlugin( plugin.component, @@ -164,18 +163,20 @@ describe('HttpPlugin', () => { }); before(() => { - const config: HttpPluginConfig = { - ignoreIncomingPaths: [ - `/ignored/string`, - /\/ignored\/regexp$/i, - (url: string) => url.endsWith(`/ignored/function`), - ], - ignoreOutgoingUrls: [ - `${protocol}://${hostname}:${serverPort}/ignored/string`, - /\/ignored\/regexp$/i, - (url: string) => url.endsWith(`/ignored/function`), - ], - applyCustomAttributesOnSpan: customAttributeFunction, + const config: PluginOptions = { + http: { + ignoreIncomingPaths: [ + `/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], + ignoreOutgoingUrls: [ + `${protocol}://${hostname}:${serverPort}/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], + applyCustomAttributesOnSpan: customAttributeFunction, + } }; plugin.enable(http, tracer, tracer.logger, config); server = http.createServer((request, response) => { @@ -608,7 +609,7 @@ describe('HttpPlugin', () => { .reply(404); const req = http.request(`${host}/`); req.on('response', response => { - response.on('data', () => {}); + response.on('data', () => { }); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts index 4976e88a33..ebc5aed489 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts @@ -15,7 +15,7 @@ */ import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind } from '@opentelemetry/types'; +import { SpanKind, PluginOptions } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; import * as nock from 'nock'; @@ -34,7 +34,6 @@ import { SimpleSpanProcessor, } from '@opentelemetry/tracing'; -import { HttpPluginConfig } from '../../src/types'; import { customAttributeFunction } from './http-enable.test'; const memoryExporter = new InMemorySpanExporter(); @@ -55,9 +54,11 @@ describe('Packages', () => { }); before(() => { - const config: HttpPluginConfig = { - applyCustomAttributesOnSpan: customAttributeFunction, - }; + const config: PluginOptions = { + http: { + applyCustomAttributesOnSpan: customAttributeFunction, + } + } plugin.enable(http, tracer, tracer.logger, config); }); @@ -91,10 +92,10 @@ describe('Packages', () => { const urlparsed = url.parse( name === 'got' && process.versions.node.startsWith('12') ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://info.cern.ch/` + // https://github.com/nock/nock/pull/1551 + // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 + // TODO: check if this is still the case when new version + `${protocol}://info.cern.ch/` : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); diff --git a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts index 138cccac81..f6f8ba6291 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts @@ -14,17 +14,16 @@ * limitations under the License. */ +import { NoopLogger } from '@opentelemetry/core'; +import { NoopScopeManager } from '@opentelemetry/scope-base'; +import { BasicTracer, Span } from '@opentelemetry/tracing'; +import { CanonicalCode, IgnoreMatcher, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; +import * as http from 'http'; import * as sinon from 'sinon'; import * as url from 'url'; -import { CanonicalCode, SpanKind } from '@opentelemetry/types'; -import { NoopScopeManager } from '@opentelemetry/scope-base'; -import { IgnoreMatcher } from '../../src/types'; -import * as utils from '../../src/utils'; -import * as http from 'http'; -import { Span, BasicTracer } from '@opentelemetry/tracing'; import { AttributeNames } from '../../src'; -import { NoopLogger } from '@opentelemetry/core'; +import * as utils from '../../src/utils'; describe('Utility', () => { describe('parseResponseStatus()', () => { @@ -54,7 +53,7 @@ describe('Utility', () => { try { utils.hasExpectHeader('' as http.RequestOptions); assert.fail(); - } catch (ignore) {} + } catch (ignore) { } }); it('should not throw if no headers', () => { diff --git a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts index 9f3eb84a7a..50f643f42f 100644 --- a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts @@ -15,21 +15,17 @@ */ import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind, Span } from '@opentelemetry/types'; +import { NodeTracer } from '@opentelemetry/node'; +import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { Span, SpanKind, PluginOptions } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; +import * as url from 'url'; import { plugin } from '../../src/http'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; import { httpRequest } from '../utils/httpRequest'; -import * as url from 'url'; import * as utils from '../utils/utils'; -import { NodeTracer } from '@opentelemetry/node'; -import { - InMemorySpanExporter, - SimpleSpanProcessor, -} from '@opentelemetry/tracing'; -import { HttpPluginConfig } from '../../src/types'; const protocol = 'http'; const serverPort = 32345; const hostname = 'localhost'; @@ -41,7 +37,7 @@ export const customAttributeFunction = (span: Span): void => { describe('HttpPlugin Integration tests', () => { describe('enable()', () => { - before(function(done) { + before(function (done) { // mandatory if (process.env.CI) { done(); @@ -74,14 +70,16 @@ describe('HttpPlugin Integration tests', () => { /\/ignored\/regexp$/i, (url: string) => url.endsWith(`/ignored/function`), ]; - const config: HttpPluginConfig = { - ignoreIncomingPaths: ignoreConfig, - ignoreOutgoingUrls: ignoreConfig, - applyCustomAttributesOnSpan: customAttributeFunction, + const config: PluginOptions = { + http: { + ignoreIncomingPaths: ignoreConfig, + ignoreOutgoingUrls: ignoreConfig, + applyCustomAttributesOnSpan: customAttributeFunction, + } }; try { plugin.disable(); - } catch (e) {} + } catch (e) { } plugin.enable(http, tracer, tracer.logger, config); }); diff --git a/packages/opentelemetry-plugin-https/README.md b/packages/opentelemetry-plugin-https/README.md index 908b1036a7..e4284a06ec 100644 --- a/packages/opentelemetry-plugin-https/README.md +++ b/packages/opentelemetry-plugin-https/README.md @@ -34,7 +34,7 @@ const tracer = new NodeTracer({ options: {} } // shared plugin options - sharedPluginOptions: {} + options: {} } }); ``` diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts index 1bfcc94105..e595ae666e 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts @@ -14,25 +14,17 @@ * limitations under the License. */ -import { - InMemorySpanExporter, - SimpleSpanProcessor, -} from '@opentelemetry/tracing'; import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { - Http, - HttpPluginConfig, - OT_REQUEST_HEADER, - AttributeNames, -} from '@opentelemetry/plugin-http'; -import { CanonicalCode, Span as ISpan, SpanKind } from '@opentelemetry/types'; +import { AttributeNames, Http, OT_REQUEST_HEADER } from '@opentelemetry/plugin-http'; +import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { CanonicalCode, PluginOptions, Span as ISpan, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as fs from 'fs'; import * as http from 'http'; import * as https from 'https'; -import * as path from 'path'; import * as nock from 'nock'; +import * as path from 'path'; import { HttpsPlugin, plugin } from '../../src/https'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; @@ -94,20 +86,22 @@ describe('HttpsPlugin', () => { }); before(() => { - const config: HttpPluginConfig = { - ignoreIncomingPaths: [ - (url: string) => { - throw new Error('bad ignoreIncomingPaths function'); - }, - ], - ignoreOutgoingUrls: [ - (url: string) => { - throw new Error('bad ignoreOutgoingUrls function'); + const config: PluginOptions = { + http: { + ignoreIncomingPaths: [ + (url: string) => { + throw new Error('bad ignoreIncomingPaths function'); + }, + ], + ignoreOutgoingUrls: [ + (url: string) => { + throw new Error('bad ignoreOutgoingUrls function'); + }, + ], + applyCustomAttributesOnSpan: () => { + throw new Error(applyCustomAttributesOnSpanErrorMessage); }, - ], - applyCustomAttributesOnSpan: () => { - throw new Error(applyCustomAttributesOnSpanErrorMessage); - }, + } }; pluginWithBadOptions = new HttpsPlugin(process.versions.node); pluginWithBadOptions.enable( @@ -177,18 +171,20 @@ describe('HttpsPlugin', () => { }); before(() => { - const config: HttpPluginConfig = { - ignoreIncomingPaths: [ - `/ignored/string`, - /\/ignored\/regexp$/i, - (url: string) => url.endsWith(`/ignored/function`), - ], - ignoreOutgoingUrls: [ - `${protocol}://${hostname}:${serverPort}/ignored/string`, - /\/ignored\/regexp$/i, - (url: string) => url.endsWith(`/ignored/function`), - ], - applyCustomAttributesOnSpan: customAttributeFunction, + const config: PluginOptions = { + http: { + ignoreIncomingPaths: [ + `/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], + ignoreOutgoingUrls: [ + `${protocol}://${hostname}:${serverPort}/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], + applyCustomAttributesOnSpan: customAttributeFunction, + } }; plugin.enable( (https as unknown) as Http, @@ -620,7 +616,7 @@ describe('HttpsPlugin', () => { .reply(404); const req = https.request(`${host}/`); req.on('response', response => { - response.on('data', () => {}); + response.on('data', () => { }); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts index 811a3e32c5..05b04c3c3a 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts @@ -15,29 +15,26 @@ */ import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind } from '@opentelemetry/types'; +import { NodeTracer } from '@opentelemetry/node'; +import { Http } from '@opentelemetry/plugin-http'; +import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { PluginOptions, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; -import * as https from 'https'; +import axios, { AxiosResponse } from 'axios'; +import * as got from 'got'; import * as http from 'http'; +import * as https from 'https'; import * as nock from 'nock'; +import * as path from 'path'; +import * as request from 'request-promise-native'; +import * as superagent from 'superagent'; +import * as url from 'url'; import { plugin } from '../../src/https'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; -import * as url from 'url'; -import axios, { AxiosResponse } from 'axios'; -import * as superagent from 'superagent'; -import * as got from 'got'; -import * as request from 'request-promise-native'; -import * as path from 'path'; -import { NodeTracer } from '@opentelemetry/node'; -import { - InMemorySpanExporter, - SimpleSpanProcessor, -} from '@opentelemetry/tracing'; - -import { Http, HttpPluginConfig } from '@opentelemetry/plugin-http'; import { customAttributeFunction } from './https-enable.test'; + const memoryExporter = new InMemorySpanExporter(); const protocol = 'https'; @@ -56,8 +53,10 @@ describe('Packages', () => { }); before(() => { - const config: HttpPluginConfig = { - applyCustomAttributesOnSpan: customAttributeFunction, + const config: PluginOptions = { + http: { + applyCustomAttributesOnSpan: customAttributeFunction, + } }; plugin.enable((https as unknown) as Http, tracer, tracer.logger, config); }); @@ -92,10 +91,10 @@ describe('Packages', () => { const urlparsed = url.parse( name === 'got' && process.versions.node.startsWith('12') ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://www.google.com` + // https://github.com/nock/nock/pull/1551 + // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 + // TODO: check if this is still the case when new version + `${protocol}://www.google.com` : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); diff --git a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts index 4c2ca192d2..8a63cc6ab9 100644 --- a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts @@ -15,22 +15,19 @@ */ import { NoopLogger } from '@opentelemetry/core'; -import { HttpPluginConfig, Http } from '@opentelemetry/plugin-http'; -import { SpanKind, Span } from '@opentelemetry/types'; +import { NodeTracer } from '@opentelemetry/node'; +import { Http } from '@opentelemetry/plugin-http'; +import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { PluginOptions, Span, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; import * as https from 'https'; +import * as url from 'url'; import { plugin } from '../../src/https'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; import { httpsRequest } from '../utils/httpsRequest'; -import * as url from 'url'; import * as utils from '../utils/utils'; -import { NodeTracer } from '@opentelemetry/node'; -import { - InMemorySpanExporter, - SimpleSpanProcessor, -} from '@opentelemetry/tracing'; const protocol = 'https'; const serverPort = 42345; @@ -43,7 +40,7 @@ export const customAttributeFunction = (span: Span): void => { describe('HttpsPlugin Integration tests', () => { describe('enable()', () => { - before(function(done) { + before(function (done) { // mandatory if (process.env.CI) { done(); @@ -76,14 +73,16 @@ describe('HttpsPlugin Integration tests', () => { /\/ignored\/regexp$/i, (url: string) => url.endsWith(`/ignored/function`), ]; - const config: HttpPluginConfig = { - ignoreIncomingPaths: ignoreConfig, - ignoreOutgoingUrls: ignoreConfig, - applyCustomAttributesOnSpan: customAttributeFunction, + const config: PluginOptions = { + http: { + ignoreIncomingPaths: ignoreConfig, + ignoreOutgoingUrls: ignoreConfig, + applyCustomAttributesOnSpan: customAttributeFunction, + } }; try { plugin.disable(); - } catch (e) {} + } catch (e) { } plugin.enable((https as unknown) as Http, tracer, tracer.logger, config); }); diff --git a/packages/opentelemetry-plugin-mongodb-core/README.md b/packages/opentelemetry-plugin-mongodb-core/README.md index 3a23141a4f..4f0b351890 100644 --- a/packages/opentelemetry-plugin-mongodb-core/README.md +++ b/packages/opentelemetry-plugin-mongodb-core/README.md @@ -33,7 +33,7 @@ const tracer = new NodeTracer({ options: {} } // shared plugin options - sharedPluginOptions: {} + options: {} } }); ``` diff --git a/packages/opentelemetry-plugin-redis/README.md b/packages/opentelemetry-plugin-redis/README.md index 17f3bdb0bb..e29664f6d1 100644 --- a/packages/opentelemetry-plugin-redis/README.md +++ b/packages/opentelemetry-plugin-redis/README.md @@ -36,7 +36,7 @@ const tracer = new NodeTracer({ options: {} } // shared plugin options - sharedPluginOptions: {} + options: {} } }); ``` diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index 9eba3486c2..4743ff5bfa 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -14,8 +14,11 @@ * limitations under the License. */ -import { Tracer } from '../tracer'; +import { ClientRequest, IncomingMessage, ServerResponse } from "http"; import { Logger } from '../../common/Logger'; +import { Span } from '../span'; +import { Tracer } from '../tracer'; + /** Interface Plugin to apply patch. */ export interface Plugin { @@ -60,40 +63,68 @@ export interface PluginConfig { path?: string; /** - * Configuration options for an individual plugin + * These plugin options override the values provided in the + * shared plugin options section. */ options?: PluginOptions; } export type IgnoreMatcher = string | RegExp | ((url: string) => boolean); -export interface PluginOptions { +/** + * These options are used by database plugins like mysql, pg, and mongodb. + */ +export interface DatabasePluginOptions { /** * If true, additional information about query parameters and * results will be attached (as `attributes`) to spans representing * database operations. */ enhancedDatabaseReporting?: boolean; +} - /** - * URLs that match any regex in ignoreUrls will not be traced. - * In addition, URLs that are _exact matches_ of strings in ignoreUrls will - * also not be traced. - */ - ignoreUrls?: Array; - +/** + * These options are used by dns module plugins. + */ +export interface DNSPluginOptions { /** * Used by dns plugin. Ignores tracing for host names which match one of * the configured matchers by either being an exact string match, matching * a regular expression, or evaluating to true. */ ignoreHostnames?: IgnoreMatcher[]; +} - /** - * This is an untyped configuration section where plugin authors - * can define their own custom configuration options - */ - [key: string]: any; +export interface HttpCustomAttributeFunction { + ( + span: Span, + request: ClientRequest | IncomingMessage, + response: IncomingMessage | ServerResponse + ): void; +} + +/** + * These options are used by http plugins like http, https, and http2. + */ +export interface HttpPluginOptions { + ignoreIncomingPaths?: IgnoreMatcher[]; + ignoreOutgoingUrls?: IgnoreMatcher[]; + applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; +} + +/** + * This is a configuration section where plugin authors + * can define their own custom configuration options. + */ +export interface CustomPluginOptions { + [key: string]: number | string | boolean | undefined; +} + +export interface PluginOptions { + database?: DatabasePluginOptions; + dns?: DNSPluginOptions; + http?: HttpPluginOptions; + custom?: CustomPluginOptions; } export interface PluginInternalFilesVersion { From 7f2f03a94bc9adc27c61cc8024ac3fa2f579570a Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 30 Dec 2019 18:16:04 -0500 Subject: [PATCH 07/15] chore: move plugin options to shared interface --- packages/opentelemetry-core/src/utils/url.ts | 90 +++++++++--- .../opentelemetry-core/test/utils/url.test.ts | 16 +-- packages/opentelemetry-plugin-dns/src/dns.ts | 29 ++-- .../opentelemetry-plugin-dns/src/utils.ts | 54 -------- .../test/functionals/utils.test.ts | 129 +----------------- .../opentelemetry-plugin-http/src/http.ts | 14 +- .../opentelemetry-plugin-http/src/utils.ts | 56 +------- .../test/functionals/utils.test.ts | 126 +---------------- .../src/types.ts | 6 - .../src/xhr.ts | 53 ++----- .../test/xhr.test.ts | 22 +-- .../src/trace/instrumentation/Plugin.ts | 21 +++ 12 files changed, 140 insertions(+), 476 deletions(-) diff --git a/packages/opentelemetry-core/src/utils/url.ts b/packages/opentelemetry-core/src/utils/url.ts index 8ed779bca2..314c5d59fc 100644 --- a/packages/opentelemetry-core/src/utils/url.ts +++ b/packages/opentelemetry-core/src/utils/url.ts @@ -14,35 +14,83 @@ * limitations under the License. */ -/** - * Check if {@param url} matches {@param urlToMatch} - * @param url - * @param urlToMatch - */ -export function urlMatches(url: string, urlToMatch: string | RegExp): boolean { - if (typeof urlToMatch === 'string') { - return url === urlToMatch; - } else { - return !!url.match(urlToMatch); - } -} +import { IgnoreMatcher } from "@opentelemetry/types"; + +// /** +// * Check if {@param url} matches {@param urlToMatch} +// * @param url +// * @param urlToMatch +// */ +// export function urlMatches(url: string, urlToMatch: string | RegExp): boolean { +// if (typeof urlToMatch === 'string') { +// return url === urlToMatch; +// } else { +// return !!url.match(urlToMatch); +// } +// } +// /** +// * @param url +// * @param ignoredUrls +// */ +// export function isUrlIgnored( +// url: string, +// ignoredUrls?: Array +// ): boolean { +// if (!ignoredUrls) { +// return false; +// } + +// for (const ignoreUrl of ignoredUrls) { +// if (urlMatches(url, ignoreUrl)) { +// return true; +// } +// } +// return false; +// } + + + /** * Check if {@param url} should be ignored when comparing against {@param ignoredUrls} - * @param url - * @param ignoredUrls + * + * @param url e.g URL of request + * @param pattern Match pattern */ -export function isUrlIgnored( +export function isIgnored( url: string, - ignoredUrls?: Array + pattern?: IgnoreMatcher | IgnoreMatcher[], + catcher?: (err: Error) => void ): boolean { - if (!ignoredUrls) { + if (!pattern) { return false; } - for (const ignoreUrl of ignoredUrls) { - if (urlMatches(url, ignoreUrl)) { - return true; + if (Array.isArray(pattern)) { + for (const p of pattern) { + if (isIgnored(url, p)) { + return true; + } } + return false; + } + + if (typeof pattern === 'string') { + return pattern === url; + } + + if (pattern instanceof RegExp) { + return pattern.test(url); } + + if (typeof pattern === 'function') { + try { + return pattern(url); + } catch (err) { + if (catcher) { + catcher(err); + } + } + } + return false; -} +}; \ No newline at end of file diff --git a/packages/opentelemetry-core/test/utils/url.test.ts b/packages/opentelemetry-core/test/utils/url.test.ts index 205ec9aa07..7ac07d9a4a 100644 --- a/packages/opentelemetry-core/test/utils/url.test.ts +++ b/packages/opentelemetry-core/test/utils/url.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; -import { isUrlIgnored } from '../../src'; +import { isIgnored } from '../../src'; const urlIgnored = 'url should be ignored'; const urlNotIgnored = 'url should NOT be ignored'; @@ -24,21 +24,21 @@ const urlNotIgnored = 'url should NOT be ignored'; const urlToTest = 'http://myaddress.com/somepath'; describe('BasePlugin - Utils', () => { - describe('isUrlIgnored', () => { + describe('isIgnored', () => { describe('when ignored urls are undefined', () => { it('should return false', () => { - assert.strictEqual(isUrlIgnored(urlToTest), false, urlNotIgnored); + assert.strictEqual(isIgnored(urlToTest), false, urlNotIgnored); }); }); describe('when ignored urls are empty', () => { it('should return false', () => { - assert.strictEqual(isUrlIgnored(urlToTest, []), false, urlNotIgnored); + assert.strictEqual(isIgnored(urlToTest, []), false, urlNotIgnored); }); }); describe('when ignored urls is the same as url', () => { it('should return true', () => { assert.strictEqual( - isUrlIgnored(urlToTest, ['http://myaddress.com/somepath']), + isIgnored(urlToTest, ['http://myaddress.com/somepath']), true, urlIgnored ); @@ -47,7 +47,7 @@ describe('BasePlugin - Utils', () => { describe('when url is part of ignored urls', () => { it('should return false', () => { assert.strictEqual( - isUrlIgnored(urlToTest, ['http://myaddress.com/some']), + isIgnored(urlToTest, ['http://myaddress.com/some']), false, urlNotIgnored ); @@ -56,7 +56,7 @@ describe('BasePlugin - Utils', () => { describe('when ignored urls is part of url - REGEXP', () => { it('should return true', () => { assert.strictEqual( - isUrlIgnored(urlToTest, [/.+?myaddress\.com/]), + isIgnored(urlToTest, [/.+?myaddress\.com/]), true, urlIgnored ); @@ -65,7 +65,7 @@ describe('BasePlugin - Utils', () => { describe('when url is part of ignored urls - REGEXP', () => { it('should return false', () => { assert.strictEqual( - isUrlIgnored(urlToTest, [/http:\/\/myaddress\.com\/somepath2/]), + isIgnored(urlToTest, [/http:\/\/myaddress\.com\/somepath2/]), false, urlNotIgnored ); diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index d742e41ff4..0bf99f36fa 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -14,26 +14,15 @@ * limitations under the License. */ -import * as shimmer from 'shimmer'; +import { BasePlugin, isIgnored } from '@opentelemetry/core'; +import { PluginOptions, Span, SpanKind, SpanOptions } from '@opentelemetry/types'; +import { LookupAddress } from 'dns'; import * as semver from 'semver'; -import * as utils from './utils'; -import { BasePlugin } from '@opentelemetry/core'; -import { - PluginOptions, - SpanOptions, - SpanKind, - Span, -} from '@opentelemetry/types'; -import { - Dns, - LookupPromiseSignature, - LookupFunction, - LookupFunctionSignature, - LookupCallbackSignature, -} from './types'; -import { AttributeNames } from './enums/AttributeNames'; +import * as shimmer from 'shimmer'; import { AddressFamily } from './enums/AddressFamily'; -import { LookupAddress } from 'dns'; +import { AttributeNames } from './enums/AttributeNames'; +import { Dns, LookupCallbackSignature, LookupFunction, LookupFunctionSignature, LookupPromiseSignature } from './types'; +import * as utils from './utils'; /** * Dns instrumentation plugin for Opentelemetry @@ -109,9 +98,7 @@ export class DnsPlugin extends BasePlugin { ...args: unknown[] ) { if ( - utils.isIgnored(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames, (e: Error) => - plugin._logger.error('caught ignoreHostname error: ', e) - ) + isIgnored(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames) ) { return original.apply(this, [hostname, ...args]); } diff --git a/packages/opentelemetry-plugin-dns/src/utils.ts b/packages/opentelemetry-plugin-dns/src/utils.ts index f1388741a7..de6f638ca7 100644 --- a/packages/opentelemetry-plugin-dns/src/utils.ts +++ b/packages/opentelemetry-plugin-dns/src/utils.ts @@ -17,7 +17,6 @@ import { Attributes, CanonicalCode, - IgnoreMatcher, Span, Status, } from '@opentelemetry/types'; @@ -161,56 +160,3 @@ export const setLookupAttributes = ( span.setAttributes(attributes); }; -/** - * Check whether the given obj match pattern - * @param constant e.g URL of request - * @param obj obj to inspect - * @param pattern Match pattern - */ -export const satisfiesPattern = ( - constant: string, - pattern: IgnoreMatcher -): boolean => { - if (typeof pattern === 'string') { - return pattern === constant; - } else if (pattern instanceof RegExp) { - return pattern.test(constant); - } else if (typeof pattern === 'function') { - return pattern(constant); - } else { - throw new TypeError('Pattern is in unsupported datatype'); - } -}; - -/** - * Check whether the given dns request is ignored by configuration - * It will not re-throw exceptions from `list` provided by the client - * @param constant e.g URL of request - * @param [list] List of ignore patterns - * @param [onException] callback for doing something when an exception has - * occurred - */ -export const isIgnored = ( - constant: string, - list?: IgnoreMatcher[], - onException?: (error: Error) => void -): boolean => { - if (!list) { - // No ignored urls - trace everything - return false; - } - // Try/catch outside the loop for failing fast - try { - for (const pattern of list) { - if (satisfiesPattern(constant, pattern)) { - return true; - } - } - } catch (e) { - if (onException) { - onException(e); - } - } - - return false; -}; diff --git a/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts index 8335b7dc9c..58672cd559 100644 --- a/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-dns/test/functionals/utils.test.ts @@ -15,11 +15,9 @@ */ import * as assert from 'assert'; -import * as sinon from 'sinon'; -import { CanonicalCode, IgnoreMatcher, SpanKind } from '@opentelemetry/types'; +import { CanonicalCode, SpanKind } from '@opentelemetry/types'; import * as utils from '../../src/utils'; import { Span, BasicTracer } from '@opentelemetry/tracing'; -import { NoopLogger } from '@opentelemetry/core'; import { AttributeNames } from '../../src/enums/AttributeNames'; describe('Utility', () => { @@ -32,131 +30,6 @@ describe('Utility', () => { }); }); - describe('satisfiesPattern()', () => { - it('string pattern', () => { - const answer1 = utils.satisfiesPattern('localhost', 'localhost'); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern('hostname', 'localhost'); - assert.strictEqual(answer2, false); - }); - - it('regex pattern', () => { - const answer1 = utils.satisfiesPattern('LocalHost', /localhost/i); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern('Montreal.ca', /montreal.ca/); - assert.strictEqual(answer2, false); - }); - - it('should throw if type is unknown', () => { - try { - utils.satisfiesPattern( - 'google.com', - (true as unknown) as IgnoreMatcher - ); - assert.fail(); - } catch (error) { - assert.strictEqual(error instanceof TypeError, true); - } - }); - - it('function pattern', () => { - const answer1 = utils.satisfiesPattern( - 'montreal.ca', - (url: string) => url === 'montreal.ca' - ); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern( - 'montreal.ca', - (url: string) => url !== 'montreal.ca' - ); - assert.strictEqual(answer2, false); - }); - }); - - describe('isIgnored()', () => { - let satisfiesPatternStub: sinon.SinonSpy<[string, IgnoreMatcher], boolean>; - beforeEach(() => { - satisfiesPatternStub = sinon.spy(utils, 'satisfiesPattern'); - }); - - afterEach(() => { - satisfiesPatternStub.restore(); - }); - - it('should call isSatisfyPattern, n match', () => { - const answer1 = utils.isIgnored('localhost', ['test']); - assert.strictEqual(answer1, false); - assert.strictEqual( - (utils.satisfiesPattern as sinon.SinonSpy).callCount, - 1 - ); - }); - - it('should call isSatisfyPattern, match for function', () => { - satisfiesPatternStub.restore(); - const answer1 = utils.isIgnored('api.montreal.ca', [ - url => url.endsWith('montreal.ca'), - ]); - assert.strictEqual(answer1, true); - }); - - it('should not re-throw when function throws an exception', () => { - satisfiesPatternStub.restore(); - const log = new NoopLogger(); - const onException = (e: Error) => { - log.error('error', e); - }; - for (const callback of [undefined, onException]) { - assert.doesNotThrow(() => - utils.isIgnored( - 'test', - [ - url => { - throw new Error('test'); - }, - ], - callback - ) - ); - } - }); - - it('should call onException when function throws an exception', () => { - satisfiesPatternStub.restore(); - const onException = sinon.spy(); - assert.doesNotThrow(() => - utils.isIgnored( - 'test', - [ - url => { - throw new Error('test'); - }, - ], - onException - ) - ); - assert.strictEqual((onException as sinon.SinonSpy).callCount, 1); - }); - - it('should not call isSatisfyPattern', () => { - utils.isIgnored('test', []); - assert.strictEqual( - (utils.satisfiesPattern as sinon.SinonSpy).callCount, - 0 - ); - }); - - it('should return false on empty list', () => { - const answer1 = utils.isIgnored('test', []); - assert.strictEqual(answer1, false); - }); - - it('should not throw and return false when list is undefined', () => { - const answer2 = utils.isIgnored('test', undefined); - assert.strictEqual(answer2, false); - }); - }); - describe('setError()', () => { it('should have error attributes', () => { const errorMessage = 'test error'; diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 18f79cd311..09de2fd9c3 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { BasePlugin, isValid } from '@opentelemetry/core'; +import { BasePlugin, isValid, isIgnored } from '@opentelemetry/core'; import { Attributes, CanonicalCode, PluginOptions, Span, SpanKind, SpanOptions, Status } from '@opentelemetry/types'; import { ClientRequest, IncomingMessage, request, RequestOptions, ServerResponse } from 'http'; import * as semver from 'semver'; @@ -268,11 +268,9 @@ export class HttpPlugin extends BasePlugin { plugin._logger.debug('%s plugin incomingRequest', plugin.moduleName); if ( - utils.isIgnored( + isIgnored( pathname, - plugin._config.http && plugin._config.http.ignoreIncomingPaths, - (e: Error) => - plugin._logger.error('caught ignoreIncomingPaths error: ', e) + plugin._config.http && plugin._config.http.ignoreIncomingPaths ) ) { return original.apply(this, [event, ...args]); @@ -395,11 +393,9 @@ export class HttpPlugin extends BasePlugin { if ( utils.isOpenTelemetryRequest(options) || - utils.isIgnored( + isIgnored( origin + pathname, - plugin._config.http && plugin._config.http.ignoreOutgoingUrls, - (e: Error) => - plugin._logger.error('caught ignoreOutgoingUrls error: ', e) + plugin._config.http && plugin._config.http.ignoreOutgoingUrls ) ) { return original.apply(this, [options, ...args]); diff --git a/packages/opentelemetry-plugin-http/src/utils.ts b/packages/opentelemetry-plugin-http/src/utils.ts index 2d1e5e440f..c255a1389a 100644 --- a/packages/opentelemetry-plugin-http/src/utils.ts +++ b/packages/opentelemetry-plugin-http/src/utils.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { CanonicalCode, IgnoreMatcher, Span, Status } from '@opentelemetry/types'; +import { CanonicalCode, Span, Status } from '@opentelemetry/types'; import { ClientRequest, IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, RequestOptions } from 'http'; import * as url from 'url'; import { AttributeNames } from './enums/AttributeNames'; @@ -96,60 +96,6 @@ export const hasExpectHeader = (options: RequestOptions | url.URL): boolean => { return !!keys.find(key => key.toLowerCase() === 'expect'); }; -/** - * Check whether the given obj match pattern - * @param constant e.g URL of request - * @param obj obj to inspect - * @param pattern Match pattern - */ -export const satisfiesPattern = ( - constant: string, - pattern: IgnoreMatcher -): boolean => { - if (typeof pattern === 'string') { - return pattern === constant; - } else if (pattern instanceof RegExp) { - return pattern.test(constant); - } else if (typeof pattern === 'function') { - return pattern(constant); - } else { - throw new TypeError('Pattern is in unsupported datatype'); - } -}; - -/** - * Check whether the given request is ignored by configuration - * It will not re-throw exceptions from `list` provided by the client - * @param constant e.g URL of request - * @param [list] List of ignore patterns - * @param [onException] callback for doing something when an exception has - * occurred - */ -export const isIgnored = ( - constant: string, - list?: IgnoreMatcher[], - onException?: (error: Error) => void -): boolean => { - if (!list) { - // No ignored urls - trace everything - return false; - } - // Try/catch outside the loop for failing fast - try { - for (const pattern of list) { - if (satisfiesPattern(constant, pattern)) { - return true; - } - } - } catch (e) { - if (onException) { - onException(e); - } - } - - return false; -}; - /** * Sets the span with the error passed in params * @param {Span} span the span that need to be set diff --git a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts index f6f8ba6291..2fcb6a8b30 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts @@ -14,13 +14,11 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; import { NoopScopeManager } from '@opentelemetry/scope-base'; import { BasicTracer, Span } from '@opentelemetry/tracing'; -import { CanonicalCode, IgnoreMatcher, SpanKind } from '@opentelemetry/types'; +import { CanonicalCode, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; -import * as sinon from 'sinon'; import * as url from 'url'; import { AttributeNames } from '../../src'; import * as utils from '../../src/utils'; @@ -96,128 +94,6 @@ describe('Utility', () => { }); }); - describe('satisfiesPattern()', () => { - it('string pattern', () => { - const answer1 = utils.satisfiesPattern('/test/1', '/test/1'); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern('/test/1', '/test/11'); - assert.strictEqual(answer2, false); - }); - - it('regex pattern', () => { - const answer1 = utils.satisfiesPattern('/TeSt/1', /\/test/i); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern('/2/tEst/1', /\/test/); - assert.strictEqual(answer2, false); - }); - - it('should throw if type is unknown', () => { - try { - utils.satisfiesPattern('/TeSt/1', (true as unknown) as IgnoreMatcher); - assert.fail(); - } catch (error) { - assert.strictEqual(error instanceof TypeError, true); - } - }); - - it('function pattern', () => { - const answer1 = utils.satisfiesPattern( - '/test/home', - (url: string) => url === '/test/home' - ); - assert.strictEqual(answer1, true); - const answer2 = utils.satisfiesPattern( - '/test/home', - (url: string) => url !== '/test/home' - ); - assert.strictEqual(answer2, false); - }); - }); - - describe('isIgnored()', () => { - let satisfiesPatternStub: sinon.SinonSpy<[string, IgnoreMatcher], boolean>; - beforeEach(() => { - satisfiesPatternStub = sinon.spy(utils, 'satisfiesPattern'); - }); - - afterEach(() => { - satisfiesPatternStub.restore(); - }); - - it('should call isSatisfyPattern, n match', () => { - const answer1 = utils.isIgnored('/test/1', ['/test/11']); - assert.strictEqual(answer1, false); - assert.strictEqual( - (utils.satisfiesPattern as sinon.SinonSpy).callCount, - 1 - ); - }); - - it('should call isSatisfyPattern, match for function', () => { - satisfiesPatternStub.restore(); - const answer1 = utils.isIgnored('/test/1', [ - url => url.endsWith('/test/1'), - ]); - assert.strictEqual(answer1, true); - }); - - it('should not re-throw when function throws an exception', () => { - satisfiesPatternStub.restore(); - const log = new NoopLogger(); - const onException = (e: Error) => { - log.error('error', e); - }; - for (const callback of [undefined, onException]) { - assert.doesNotThrow(() => - utils.isIgnored( - '/test/1', - [ - url => { - throw new Error('test'); - }, - ], - callback - ) - ); - } - }); - - it('should call onException when function throws an exception', () => { - satisfiesPatternStub.restore(); - const onException = sinon.spy(); - assert.doesNotThrow(() => - utils.isIgnored( - '/test/1', - [ - url => { - throw new Error('test'); - }, - ], - onException - ) - ); - assert.strictEqual((onException as sinon.SinonSpy).callCount, 1); - }); - - it('should not call isSatisfyPattern', () => { - utils.isIgnored('/test/1', []); - assert.strictEqual( - (utils.satisfiesPattern as sinon.SinonSpy).callCount, - 0 - ); - }); - - it('should return false on empty list', () => { - const answer1 = utils.isIgnored('/test/1', []); - assert.strictEqual(answer1, false); - }); - - it('should not throw and return false when list is undefined', () => { - const answer2 = utils.isIgnored('/test/1', undefined); - assert.strictEqual(answer2, false); - }); - }); - describe('getAbsoluteUrl()', () => { it('should return absolute url with localhost', () => { const path = '/test/1'; diff --git a/packages/opentelemetry-plugin-xml-http-request/src/types.ts b/packages/opentelemetry-plugin-xml-http-request/src/types.ts index 92c64f982f..906d1f502c 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/types.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/types.ts @@ -64,9 +64,3 @@ export interface XhrMem { // callback to remove events from xhr once the span ends callbackToRemoveEvents?: Function; } - -export type PropagateTraceHeaderCorsUrl = string | RegExp; - -export type PropagateTraceHeaderCorsUrls = - | PropagateTraceHeaderCorsUrl - | PropagateTraceHeaderCorsUrl[]; diff --git a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts index 46a8f61c39..302232fc3e 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts @@ -14,31 +14,15 @@ * limitations under the License. */ -import { - BasePlugin, - hrTime, - isUrlIgnored, - isWrapped, - otperformance, - urlMatches, -} from '@opentelemetry/core'; +import { BasePlugin, hrTime, isWrapped, otperformance, isIgnored } from '@opentelemetry/core'; import * as types from '@opentelemetry/types'; -import { - addSpanNetworkEvent, - getResource, - parseUrl, - PerformanceTimingNames as PTN, -} from '@opentelemetry/web'; +import { PluginOptions, XMLHttpRequestPluginOptions, IgnoreMatcher } from '@opentelemetry/types'; +import { addSpanNetworkEvent, getResource, parseUrl, PerformanceTimingNames as PTN } from '@opentelemetry/web'; import * as shimmer from 'shimmer'; import { AttributeNames } from './enums/AttributeNames'; import { EventNames } from './enums/EventNames'; import { Format } from './enums/Format'; -import { - OpenFunction, - PropagateTraceHeaderCorsUrls, - SendFunction, - XhrMem, -} from './types'; +import { OpenFunction, SendFunction, XhrMem } from './types'; // how long to wait for observer to collect information about resources // this is needed as event "load" is called before observer @@ -46,19 +30,6 @@ import { // safe enough const OBSERVER_WAIT_TIME_MS = 300; -/** - * XMLHttpRequest config - */ -export interface XMLHttpRequestPluginConfig extends types.PluginConfig { - // the number of timing resources is limited, after the limit - // (chrome 250, safari 150) the information is not collected anymore - // the only way to prevent that is to regularly clean the resources - // whenever it is possible, this is needed only when PerformanceObserver - // is not available - clearTimingResources?: boolean; - // urls which should include trace headers when origin doesn't match - propagateTraceHeaderCorsUrls?: PropagateTraceHeaderCorsUrls; -} /** * This class represents a XMLHttpRequest plugin for auto instrumentation @@ -69,15 +40,19 @@ export class XMLHttpRequestPlugin extends BasePlugin { readonly version: string = '0.3.0'; moduleName = this.component; - protected _config!: XMLHttpRequestPluginConfig; + protected _config!: PluginOptions; + private _xhrOptions: XMLHttpRequestPluginOptions; + private _ignoreOutgoingUrls: IgnoreMatcher[]; private _tasksCount = 0; private _xhrMem = new WeakMap(); private _usedResources = new WeakSet(); - constructor(config: XMLHttpRequestPluginConfig = {}) { + constructor(config: PluginOptions = {}) { super(); this._config = config; + this._xhrOptions = config.xhr || {}; + this._ignoreOutgoingUrls = config.http && config.http.ignoreOutgoingUrls || []; } /** @@ -107,7 +82,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { */ _shouldPropagateTraceHeaders(spanUrl: string) { let propagateTraceHeaderUrls = - this._config.propagateTraceHeaderCorsUrls || []; + this._xhrOptions.propagateTraceHeaderCorsUrls || []; if ( typeof propagateTraceHeaderUrls === 'string' || propagateTraceHeaderUrls instanceof RegExp @@ -120,7 +95,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { return true; } else { for (const propagateTraceHeaderUrl of propagateTraceHeaderUrls) { - if (urlMatches(spanUrl, propagateTraceHeaderUrl)) { + if (isIgnored(spanUrl, propagateTraceHeaderUrl)) { return true; } } @@ -232,7 +207,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { * @private */ private _clearResources() { - if (this._tasksCount === 0 && this._config.clearTimingResources) { + if (this._tasksCount === 0 && this._xhrOptions.clearTimingResources) { ((otperformance as unknown) as Performance).clearResourceTimings(); this._xhrMem = new WeakMap(); this._usedResources = new WeakSet(); @@ -316,7 +291,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { url: string, method: string ): types.Span | undefined { - if (isUrlIgnored(url, this._config.ignoreUrls)) { + if (isIgnored(url, this._ignoreOutgoingUrls)) { this._logger.debug('ignoring span as url matches ignored url'); return; } diff --git a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts index 2d920b0bfb..fab8f0c946 100644 --- a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts @@ -33,9 +33,9 @@ import { EventNames } from '../src/enums/EventNames'; import { XMLHttpRequestPlugin } from '../src/xhr'; class DummySpanExporter implements tracing.SpanExporter { - export(spans: any) {} + export(spans: any) { } - shutdown() {} + shutdown() { } } const getData = (url: string, callbackAfterSend: Function) => { @@ -43,15 +43,15 @@ const getData = (url: string, callbackAfterSend: Function) => { const req = new XMLHttpRequest(); req.open('GET', url, true); req.send(); - req.onload = function() { + req.onload = function () { resolve(); }; - req.onerror = function() { + req.onerror = function () { resolve(); }; - req.ontimeout = function() { + req.ontimeout = function () { resolve(); }; @@ -118,7 +118,7 @@ describe('xhr', () => { ) => { sandbox = sinon.createSandbox(); const fakeXhr = sandbox.useFakeXMLHttpRequest(); - fakeXhr.onCreate = function(xhr: any) { + fakeXhr.onCreate = function (xhr: any) { requests.push(xhr); }; sandbox.useFakeTimers(); @@ -142,7 +142,9 @@ describe('xhr', () => { scopeManager: new ZoneScopeManager(), plugins: [ new XMLHttpRequestPlugin({ - propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls, + xhr: { + propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls, + } }), ], }); @@ -340,7 +342,7 @@ describe('xhr', () => { describe( 'AND origin does NOT match window.location but match with' + - ' propagateTraceHeaderCorsUrls', + ' propagateTraceHeaderCorsUrls', () => { beforeEach(done => { clearData(); @@ -372,7 +374,7 @@ describe('xhr', () => { ); describe( 'AND origin does NOT match window.location And does NOT match' + - ' with propagateTraceHeaderCorsUrls', + ' with propagateTraceHeaderCorsUrls', () => { beforeEach(done => { clearData(); @@ -415,7 +417,7 @@ describe('xhr', () => { beforeEach(done => { sandbox = sinon.createSandbox(); const fakeXhr = sandbox.useFakeXMLHttpRequest(); - fakeXhr.onCreate = function(xhr: any) { + fakeXhr.onCreate = function (xhr: any) { requests.push(xhr); }; diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index 4743ff5bfa..25d00517f9 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -112,6 +112,26 @@ export interface HttpPluginOptions { applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; } +export type PropagateTraceHeaderCorsUrl = string | RegExp; + +export type PropagateTraceHeaderCorsUrls = + | PropagateTraceHeaderCorsUrl + | PropagateTraceHeaderCorsUrl[]; + +/** + * XMLHttpRequest config + */ +export interface XMLHttpRequestPluginOptions { + // the number of timing resources is limited, after the limit + // (chrome 250, safari 150) the information is not collected anymore + // the only way to prevent that is to regularly clean the resources + // whenever it is possible, this is needed only when PerformanceObserver + // is not available + clearTimingResources?: boolean; + // urls which should include trace headers when origin doesn't match + propagateTraceHeaderCorsUrls?: PropagateTraceHeaderCorsUrls; +} + /** * This is a configuration section where plugin authors * can define their own custom configuration options. @@ -124,6 +144,7 @@ export interface PluginOptions { database?: DatabasePluginOptions; dns?: DNSPluginOptions; http?: HttpPluginOptions; + xhr?: XMLHttpRequestPluginOptions; custom?: CustomPluginOptions; } From ceb6b6637dc6c6c56cc2396880e5809815bbe99f Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 31 Dec 2019 12:21:06 -0500 Subject: [PATCH 08/15] chore: review comments --- packages/opentelemetry-core/src/utils/url.ts | 111 +++++++++--------- .../opentelemetry-core/test/utils/url.test.ts | 87 ++++++++++++-- packages/opentelemetry-plugin-dns/src/dns.ts | 4 +- .../opentelemetry-plugin-http/src/http.ts | 6 +- .../src/xhr.ts | 15 ++- 5 files changed, 141 insertions(+), 82 deletions(-) diff --git a/packages/opentelemetry-core/src/utils/url.ts b/packages/opentelemetry-core/src/utils/url.ts index 314c5d59fc..9879f6eba2 100644 --- a/packages/opentelemetry-core/src/utils/url.ts +++ b/packages/opentelemetry-core/src/utils/url.ts @@ -14,83 +14,78 @@ * limitations under the License. */ -import { IgnoreMatcher } from "@opentelemetry/types"; - -// /** -// * Check if {@param url} matches {@param urlToMatch} -// * @param url -// * @param urlToMatch -// */ -// export function urlMatches(url: string, urlToMatch: string | RegExp): boolean { -// if (typeof urlToMatch === 'string') { -// return url === urlToMatch; -// } else { -// return !!url.match(urlToMatch); -// } -// } -// /** -// * @param url -// * @param ignoredUrls -// */ -// export function isUrlIgnored( -// url: string, -// ignoredUrls?: Array -// ): boolean { -// if (!ignoredUrls) { -// return false; -// } - -// for (const ignoreUrl of ignoredUrls) { -// if (urlMatches(url, ignoreUrl)) { -// return true; -// } -// } -// return false; -// } - - +import { IgnoreMatcher } from '@opentelemetry/types'; /** - * Check if {@param url} should be ignored when comparing against {@param ignoredUrls} + * Check if {@param url} matches an ignore matcher. An ignore + * matcher is matched if it satisfies one of the following: + * + * 1. Exact string match (case insensitive) + * + * 2. Matches regexp using pattern.test + * + * 3. Function does not throw AND returns a truthy value * * @param url e.g URL of request - * @param pattern Match pattern + * @param pattern Match pattern may be a string, regex, or function */ -export function isIgnored( - url: string, - pattern?: IgnoreMatcher | IgnoreMatcher[], - catcher?: (err: Error) => void -): boolean { +export function matchesPattern(url: string, pattern?: IgnoreMatcher): boolean { if (!pattern) { return false; } - if (Array.isArray(pattern)) { - for (const p of pattern) { - if (isIgnored(url, p)) { - return true; - } - } - return false; - } - + // exact string match if (typeof pattern === 'string') { - return pattern === url; + return pattern.toUpperCase() === url.toUpperCase(); } + // test regex if (pattern instanceof RegExp) { return pattern.test(url); } + // function does not throw AND returns truthy value if (typeof pattern === 'function') { try { - return pattern(url); - } catch (err) { - if (catcher) { - catcher(err); - } + return Boolean(pattern(url)); + } catch { + return false; } } + // If none of the above conditions are met, the + // url is not considered matched. + return false; +} + +/** + * Check if {@param url} matches any matcher in an array of + * ignore matchers. An ignore matcher is matched if it satisfies + * one of the following: + * + * 1. Exact string match (case insensitive) + * + * 2. Matches regexp using pattern.test + * + * 3. Function does not throw AND returns a truthy value + * + * @param url e.g URL of request + * @param patterns Array of match patterns which may be a string, regex, or function + */ +export function matchesAnyPattern( + url: string, + patterns?: IgnoreMatcher[] +): boolean { + if (!patterns) { + return false; + } + + // If any matcher in an array is matched, the url + // is considered ignored. + for (const p of patterns) { + if (matchesPattern(url, p)) { + return true; + } + } return false; -}; \ No newline at end of file +} diff --git a/packages/opentelemetry-core/test/utils/url.test.ts b/packages/opentelemetry-core/test/utils/url.test.ts index 7ac07d9a4a..6dd56aa3b3 100644 --- a/packages/opentelemetry-core/test/utils/url.test.ts +++ b/packages/opentelemetry-core/test/utils/url.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; -import { isIgnored } from '../../src'; +import { matchesAnyPattern, matchesPattern } from '../../src'; const urlIgnored = 'url should be ignored'; const urlNotIgnored = 'url should NOT be ignored'; @@ -24,30 +24,95 @@ const urlNotIgnored = 'url should NOT be ignored'; const urlToTest = 'http://myaddress.com/somepath'; describe('BasePlugin - Utils', () => { - describe('isIgnored', () => { + describe('matchesPattern', () => { + describe('when a pattern is a string', () => { + describe('and an exact case insensitive match', () => { + it('should return true', () => { + assert.strictEqual(matchesPattern('http://url', 'http://URL'), true); + }); + }); + describe('and not an exact case insensitive match', () => { + it('should return false', () => { + assert.strictEqual( + matchesPattern('http://url', 'http://URL.com'), + false + ); + }); + }); + }); + describe('when a pattern is a regular expression', () => { + describe('and a match', () => { + it('should return true', () => { + assert.strictEqual(matchesPattern('http://url', /url/), true); + }); + }); + describe('and not a match', () => { + it('should return false', () => { + assert.strictEqual(matchesPattern('http://url', /noturl/), false); + }); + }); + }); + describe('when a pattern is a function', () => { + describe('that throws', () => { + it('should return false', () => { + assert.strictEqual( + matchesPattern('http://url', () => { + throw new Error(); + }), + false + ); + }); + }); + describe('that returns a false value', () => { + it('should return false', () => { + assert.strictEqual( + matchesPattern('http://url', url => { + return url === 'noturl'; + }), + false + ); + }); + }); + describe('that returns a true value', () => { + it('should return true', () => { + assert.strictEqual( + matchesPattern('http://url', url => { + return url === 'http://url'; + }), + true + ); + }); + }); + }); + }); + describe('matchesAnyPattern', () => { describe('when ignored urls are undefined', () => { it('should return false', () => { - assert.strictEqual(isIgnored(urlToTest), false, urlNotIgnored); + assert.strictEqual(matchesAnyPattern(urlToTest), false, urlNotIgnored); }); }); describe('when ignored urls are empty', () => { it('should return false', () => { - assert.strictEqual(isIgnored(urlToTest, []), false, urlNotIgnored); + assert.strictEqual( + matchesAnyPattern(urlToTest, []), + false, + urlNotIgnored + ); }); }); - describe('when ignored urls is the same as url', () => { + describe('when ignored urls contain the url', () => { it('should return true', () => { assert.strictEqual( - isIgnored(urlToTest, ['http://myaddress.com/somepath']), + matchesAnyPattern(urlToTest, ['http://myaddress.com/somepath']), true, urlIgnored ); }); }); - describe('when url is part of ignored urls', () => { + describe('when ignored urls does not contain the url', () => { it('should return false', () => { assert.strictEqual( - isIgnored(urlToTest, ['http://myaddress.com/some']), + matchesAnyPattern(urlToTest, ['http://myaddress.com/some']), false, urlNotIgnored ); @@ -56,16 +121,16 @@ describe('BasePlugin - Utils', () => { describe('when ignored urls is part of url - REGEXP', () => { it('should return true', () => { assert.strictEqual( - isIgnored(urlToTest, [/.+?myaddress\.com/]), + matchesAnyPattern(urlToTest, [/.+?myaddress\.com/]), true, urlIgnored ); }); }); - describe('when url is part of ignored urls - REGEXP', () => { + describe('when url is not part of ignored urls - REGEXP', () => { it('should return false', () => { assert.strictEqual( - isIgnored(urlToTest, [/http:\/\/myaddress\.com\/somepath2/]), + matchesAnyPattern(urlToTest, [/http:\/\/myaddress\.com\/somepath2/]), false, urlNotIgnored ); diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index 0bf99f36fa..af4f5a133c 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { BasePlugin, isIgnored } from '@opentelemetry/core'; +import { BasePlugin, matchesAnyPattern } from '@opentelemetry/core'; import { PluginOptions, Span, SpanKind, SpanOptions } from '@opentelemetry/types'; import { LookupAddress } from 'dns'; import * as semver from 'semver'; @@ -98,7 +98,7 @@ export class DnsPlugin extends BasePlugin { ...args: unknown[] ) { if ( - isIgnored(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames) + matchesAnyPattern(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames) ) { return original.apply(this, [hostname, ...args]); } diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 09de2fd9c3..6832c8b875 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { BasePlugin, isValid, isIgnored } from '@opentelemetry/core'; +import { BasePlugin, isValid, matchesAnyPattern } from '@opentelemetry/core'; import { Attributes, CanonicalCode, PluginOptions, Span, SpanKind, SpanOptions, Status } from '@opentelemetry/types'; import { ClientRequest, IncomingMessage, request, RequestOptions, ServerResponse } from 'http'; import * as semver from 'semver'; @@ -268,7 +268,7 @@ export class HttpPlugin extends BasePlugin { plugin._logger.debug('%s plugin incomingRequest', plugin.moduleName); if ( - isIgnored( + matchesAnyPattern( pathname, plugin._config.http && plugin._config.http.ignoreIncomingPaths ) @@ -393,7 +393,7 @@ export class HttpPlugin extends BasePlugin { if ( utils.isOpenTelemetryRequest(options) || - isIgnored( + matchesAnyPattern( origin + pathname, plugin._config.http && plugin._config.http.ignoreOutgoingUrls ) diff --git a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts index 302232fc3e..8b1cff6776 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { BasePlugin, hrTime, isWrapped, otperformance, isIgnored } from '@opentelemetry/core'; +import { BasePlugin, hrTime, isWrapped, otperformance, matchesAnyPattern, matchesPattern } from '@opentelemetry/core'; import * as types from '@opentelemetry/types'; -import { PluginOptions, XMLHttpRequestPluginOptions, IgnoreMatcher } from '@opentelemetry/types'; import { addSpanNetworkEvent, getResource, parseUrl, PerformanceTimingNames as PTN } from '@opentelemetry/web'; import * as shimmer from 'shimmer'; import { AttributeNames } from './enums/AttributeNames'; @@ -40,15 +39,15 @@ export class XMLHttpRequestPlugin extends BasePlugin { readonly version: string = '0.3.0'; moduleName = this.component; - protected _config!: PluginOptions; - private _xhrOptions: XMLHttpRequestPluginOptions; - private _ignoreOutgoingUrls: IgnoreMatcher[]; + protected _config!: types.PluginOptions; + private _xhrOptions: types.XMLHttpRequestPluginOptions; + private _ignoreOutgoingUrls: types.IgnoreMatcher[]; private _tasksCount = 0; private _xhrMem = new WeakMap(); private _usedResources = new WeakSet(); - constructor(config: PluginOptions = {}) { + constructor(config: types.PluginOptions = {}) { super(); this._config = config; this._xhrOptions = config.xhr || {}; @@ -95,7 +94,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { return true; } else { for (const propagateTraceHeaderUrl of propagateTraceHeaderUrls) { - if (isIgnored(spanUrl, propagateTraceHeaderUrl)) { + if (matchesPattern(spanUrl, propagateTraceHeaderUrl)) { return true; } } @@ -291,7 +290,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { url: string, method: string ): types.Span | undefined { - if (isIgnored(url, this._ignoreOutgoingUrls)) { + if (matchesAnyPattern(url, this._ignoreOutgoingUrls)) { this._logger.debug('ignoring span as url matches ignored url'); return; } From cb0b8129a39c5dbedfead5669fc407cb42dfb60f Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 31 Dec 2019 12:26:16 -0500 Subject: [PATCH 09/15] fix: lint --- .../src/instrumentation/PluginLoader.ts | 7 +++- .../test/NodeTracer.test.ts | 2 +- .../test/instrumentation/PluginLoader.test.ts | 12 +++--- packages/opentelemetry-plugin-dns/src/dns.ts | 20 ++++++++-- .../opentelemetry-plugin-dns/src/utils.ts | 8 +--- .../src/documentLoad.ts | 7 +++- .../test/documentLoad.test.ts | 9 +++-- .../opentelemetry-plugin-http/src/http.ts | 37 ++++++++++++++++--- .../opentelemetry-plugin-http/src/types.ts | 1 - .../opentelemetry-plugin-http/src/utils.ts | 8 +++- .../test/functionals/http-enable.test.ts | 18 ++++++--- .../test/functionals/http-package.test.ts | 12 +++--- .../test/functionals/utils.test.ts | 2 +- .../test/integrations/http-enable.test.ts | 11 ++++-- .../test/functionals/https-enable.test.ts | 24 +++++++++--- .../test/functionals/https-package.test.ts | 16 ++++---- .../test/integrations/https-enable.test.ts | 11 ++++-- .../src/xhr.ts | 20 ++++++++-- .../test/xhr.test.ts | 20 +++++----- .../src/trace/instrumentation/Plugin.ts | 3 +- 20 files changed, 170 insertions(+), 78 deletions(-) diff --git a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts index 6229aa6f85..ef4869cac8 100644 --- a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts +++ b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts @@ -148,7 +148,12 @@ export class PluginLoader { this._plugins.push(plugin); // Enable each supported plugin. - return plugin.enable(exports, this.tracer, this.logger, config.options); + return plugin.enable( + exports, + this.tracer, + this.logger, + config.options + ); } catch (e) { this.logger.error( `PluginLoader#load: could not load plugin ${modulePath} of module ${name}. Error: ${e.message}` diff --git a/packages/opentelemetry-node/test/NodeTracer.test.ts b/packages/opentelemetry-node/test/NodeTracer.test.ts index d7bfa52775..ebdb1e0343 100644 --- a/packages/opentelemetry-node/test/NodeTracer.test.ts +++ b/packages/opentelemetry-node/test/NodeTracer.test.ts @@ -102,7 +102,7 @@ describe('NodeTracer', () => { }, http: { ignoreOutgoingUrls: [], - } + }, }, }, }, diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index 8cefe70e61..0c7654b56d 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -39,7 +39,7 @@ const httpPlugins: Plugins = { path: '@opentelemetry/plugin-http-module', options: { http: { - ignoreOutgoingUrls: ["plugin specific option"] + ignoreOutgoingUrls: ['plugin specific option'], }, }, }, @@ -184,14 +184,14 @@ describe('PluginLoader', () => { assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.load({ plugins: httpPlugins, - options: { http: { ignoreOutgoingUrls: ["shared"] } }, + options: { http: { ignoreOutgoingUrls: ['shared'] } }, }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.deepStrictEqual( (pluginLoader['_plugins'][0] as any)._config.http.ignoreOutgoingUrls, - ["plugin specific option"] + ['plugin specific option'] ); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); @@ -202,14 +202,14 @@ describe('PluginLoader', () => { assert.strictEqual(pluginLoader['_plugins'].length, 0); pluginLoader.load({ plugins: httpPlugins, - options: { dns: { ignoreHostnames: ["shared"] } }, + options: { dns: { ignoreHostnames: ['shared'] } }, }); // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.deepStrictEqual((pluginLoader['_plugins'][0] as any)._config, { - dns: { ignoreHostnames: ["shared"] }, - http: { ignoreOutgoingUrls: ["plugin specific option"] }, + dns: { ignoreHostnames: ['shared'] }, + http: { ignoreOutgoingUrls: ['plugin specific option'] }, }); assert.strictEqual(httpModule.get(), 'patched'); pluginLoader.unload(); diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index af4f5a133c..ec8a8f153d 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -15,13 +15,24 @@ */ import { BasePlugin, matchesAnyPattern } from '@opentelemetry/core'; -import { PluginOptions, Span, SpanKind, SpanOptions } from '@opentelemetry/types'; +import { + PluginOptions, + Span, + SpanKind, + SpanOptions, +} from '@opentelemetry/types'; import { LookupAddress } from 'dns'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; import { AddressFamily } from './enums/AddressFamily'; import { AttributeNames } from './enums/AttributeNames'; -import { Dns, LookupCallbackSignature, LookupFunction, LookupFunctionSignature, LookupPromiseSignature } from './types'; +import { + Dns, + LookupCallbackSignature, + LookupFunction, + LookupFunctionSignature, + LookupPromiseSignature, +} from './types'; import * as utils from './utils'; /** @@ -98,7 +109,10 @@ export class DnsPlugin extends BasePlugin { ...args: unknown[] ) { if ( - matchesAnyPattern(hostname, plugin._config.dns && plugin._config.dns.ignoreHostnames) + matchesAnyPattern( + hostname, + plugin._config.dns && plugin._config.dns.ignoreHostnames + ) ) { return original.apply(this, [hostname, ...args]); } diff --git a/packages/opentelemetry-plugin-dns/src/utils.ts b/packages/opentelemetry-plugin-dns/src/utils.ts index de6f638ca7..baafd8dbcb 100644 --- a/packages/opentelemetry-plugin-dns/src/utils.ts +++ b/packages/opentelemetry-plugin-dns/src/utils.ts @@ -14,12 +14,7 @@ * limitations under the License. */ -import { - Attributes, - CanonicalCode, - Span, - Status, -} from '@opentelemetry/types'; +import { Attributes, CanonicalCode, Span, Status } from '@opentelemetry/types'; import * as dns from 'dns'; import { AddressFamily } from './enums/AddressFamily'; import { AttributeNames } from './enums/AttributeNames'; @@ -159,4 +154,3 @@ export const setLookupAttributes = ( span.setAttributes(attributes); }; - diff --git a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts index 40d0a0cc19..8c025a1f0b 100644 --- a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -14,7 +14,12 @@ * limitations under the License. */ -import { BasePlugin, otperformance, parseTraceParent, TRACE_PARENT_HEADER } from '@opentelemetry/core'; +import { + BasePlugin, + otperformance, + parseTraceParent, + TRACE_PARENT_HEADER, +} from '@opentelemetry/core'; import { PluginOptions, Span, SpanOptions } from '@opentelemetry/types'; import { AttributeNames } from './enums/AttributeNames'; import { diff --git a/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts b/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts index 6982f945d1..4c40840ed0 100644 --- a/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts +++ b/packages/opentelemetry-plugin-document-load/test/documentLoad.test.ts @@ -19,7 +19,12 @@ */ import { ConsoleLogger, TRACE_PARENT_HEADER } from '@opentelemetry/core'; -import { BasicTracer, ReadableSpan, SimpleSpanProcessor, SpanExporter } from '@opentelemetry/tracing'; +import { + BasicTracer, + ReadableSpan, + SimpleSpanProcessor, + SpanExporter, +} from '@opentelemetry/tracing'; import { Logger, PluginOptions, TimedEvent } from '@opentelemetry/types'; import * as assert from 'assert'; import * as sinon from 'sinon'; @@ -27,8 +32,6 @@ import { ExportResult } from '../../opentelemetry-base/build/src'; import { DocumentLoad } from '../src'; import { PerformanceTimingNames as PTN } from '@opentelemetry/web'; - - export class DummyExporter implements SpanExporter { export( spans: ReadableSpan[], diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 6832c8b875..f1bc87a319 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -15,14 +15,35 @@ */ import { BasePlugin, isValid, matchesAnyPattern } from '@opentelemetry/core'; -import { Attributes, CanonicalCode, PluginOptions, Span, SpanKind, SpanOptions, Status } from '@opentelemetry/types'; -import { ClientRequest, IncomingMessage, request, RequestOptions, ServerResponse } from 'http'; +import { + Attributes, + CanonicalCode, + PluginOptions, + Span, + SpanKind, + SpanOptions, + Status, +} from '@opentelemetry/types'; +import { + ClientRequest, + IncomingMessage, + request, + RequestOptions, + ServerResponse, +} from 'http'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; import * as url from 'url'; import { AttributeNames } from './enums/AttributeNames'; import { Format } from './enums/Format'; -import { Err, Func, Http, HttpRequestArgs, ParsedRequestOptions, ResponseEndArgs } from './types'; +import { + Err, + Func, + Http, + HttpRequestArgs, + ParsedRequestOptions, + ResponseEndArgs, +} from './types'; import * as utils from './utils'; /** @@ -208,7 +229,10 @@ export class HttpPlugin extends BasePlugin { span.setStatus(status); - if (this._config.http && this._config.http.applyCustomAttributesOnSpan) { + if ( + this._config.http && + this._config.http.applyCustomAttributesOnSpan + ) { this._safeExecute( span, () => @@ -340,7 +364,10 @@ export class HttpPlugin extends BasePlugin { .setAttributes(attributes) .setStatus(utils.parseResponseStatus(response.statusCode)); - if (plugin._config.http && plugin._config.http.applyCustomAttributesOnSpan) { + if ( + plugin._config.http && + plugin._config.http.applyCustomAttributesOnSpan + ) { plugin._safeExecute( span, () => diff --git a/packages/opentelemetry-plugin-http/src/types.ts b/packages/opentelemetry-plugin-http/src/types.ts index 6377701201..9857e0a023 100644 --- a/packages/opentelemetry-plugin-http/src/types.ts +++ b/packages/opentelemetry-plugin-http/src/types.ts @@ -41,7 +41,6 @@ export type ResponseEndArgs = | [unknown, ((() => void) | undefined)?] | [unknown, string, ((() => void) | undefined)?]; - export interface Err extends Error { errno?: number; code?: string; diff --git a/packages/opentelemetry-plugin-http/src/utils.ts b/packages/opentelemetry-plugin-http/src/utils.ts index c255a1389a..b3e7ecd601 100644 --- a/packages/opentelemetry-plugin-http/src/utils.ts +++ b/packages/opentelemetry-plugin-http/src/utils.ts @@ -15,7 +15,13 @@ */ import { CanonicalCode, Span, Status } from '@opentelemetry/types'; -import { ClientRequest, IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, RequestOptions } from 'http'; +import { + ClientRequest, + IncomingHttpHeaders, + IncomingMessage, + OutgoingHttpHeaders, + RequestOptions, +} from 'http'; import * as url from 'url'; import { AttributeNames } from './enums/AttributeNames'; import { Err, ParsedRequestOptions } from './types'; diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 68dfca8426..5396640db1 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -16,8 +16,16 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; -import { CanonicalCode, PluginOptions, Span as ISpan, SpanKind } from '@opentelemetry/types'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; +import { + CanonicalCode, + PluginOptions, + Span as ISpan, + SpanKind, +} from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; import * as nock from 'nock'; @@ -101,7 +109,7 @@ describe('HttpPlugin', () => { applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, - } + }, }; pluginWithBadOptions = new HttpPlugin( plugin.component, @@ -176,7 +184,7 @@ describe('HttpPlugin', () => { (url: string) => url.endsWith(`/ignored/function`), ], applyCustomAttributesOnSpan: customAttributeFunction, - } + }, }; plugin.enable(http, tracer, tracer.logger, config); server = http.createServer((request, response) => { @@ -609,7 +617,7 @@ describe('HttpPlugin', () => { .reply(404); const req = http.request(`${host}/`); req.on('response', response => { - response.on('data', () => { }); + response.on('data', () => {}); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts index ebc5aed489..235716f3c1 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts @@ -57,8 +57,8 @@ describe('Packages', () => { const config: PluginOptions = { http: { applyCustomAttributesOnSpan: customAttributeFunction, - } - } + }, + }; plugin.enable(http, tracer, tracer.logger, config); }); @@ -92,10 +92,10 @@ describe('Packages', () => { const urlparsed = url.parse( name === 'got' && process.versions.node.startsWith('12') ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://info.cern.ch/` + // https://github.com/nock/nock/pull/1551 + // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 + // TODO: check if this is still the case when new version + `${protocol}://info.cern.ch/` : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); diff --git a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts index 2fcb6a8b30..f959deeddb 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts @@ -51,7 +51,7 @@ describe('Utility', () => { try { utils.hasExpectHeader('' as http.RequestOptions); assert.fail(); - } catch (ignore) { } + } catch (ignore) {} }); it('should not throw if no headers', () => { diff --git a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts index 50f643f42f..a66e738550 100644 --- a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts @@ -16,7 +16,10 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; import { Span, SpanKind, PluginOptions } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; @@ -37,7 +40,7 @@ export const customAttributeFunction = (span: Span): void => { describe('HttpPlugin Integration tests', () => { describe('enable()', () => { - before(function (done) { + before(function(done) { // mandatory if (process.env.CI) { done(); @@ -75,11 +78,11 @@ describe('HttpPlugin Integration tests', () => { ignoreIncomingPaths: ignoreConfig, ignoreOutgoingUrls: ignoreConfig, applyCustomAttributesOnSpan: customAttributeFunction, - } + }, }; try { plugin.disable(); - } catch (e) { } + } catch (e) {} plugin.enable(http, tracer, tracer.logger, config); }); diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts index e595ae666e..dc416df49a 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts @@ -16,9 +16,21 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { AttributeNames, Http, OT_REQUEST_HEADER } from '@opentelemetry/plugin-http'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; -import { CanonicalCode, PluginOptions, Span as ISpan, SpanKind } from '@opentelemetry/types'; +import { + AttributeNames, + Http, + OT_REQUEST_HEADER, +} from '@opentelemetry/plugin-http'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; +import { + CanonicalCode, + PluginOptions, + Span as ISpan, + SpanKind, +} from '@opentelemetry/types'; import * as assert from 'assert'; import * as fs from 'fs'; import * as http from 'http'; @@ -101,7 +113,7 @@ describe('HttpsPlugin', () => { applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, - } + }, }; pluginWithBadOptions = new HttpsPlugin(process.versions.node); pluginWithBadOptions.enable( @@ -184,7 +196,7 @@ describe('HttpsPlugin', () => { (url: string) => url.endsWith(`/ignored/function`), ], applyCustomAttributesOnSpan: customAttributeFunction, - } + }, }; plugin.enable( (https as unknown) as Http, @@ -616,7 +628,7 @@ describe('HttpsPlugin', () => { .reply(404); const req = https.request(`${host}/`); req.on('response', response => { - response.on('data', () => { }); + response.on('data', () => {}); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts index 05b04c3c3a..bb1bf635bf 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts @@ -17,7 +17,10 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; import { Http } from '@opentelemetry/plugin-http'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; import { PluginOptions, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import axios, { AxiosResponse } from 'axios'; @@ -34,7 +37,6 @@ import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; import { customAttributeFunction } from './https-enable.test'; - const memoryExporter = new InMemorySpanExporter(); const protocol = 'https'; @@ -56,7 +58,7 @@ describe('Packages', () => { const config: PluginOptions = { http: { applyCustomAttributesOnSpan: customAttributeFunction, - } + }, }; plugin.enable((https as unknown) as Http, tracer, tracer.logger, config); }); @@ -91,10 +93,10 @@ describe('Packages', () => { const urlparsed = url.parse( name === 'got' && process.versions.node.startsWith('12') ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://www.google.com` + // https://github.com/nock/nock/pull/1551 + // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 + // TODO: check if this is still the case when new version + `${protocol}://www.google.com` : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); diff --git a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts index 8a63cc6ab9..1110229949 100644 --- a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts @@ -17,7 +17,10 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; import { Http } from '@opentelemetry/plugin-http'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; import { PluginOptions, Span, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; @@ -40,7 +43,7 @@ export const customAttributeFunction = (span: Span): void => { describe('HttpsPlugin Integration tests', () => { describe('enable()', () => { - before(function (done) { + before(function(done) { // mandatory if (process.env.CI) { done(); @@ -78,11 +81,11 @@ describe('HttpsPlugin Integration tests', () => { ignoreIncomingPaths: ignoreConfig, ignoreOutgoingUrls: ignoreConfig, applyCustomAttributesOnSpan: customAttributeFunction, - } + }, }; try { plugin.disable(); - } catch (e) { } + } catch (e) {} plugin.enable((https as unknown) as Http, tracer, tracer.logger, config); }); diff --git a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts index 8b1cff6776..09e6946fa1 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts @@ -14,9 +14,21 @@ * limitations under the License. */ -import { BasePlugin, hrTime, isWrapped, otperformance, matchesAnyPattern, matchesPattern } from '@opentelemetry/core'; +import { + BasePlugin, + hrTime, + isWrapped, + otperformance, + matchesAnyPattern, + matchesPattern, +} from '@opentelemetry/core'; import * as types from '@opentelemetry/types'; -import { addSpanNetworkEvent, getResource, parseUrl, PerformanceTimingNames as PTN } from '@opentelemetry/web'; +import { + addSpanNetworkEvent, + getResource, + parseUrl, + PerformanceTimingNames as PTN, +} from '@opentelemetry/web'; import * as shimmer from 'shimmer'; import { AttributeNames } from './enums/AttributeNames'; import { EventNames } from './enums/EventNames'; @@ -29,7 +41,6 @@ import { OpenFunction, SendFunction, XhrMem } from './types'; // safe enough const OBSERVER_WAIT_TIME_MS = 300; - /** * This class represents a XMLHttpRequest plugin for auto instrumentation */ @@ -51,7 +62,8 @@ export class XMLHttpRequestPlugin extends BasePlugin { super(); this._config = config; this._xhrOptions = config.xhr || {}; - this._ignoreOutgoingUrls = config.http && config.http.ignoreOutgoingUrls || []; + this._ignoreOutgoingUrls = + (config.http && config.http.ignoreOutgoingUrls) || []; } /** diff --git a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts index fab8f0c946..08d95a4e59 100644 --- a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts @@ -33,9 +33,9 @@ import { EventNames } from '../src/enums/EventNames'; import { XMLHttpRequestPlugin } from '../src/xhr'; class DummySpanExporter implements tracing.SpanExporter { - export(spans: any) { } + export(spans: any) {} - shutdown() { } + shutdown() {} } const getData = (url: string, callbackAfterSend: Function) => { @@ -43,15 +43,15 @@ const getData = (url: string, callbackAfterSend: Function) => { const req = new XMLHttpRequest(); req.open('GET', url, true); req.send(); - req.onload = function () { + req.onload = function() { resolve(); }; - req.onerror = function () { + req.onerror = function() { resolve(); }; - req.ontimeout = function () { + req.ontimeout = function() { resolve(); }; @@ -118,7 +118,7 @@ describe('xhr', () => { ) => { sandbox = sinon.createSandbox(); const fakeXhr = sandbox.useFakeXMLHttpRequest(); - fakeXhr.onCreate = function (xhr: any) { + fakeXhr.onCreate = function(xhr: any) { requests.push(xhr); }; sandbox.useFakeTimers(); @@ -144,7 +144,7 @@ describe('xhr', () => { new XMLHttpRequestPlugin({ xhr: { propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls, - } + }, }), ], }); @@ -342,7 +342,7 @@ describe('xhr', () => { describe( 'AND origin does NOT match window.location but match with' + - ' propagateTraceHeaderCorsUrls', + ' propagateTraceHeaderCorsUrls', () => { beforeEach(done => { clearData(); @@ -374,7 +374,7 @@ describe('xhr', () => { ); describe( 'AND origin does NOT match window.location And does NOT match' + - ' with propagateTraceHeaderCorsUrls', + ' with propagateTraceHeaderCorsUrls', () => { beforeEach(done => { clearData(); @@ -417,7 +417,7 @@ describe('xhr', () => { beforeEach(done => { sandbox = sinon.createSandbox(); const fakeXhr = sandbox.useFakeXMLHttpRequest(); - fakeXhr.onCreate = function (xhr: any) { + fakeXhr.onCreate = function(xhr: any) { requests.push(xhr); }; diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index 25d00517f9..9b0781cb2b 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -14,12 +14,11 @@ * limitations under the License. */ -import { ClientRequest, IncomingMessage, ServerResponse } from "http"; +import { ClientRequest, IncomingMessage, ServerResponse } from 'http'; import { Logger } from '../../common/Logger'; import { Span } from '../span'; import { Tracer } from '../tracer'; - /** Interface Plugin to apply patch. */ export interface Plugin { /** From 9da7a6202753bbdd77bd3918e0d540c59fd8f01b Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 14:51:59 -0500 Subject: [PATCH 10/15] fix: lint --- packages/opentelemetry-plugin-http/src/http.ts | 4 ++-- packages/opentelemetry-plugin-http/src/utils.ts | 9 ++++++++- .../test/functionals/http-enable.test.ts | 2 +- .../test/integrations/http-enable.test.ts | 5 ++++- .../test/functionals/https-enable.test.ts | 2 +- .../test/integrations/https-enable.test.ts | 5 ++++- .../src/xhr.ts | 16 ++++++++++++++-- .../src/trace/instrumentation/Plugin.ts | 5 +++++ 8 files changed, 39 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index f3696907b4..21303fc3a0 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -312,7 +312,7 @@ export class HttpPlugin extends BasePlugin { // Wraps end (inspired by: // https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/master/src/plugins/plugin-connect.ts#L75) const originalEnd = response.end; - response.end = function ( + response.end = function( this: ServerResponse, ...args: ResponseEndArgs ) { @@ -378,7 +378,7 @@ export class HttpPlugin extends BasePlugin { const extraOptions = typeof args[0] === 'object' && - (typeof options === 'string' || options instanceof url.URL) + (typeof options === 'string' || options instanceof url.URL) ? (args.shift() as RequestOptions) : undefined; const { origin, pathname, method, optionsParsed } = utils.getRequestInfo( diff --git a/packages/opentelemetry-plugin-http/src/utils.ts b/packages/opentelemetry-plugin-http/src/utils.ts index e6b3eddb5e..fe5e37a941 100644 --- a/packages/opentelemetry-plugin-http/src/utils.ts +++ b/packages/opentelemetry-plugin-http/src/utils.ts @@ -15,7 +15,14 @@ */ import { Attributes, CanonicalCode, Span, Status } from '@opentelemetry/types'; -import { ClientRequest, IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, RequestOptions, ServerResponse } from 'http'; +import { + ClientRequest, + IncomingHttpHeaders, + IncomingMessage, + OutgoingHttpHeaders, + RequestOptions, + ServerResponse, +} from 'http'; import { Socket } from 'net'; import * as url from 'url'; import { AttributeNames } from './enums/AttributeNames'; diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 8a6431ce00..6598514f9d 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -658,7 +658,7 @@ describe('HttpPlugin', () => { .reply(404); const req = http.request(`${host}/`); req.on('response', response => { - response.on('data', () => { }); + response.on('data', () => {}); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts index 416713e14b..92e3cd0534 100644 --- a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts @@ -16,7 +16,10 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; import { PluginOptions, Span, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts index af72abffbc..1ca48f9002 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts @@ -670,7 +670,7 @@ describe('HttpsPlugin', () => { .reply(404); const req = https.request(`${host}/`); req.on('response', response => { - response.on('data', () => { }); + response.on('data', () => {}); response.on('end', () => { const spans = memoryExporter.getFinishedSpans(); const [span] = spans; diff --git a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts index 097a806c47..ded229c0c4 100644 --- a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts @@ -17,7 +17,10 @@ import { NoopLogger } from '@opentelemetry/core'; import { NodeTracer } from '@opentelemetry/node'; import { AttributeNames, Http } from '@opentelemetry/plugin-http'; -import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; import { PluginOptions, Span, SpanKind } from '@opentelemetry/types'; import * as assert from 'assert'; import * as http from 'http'; diff --git a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts index b94ef27a5b..a152629c84 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts @@ -14,9 +14,21 @@ * limitations under the License. */ -import { BasePlugin, hrTime, isWrapped, matchesAnyPattern, matchesPattern, otperformance } from '@opentelemetry/core'; +import { + BasePlugin, + hrTime, + isWrapped, + matchesAnyPattern, + matchesPattern, + otperformance, +} from '@opentelemetry/core'; import * as types from '@opentelemetry/types'; -import { addSpanNetworkEvent, getResource, parseUrl, PerformanceTimingNames as PTN } from '@opentelemetry/web'; +import { + addSpanNetworkEvent, + getResource, + parseUrl, + PerformanceTimingNames as PTN, +} from '@opentelemetry/web'; import * as shimmer from 'shimmer'; import { AttributeNames } from './enums/AttributeNames'; import { EventNames } from './enums/EventNames'; diff --git a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts index 9b0781cb2b..235b17c986 100644 --- a/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-types/src/trace/instrumentation/Plugin.ts @@ -106,9 +106,14 @@ export interface HttpCustomAttributeFunction { * These options are used by http plugins like http, https, and http2. */ export interface HttpPluginOptions { + /** Not trace all incoming requests that match paths */ ignoreIncomingPaths?: IgnoreMatcher[]; + /** Not trace all outgoing requests that match urls */ ignoreOutgoingUrls?: IgnoreMatcher[]; + /** Function for adding custom attributes */ applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; + /** The primary server name of the matched virtual host. */ + serverName?: string; } export type PropagateTraceHeaderCorsUrl = string | RegExp; From 401ceb2cf1cb99b8712e0656527c4a7e7af3e648 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 14:59:25 -0500 Subject: [PATCH 11/15] chore: rename _config to _options --- .../src/trace/instrumentation/BasePlugin.ts | 6 +++--- .../test/instrumentation/PluginLoader.test.ts | 6 +++--- packages/opentelemetry-plugin-dns/src/dns.ts | 5 +---- .../src/documentLoad.ts | 2 -- .../opentelemetry-plugin-grpc/src/grpc.ts | 3 --- .../opentelemetry-plugin-grpc/src/types.ts | 2 -- .../opentelemetry-plugin-http/src/http.ts | 20 +++++++++---------- .../src/pg-pool.ts | 4 ---- .../opentelemetry-plugin-pg-pool/src/types.ts | 2 -- .../opentelemetry-plugin-pg/src/pg.ts | 4 ---- .../opentelemetry-plugin-pg/src/types.ts | 2 -- .../src/xhr.ts | 3 +-- 12 files changed, 17 insertions(+), 42 deletions(-) diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts index 0e1c7d59fe..83a0a96bc5 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts @@ -37,19 +37,19 @@ export abstract class BasePlugin implements Plugin { protected _logger!: Logger; protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports - protected _config: PluginOptions = {}; + protected _options: PluginOptions = {}; enable( moduleExports: T, tracer: Tracer, logger: Logger, - config?: PluginOptions + options: PluginOptions = {}; ): T { this._moduleExports = moduleExports; this._tracer = tracer; this._logger = logger; this._internalFilesExports = this._loadInternalFilesExports(); - this._config = config || {}; + this._options = options return this.patch(); } diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index 0c7654b56d..9bc16dd553 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -172,7 +172,7 @@ describe('PluginLoader', () => { const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.deepStrictEqual( - (pluginLoader['_plugins'][0] as any)._config.dns.ignoreHostnames, + (pluginLoader['_plugins'][0] as any)._options.dns.ignoreHostnames, ['shared option'] ); assert.strictEqual(httpModule.get(), 'patched'); @@ -190,7 +190,7 @@ describe('PluginLoader', () => { const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); assert.deepStrictEqual( - (pluginLoader['_plugins'][0] as any)._config.http.ignoreOutgoingUrls, + (pluginLoader['_plugins'][0] as any)._options.http.ignoreOutgoingUrls, ['plugin specific option'] ); assert.strictEqual(httpModule.get(), 'patched'); @@ -207,7 +207,7 @@ describe('PluginLoader', () => { // The hook is only called the first time the module is loaded. const httpModule = require('http'); assert.strictEqual(pluginLoader['_plugins'].length, 1); - assert.deepStrictEqual((pluginLoader['_plugins'][0] as any)._config, { + assert.deepStrictEqual((pluginLoader['_plugins'][0] as any)._options, { dns: { ignoreHostnames: ['shared'] }, http: { ignoreOutgoingUrls: ['plugin specific option'] }, }); diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index ec8a8f153d..319b2f2bc9 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -16,7 +16,6 @@ import { BasePlugin, matchesAnyPattern } from '@opentelemetry/core'; import { - PluginOptions, Span, SpanKind, SpanOptions, @@ -40,13 +39,11 @@ import * as utils from './utils'; */ export class DnsPlugin extends BasePlugin { readonly component: string; - protected _config!: PluginOptions; constructor(readonly moduleName: string, readonly version: string) { super(); // For now component is equal to moduleName but it can change in the future. this.component = this.moduleName; - this._config = {}; } /** Patches DNS functions. */ @@ -111,7 +108,7 @@ export class DnsPlugin extends BasePlugin { if ( matchesAnyPattern( hostname, - plugin._config.dns && plugin._config.dns.ignoreHostnames + plugin._options.dns && plugin._options.dns.ignoreHostnames ) ) { return original.apply(this, [hostname, ...args]); diff --git a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts index 8c025a1f0b..5040afa56c 100644 --- a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -37,7 +37,6 @@ export class DocumentLoad extends BasePlugin { readonly component: string = 'document-load'; readonly version: string = '1'; moduleName = this.component; - protected _config!: PluginOptions; /** * @@ -46,7 +45,6 @@ export class DocumentLoad extends BasePlugin { constructor(config: PluginOptions = {}) { super(); this._onDocumentLoaded = this._onDocumentLoaded.bind(this); - this._config = config; } /** diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index 2fba28743e..dde0ea2981 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -53,11 +53,8 @@ export class GrpcPlugin extends BasePlugin { static readonly component = 'grpc'; readonly supportedVersions = ['1.*']; - protected _config!: GrpcPluginOptions; - constructor(readonly moduleName: string, readonly version: string) { super(); - this._config = {}; } protected readonly _internalFilesList: ModuleExportsMapping = { diff --git a/packages/opentelemetry-plugin-grpc/src/types.ts b/packages/opentelemetry-plugin-grpc/src/types.ts index 15f1f3d119..7f9a634611 100644 --- a/packages/opentelemetry-plugin-grpc/src/types.ts +++ b/packages/opentelemetry-plugin-grpc/src/types.ts @@ -27,8 +27,6 @@ export type SendUnaryDataCallback = ( flags?: grpcModule.writeFlags ) => void; -export interface GrpcPluginOptions {} - interface GrpcStatus { code: number; details: string; diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 21303fc3a0..fa77fcddae 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -51,7 +51,6 @@ import { Socket } from 'net'; */ export class HttpPlugin extends BasePlugin { readonly component: string; - protected _config!: PluginOptions; /** keep track on spans not ended */ private readonly _spanNotEnded: WeakSet; @@ -60,7 +59,6 @@ export class HttpPlugin extends BasePlugin { // For now component is equal to moduleName but it can change in the future. this.component = this.moduleName; this._spanNotEnded = new WeakSet(); - this._config = {}; } /** Patches HTTP incoming and outcoming request functions. */ @@ -217,13 +215,13 @@ export class HttpPlugin extends BasePlugin { span.setStatus(status); if ( - this._config.http && - this._config.http.applyCustomAttributesOnSpan + this._options.http && + this._options.http.applyCustomAttributesOnSpan ) { this._safeExecute( span, () => - this._config.http!.applyCustomAttributesOnSpan!( + this._options.http!.applyCustomAttributesOnSpan!( span, request, response @@ -281,7 +279,7 @@ export class HttpPlugin extends BasePlugin { if ( matchesAnyPattern( pathname, - plugin._config.http && plugin._config.http.ignoreIncomingPaths + plugin._options.http && plugin._options.http.ignoreIncomingPaths ) ) { return original.apply(this, [event, ...args]); @@ -294,7 +292,7 @@ export class HttpPlugin extends BasePlugin { kind: SpanKind.SERVER, attributes: utils.getIncomingRequestAttributes(request, { component: plugin.component, - serverName: plugin._config.http && plugin._config.http.serverName, + serverName: plugin._options.http && plugin._options.http.serverName, }), }; @@ -335,13 +333,13 @@ export class HttpPlugin extends BasePlugin { .setStatus(utils.parseResponseStatus(response.statusCode)); if ( - plugin._config.http && - plugin._config.http.applyCustomAttributesOnSpan + plugin._options.http && + plugin._options.http.applyCustomAttributesOnSpan ) { plugin._safeExecute( span, () => - plugin._config.http!.applyCustomAttributesOnSpan!( + plugin._options.http!.applyCustomAttributesOnSpan!( span, request, response @@ -392,7 +390,7 @@ export class HttpPlugin extends BasePlugin { utils.isOpenTelemetryRequest(options) || matchesAnyPattern( origin + pathname, - plugin._config.http && plugin._config.http.ignoreOutgoingUrls + plugin._options.http && plugin._options.http.ignoreOutgoingUrls ) ) { return original.apply(this, [options, ...args]); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts index be335c1203..81a790c47b 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts @@ -20,15 +20,12 @@ import { AttributeNames } from './enums'; import * as shimmer from 'shimmer'; import * as pgPoolTypes from 'pg-pool'; import { - PostgresPoolPluginOptions, PgPoolCallback, PgPoolExtended, } from './types'; import * as utils from './utils'; export class PostgresPoolPlugin extends BasePlugin { - protected _config: PostgresPoolPluginOptions; - static readonly COMPONENT = 'pg-pool'; static readonly DB_TYPE = 'sql'; @@ -36,7 +33,6 @@ export class PostgresPoolPlugin extends BasePlugin { constructor(readonly moduleName: string) { super(); - this._config = {}; } protected patch(): typeof pgPoolTypes { diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/types.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/types.ts index 5f9648faf2..233addc28f 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/types.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/types.ts @@ -17,8 +17,6 @@ import * as pgTypes from 'pg'; import * as pgPoolTypes from 'pg-pool'; -export interface PostgresPoolPluginOptions {} - export type PgPoolCallback = ( err: Error, client: any, diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts index a6841ae33a..2bb491a2b9 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts @@ -17,7 +17,6 @@ import { BasePlugin } from '@opentelemetry/core'; import { CanonicalCode, Span } from '@opentelemetry/types'; import { - PostgresPluginOptions, PgClientExtended, PgPluginQueryConfig, PostgresCallback, @@ -27,8 +26,6 @@ import * as shimmer from 'shimmer'; import * as utils from './utils'; export class PostgresPlugin extends BasePlugin { - protected _config: PostgresPluginOptions; - static readonly COMPONENT = 'pg'; static readonly DB_TYPE = 'sql'; @@ -38,7 +35,6 @@ export class PostgresPlugin extends BasePlugin { constructor(readonly moduleName: string) { super(); - this._config = {}; } protected patch(): typeof pgTypes { diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/types.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/types.ts index c9d1f70974..8c065e816d 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/types.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/types.ts @@ -16,8 +16,6 @@ import * as pgTypes from 'pg'; -export interface PostgresPluginOptions {} - export type PostgresCallback = (err: Error, res: object) => unknown; // These are not included in @types/pg, so manually define them. diff --git a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts index a152629c84..ef9e70e6db 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/xhr.ts @@ -50,7 +50,6 @@ export class XMLHttpRequestPlugin extends BasePlugin { readonly version: string = VERSION; moduleName = this.component; - protected _config!: types.PluginOptions; private _xhrOptions: types.XMLHttpRequestPluginOptions; private _ignoreOutgoingUrls: types.IgnoreMatcher[]; @@ -60,7 +59,7 @@ export class XMLHttpRequestPlugin extends BasePlugin { constructor(config: types.PluginOptions = {}) { super(); - this._config = config; + this._options = config; this._xhrOptions = config.xhr || {}; this._ignoreOutgoingUrls = (config.http && config.http.ignoreOutgoingUrls) || []; From 69cc7e52a6b1c2fca44eaf88df7a31be92a9a1e2 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 15:05:37 -0500 Subject: [PATCH 12/15] chore: fix typo --- .../src/trace/instrumentation/BasePlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts index 83a0a96bc5..fdcf05358d 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts @@ -43,13 +43,13 @@ export abstract class BasePlugin implements Plugin { moduleExports: T, tracer: Tracer, logger: Logger, - options: PluginOptions = {}; + options: PluginOptions = {} ): T { this._moduleExports = moduleExports; this._tracer = tracer; this._logger = logger; this._internalFilesExports = this._loadInternalFilesExports(); - this._options = options + this._options = options; return this.patch(); } From e35064a29ea844003f98375fda30e18fdad50123 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 15:06:16 -0500 Subject: [PATCH 13/15] chore: fix lint --- packages/opentelemetry-plugin-dns/src/dns.ts | 6 +----- .../opentelemetry-plugin-pg-pool/src/pg-pool.ts | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-plugin-dns/src/dns.ts b/packages/opentelemetry-plugin-dns/src/dns.ts index 319b2f2bc9..e97c3337ce 100644 --- a/packages/opentelemetry-plugin-dns/src/dns.ts +++ b/packages/opentelemetry-plugin-dns/src/dns.ts @@ -15,11 +15,7 @@ */ import { BasePlugin, matchesAnyPattern } from '@opentelemetry/core'; -import { - Span, - SpanKind, - SpanOptions, -} from '@opentelemetry/types'; +import { Span, SpanKind, SpanOptions } from '@opentelemetry/types'; import { LookupAddress } from 'dns'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts index 81a790c47b..ac58f6bb1a 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/src/pg-pool.ts @@ -19,10 +19,7 @@ import { CanonicalCode, SpanKind } from '@opentelemetry/types'; import { AttributeNames } from './enums'; import * as shimmer from 'shimmer'; import * as pgPoolTypes from 'pg-pool'; -import { - PgPoolCallback, - PgPoolExtended, -} from './types'; +import { PgPoolCallback, PgPoolExtended } from './types'; import * as utils from './utils'; export class PostgresPoolPlugin extends BasePlugin { From 34dc175da4dc9c670dbb00358c36cb74d05591c3 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 15:18:46 -0500 Subject: [PATCH 14/15] chore: remove old import --- packages/opentelemetry-plugin-grpc/src/grpc.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index dde0ea2981..d14da8fe1d 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -27,7 +27,6 @@ import { AttributeNames } from './enums/AttributeNames'; import { grpc, ModuleExportsMapping, - GrpcPluginOptions, ServerCallWithMeta, SendUnaryDataCallback, GrpcClientFunc, From e5c6798bdc7d3fabd3c4399b6b202887f38881b2 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 2 Jan 2020 15:35:09 -0500 Subject: [PATCH 15/15] chore: fix compilation --- packages/opentelemetry-plugin-http/src/http.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index fa77fcddae..e9c28ea070 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -17,7 +17,6 @@ import { BasePlugin, isValid, matchesAnyPattern } from '@opentelemetry/core'; import { CanonicalCode, - PluginOptions, Span, SpanKind, SpanOptions,