From 059aa838bfa5a80f1ef6797c8a460424eb735139 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 22 Sep 2020 13:48:45 -0400 Subject: [PATCH] fix: avoid circular require in plugins (#1551) --- .../opentelemetry-plugin-http/src/http.ts | 5 ++-- .../opentelemetry-plugin-http/src/types.ts | 2 +- .../opentelemetry-plugin-https/src/https.ts | 15 ++++++++---- .../opentelemetry-plugin-https/src/utils.ts | 23 ------------------- 4 files changed, 13 insertions(+), 32 deletions(-) delete mode 100644 packages/opentelemetry-plugin-https/src/utils.ts diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 7f5e45b258..fbca014ece 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -29,10 +29,9 @@ import { NoRecordingSpan, getExtractedSpanContext, } from '@opentelemetry/core'; -import { +import type { ClientRequest, IncomingMessage, - request, RequestOptions, ServerResponse, } from 'http'; @@ -95,7 +94,7 @@ export class HttpPlugin extends BasePlugin { shimmer.wrap( this._moduleExports, 'get', - this._getPatchOutgoingGetFunction(request) + this._getPatchOutgoingGetFunction(this._moduleExports.request) ); } diff --git a/packages/opentelemetry-plugin-http/src/types.ts b/packages/opentelemetry-plugin-http/src/types.ts index 9f1149e269..82f19cf767 100644 --- a/packages/opentelemetry-plugin-http/src/types.ts +++ b/packages/opentelemetry-plugin-http/src/types.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import { PluginConfig, Span } from '@opentelemetry/api'; -import * as http from 'http'; +import type * as http from 'http'; import { ClientRequest, get, diff --git a/packages/opentelemetry-plugin-https/src/https.ts b/packages/opentelemetry-plugin-https/src/https.ts index 396587eaca..40dc543a9a 100644 --- a/packages/opentelemetry-plugin-https/src/https.ts +++ b/packages/opentelemetry-plugin-https/src/https.ts @@ -15,12 +15,11 @@ */ import { HttpPlugin, Func, HttpRequestArgs } from '@opentelemetry/plugin-http'; -import * as http from 'http'; -import * as https from 'https'; +import type * as http from 'http'; +import type * as https from 'https'; import { URL } from 'url'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; -import * as utils from './utils'; /** * Https instrumentation plugin for Opentelemetry @@ -70,7 +69,7 @@ export class HttpsPlugin extends HttpPlugin { shimmer.wrap( this._moduleExports, 'get', - this._getPatchHttpsOutgoingGetFunction(https.request) + this._getPatchHttpsOutgoingGetFunction(this._moduleExports.request) ); } @@ -88,7 +87,7 @@ export class HttpsPlugin extends HttpPlugin { // Makes sure options will have default HTTPS parameters if (typeof options === 'object' && !(options instanceof URL)) { options = Object.assign({}, options); - utils.setDefaultOptions(options as https.RequestOptions); + plugin._setDefaultOptions(options); } return plugin._getPatchOutgoingRequestFunction()(original)( options, @@ -98,6 +97,12 @@ export class HttpsPlugin extends HttpPlugin { }; } + private _setDefaultOptions(options: https.RequestOptions) { + options.protocol = options.protocol || 'https:'; + options.port = options.port || 443; + options.agent = options.agent || this._moduleExports.globalAgent; + } + /** Patches HTTPS outgoing get requests */ private _getPatchHttpsOutgoingGetFunction( clientRequest: ( diff --git a/packages/opentelemetry-plugin-https/src/utils.ts b/packages/opentelemetry-plugin-https/src/utils.ts deleted file mode 100644 index 8d702bdfc0..0000000000 --- a/packages/opentelemetry-plugin-https/src/utils.ts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as https from 'https'; - -export const setDefaultOptions = (options: https.RequestOptions) => { - options.protocol = options.protocol || 'https:'; - options.port = options.port || 443; - options.agent = options.agent || https.globalAgent; -};