Skip to content

Commit

Permalink
feat(instrumentation-http)!: reduce public API surface by removing ex…
Browse files Browse the repository at this point in the history
…ports and making protected methods private (#5124)

Co-authored-by: Hector Hernandez <[email protected]>
  • Loading branch information
pichlermarc and hectorhdzg authored Nov 8, 2024
1 parent 72c9af9 commit 030aff3
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 99 deletions.
35 changes: 35 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -326,7 +326,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
/**
* Creates spans for incoming requests, restoring spans' context if applied.
*/
protected _getPatchIncomingRequestFunction(component: 'http' | 'https') {
private _getPatchIncomingRequestFunction(component: 'http' | 'https') {
return (
original: (event: string, ...args: unknown[]) => boolean
): ((this: unknown, event: string, ...args: unknown[]) => boolean) => {
Expand All @@ -338,13 +338,13 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
* Creates spans for outgoing requests, sending spans' context for distributed
* tracing.
*/
protected _getPatchOutgoingRequestFunction(component: 'http' | 'https') {
private _getPatchOutgoingRequestFunction(component: 'http' | 'https') {
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
return this._outgoingRequestFunction(component, original);
};
}

protected _getPatchOutgoingGetFunction(
private _getPatchOutgoingGetFunction(
clientRequest: (
options: http.RequestOptions | string | url.URL,
...args: HttpRequestArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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<HttpCallbackOptional | RequestSignature>;

export type ParsedRequestOptions =
| (http.RequestOptions & Partial<url.UrlWithParsedQuery>)
| http.RequestOptions;
export type Http = typeof http;
export type Https = typeof https;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Func<T> = (...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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpCallbackOptional | RequestSignature>;

export type ParsedRequestOptions =
| (http.RequestOptions & Partial<url.UrlWithParsedQuery>)
| http.RequestOptions;
export type Http = typeof http;
export type Https = typeof https;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Func<T> = (...args: any[]) => T;

export interface HttpCustomAttributeFunction {
(
span: Span,
Expand Down Expand Up @@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
IgnoreMatcher,
ParsedRequestOptions,
SemconvStability,
} from './types';
} from './internal-types';
import forwardedParse = require('forwarded-parse');

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 030aff3

Please sign in to comment.