From 030aff30f7725d0bc024c0b00e83d479167a9f9c Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 8 Nov 2024 17:39:06 +0100 Subject: [PATCH] feat(instrumentation-http)!: reduce public API surface by removing exports and making protected methods private (#5124) Co-authored-by: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> --- experimental/CHANGELOG.md | 35 ++++++++++ .../src/http.ts | 24 +++---- .../src/index.ts | 34 ---------- .../src/internal-types.ts | 64 +++++++++++++++++++ .../src/types.ts | 49 -------------- .../src/utils.ts | 2 +- .../test/functionals/http-enable.test.ts | 3 +- .../test/functionals/http-metrics.test.ts | 2 +- .../test/functionals/utils.test.ts | 2 +- 9 files changed, 116 insertions(+), 99 deletions(-) create mode 100644 experimental/packages/opentelemetry-instrumentation-http/src/internal-types.ts diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 8306297a22..7935a1df7f 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -7,6 +7,41 @@ All notable changes to experimental packages in this project will be documented ### :boom: Breaking Change +* feat(instrumentation-http)!: reduce public API surface by removing exports and making protected methods private [#5124](https://github.com/open-telemetry/opentelemetry-js/pull/5124) @pichlermarc + * (user-facing) the following exports were intended for internal use only and have been removed without replacement + * extractHostnameAndPort + * getAbsoluteUrl + * getIncomingRequestAttributes + * getIncomingRequestAttributesOnResponse + * getIncomingRequestMetricAttributes + * getIncomingRequestMetricAttributesOnResponse + * getOutgoingRequestAttributes + * getOutgoingRequestAttributesOnResponse + * getOutgoingRequestMetricAttributes + * getOutgoingRequestMetricAttributesOnResponse + * getRequestInfo + * headerCapture + * isCompressed + * isValidOptionsType + * parseResponseStatus + * satisfiesPattern + * setAttributesFromHttpKind + * setRequestContentLengthAttribute + * setResponseContentLengthAttribute + * setSpanWithError + * RequestSignature + * RequestFunction + * ParsedRequestOptions + * IgnoreMatcher + * Https + * HttpRequestArgs + * HttpCallbackOptional + * HttpCallback + * Http + * GetFunction + * Func + * Err + ### :rocket: (Enhancement) ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 98a7ea2e04..eec4394035 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -40,15 +40,7 @@ import type * as https from 'https'; import { Socket } from 'net'; import * as semver from 'semver'; import * as url from 'url'; -import { - Err, - Func, - Http, - HttpInstrumentationConfig, - HttpRequestArgs, - Https, - SemconvStability, -} from './types'; +import { HttpInstrumentationConfig } from './types'; import { VERSION } from './version'; import { InstrumentationBase, @@ -90,6 +82,14 @@ import { parseResponseStatus, setSpanWithError, } from './utils'; +import { + Err, + Func, + Http, + HttpRequestArgs, + Https, + SemconvStability, +} from './internal-types'; /** * `node:http` and `node:https` instrumentation for OpenTelemetry @@ -326,7 +326,7 @@ export class HttpInstrumentation extends InstrumentationBase boolean ): ((this: unknown, event: string, ...args: unknown[]) => boolean) => { @@ -338,13 +338,13 @@ export class HttpInstrumentation extends InstrumentationBase): Func => { return this._outgoingRequestFunction(component, original); }; } - protected _getPatchOutgoingGetFunction( + private _getPatchOutgoingGetFunction( clientRequest: ( options: http.RequestOptions | string | url.URL, ...args: HttpRequestArgs diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/index.ts b/experimental/packages/opentelemetry-instrumentation-http/src/index.ts index 436951fc3a..67984fc270 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/index.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/index.ts @@ -16,46 +16,12 @@ export { HttpInstrumentation } from './http'; export { - Err, - Func, - GetFunction, - Http, - HttpCallback, - HttpCallbackOptional, HttpCustomAttributeFunction, HttpInstrumentationConfig, - HttpRequestArgs, HttpRequestCustomAttributeFunction, HttpResponseCustomAttributeFunction, - Https, IgnoreIncomingRequestFunction, - IgnoreMatcher, IgnoreOutgoingRequestFunction, - ParsedRequestOptions, - RequestFunction, - RequestSignature, StartIncomingSpanCustomAttributeFunction, StartOutgoingSpanCustomAttributeFunction, } from './types'; -export { - extractHostnameAndPort, - getAbsoluteUrl, - getIncomingRequestAttributes, - getIncomingRequestAttributesOnResponse, - getIncomingRequestMetricAttributes, - getIncomingRequestMetricAttributesOnResponse, - getOutgoingRequestAttributes, - getOutgoingRequestAttributesOnResponse, - getOutgoingRequestMetricAttributes, - getOutgoingRequestMetricAttributesOnResponse, - getRequestInfo, - headerCapture, - isCompressed, - isValidOptionsType, - parseResponseStatus, - satisfiesPattern, - setAttributesFromHttpKind, - setRequestContentLengthAttribute, - setResponseContentLengthAttribute, - setSpanWithError, -} from './utils'; diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/internal-types.ts b/experimental/packages/opentelemetry-instrumentation-http/src/internal-types.ts new file mode 100644 index 0000000000..14313fa0a4 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation-http/src/internal-types.ts @@ -0,0 +1,64 @@ +/* + * 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 type * as http from 'http'; +import type * as https from 'https'; +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; + +export type HttpCallbackOptional = HttpCallback | undefined; + +// from node 10+ +export type RequestSignature = [http.RequestOptions, HttpCallbackOptional] & + HttpCallback; + +export type HttpRequestArgs = Array; + +export type ParsedRequestOptions = + | (http.RequestOptions & Partial) + | http.RequestOptions; +export type Http = typeof http; +export type Https = typeof https; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type Func = (...args: any[]) => T; + +export interface Err extends Error { + errno?: number; + code?: string; + path?: string; + syscall?: string; + stack?: string; +} + +/** + * Tracks whether this instrumentation emits old experimental, + * new stable, or both semantic conventions. + * + * Enum values chosen such that the enum may be used as a bitmask. + */ +export const enum SemconvStability { + /** Emit only stable semantic conventions */ + STABLE = 0x1, + /** Emit only old semantic conventions*/ + OLD = 0x2, + /** Emit both stable and old semantic conventions*/ + DUPLICATE = 0x1 | 0x2, +} diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts index fc24995d3d..b1c979f412 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts @@ -14,40 +14,14 @@ * limitations under the License. */ import { Span, Attributes } from '@opentelemetry/api'; -import type * as http from 'http'; -import type * as https from 'https'; import { ClientRequest, - get, IncomingMessage, - request, ServerResponse, RequestOptions, } from 'http'; -import * as url from 'url'; import { InstrumentationConfig } from '@opentelemetry/instrumentation'; -export type IgnoreMatcher = string | RegExp | ((url: string) => boolean); -export type HttpCallback = (res: IncomingMessage) => void; -export type RequestFunction = typeof request; -export type GetFunction = typeof get; - -export type HttpCallbackOptional = HttpCallback | undefined; - -// from node 10+ -export type RequestSignature = [http.RequestOptions, HttpCallbackOptional] & - HttpCallback; - -export type HttpRequestArgs = Array; - -export type ParsedRequestOptions = - | (http.RequestOptions & Partial) - | http.RequestOptions; -export type Http = typeof http; -export type Https = typeof https; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type Func = (...args: any[]) => T; - export interface HttpCustomAttributeFunction { ( span: Span, @@ -114,26 +88,3 @@ export interface HttpInstrumentationConfig extends InstrumentationConfig { server?: { requestHeaders?: string[]; responseHeaders?: string[] }; }; } - -export interface Err extends Error { - errno?: number; - code?: string; - path?: string; - syscall?: string; - stack?: string; -} - -/** - * Tracks whether this instrumentation emits old experimental, - * new stable, or both semantic conventions. - * - * Enum values chosen such that the enum may be used as a bitmask. - */ -export const enum SemconvStability { - /** Emit only stable semantic conventions */ - STABLE = 0x1, - /** Emit only old semantic convetions */ - OLD = 0x2, - /** Emit both stable and old semantic convetions */ - DUPLICATE = 0x1 | 0x2, -} diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index ffbdeff675..97e7b28e5c 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -76,7 +76,7 @@ import { IgnoreMatcher, ParsedRequestOptions, SemconvStability, -} from './types'; +} from './internal-types'; import forwardedParse = require('forwarded-parse'); /** diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index f38d201834..b7878efc0a 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -66,7 +66,7 @@ import * as assert from 'assert'; import * as nock from 'nock'; import * as path from 'path'; import { HttpInstrumentation } from '../../src/http'; -import { HttpInstrumentationConfig, SemconvStability } from '../../src/types'; +import { HttpInstrumentationConfig } from '../../src/types'; import { assertSpan } from '../utils/assertSpan'; import { DummyPropagation } from '../utils/DummyPropagation'; import { httpRequest } from '../utils/httpRequest'; @@ -88,6 +88,7 @@ instrumentation.disable(); import * as http from 'http'; import { AttributeNames } from '../../src/enums/AttributeNames'; import { getRemoteClientAddress } from '../../src/utils'; +import { SemconvStability } from '../../src/internal-types'; const applyCustomAttributesOnSpanErrorMessage = 'bad applyCustomAttributesOnSpan function'; diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts index 9f9d7b4a35..907a67b10b 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts @@ -48,7 +48,7 @@ instrumentation.enable(); instrumentation.disable(); import * as http from 'http'; -import { SemconvStability } from '../../src/types'; +import { SemconvStability } from '../../src/internal-types'; import { getRPCMetadata, RPCType } from '@opentelemetry/core'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index 2d9911c7d3..80e48126fd 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -37,7 +37,7 @@ import { IgnoreMatcher, ParsedRequestOptions, SemconvStability, -} from '../../src/types'; +} from '../../src/internal-types'; import * as utils from '../../src/utils'; import { AttributeNames } from '../../src/enums/AttributeNames'; import { RPCType, setRPCMetadata } from '@opentelemetry/core';