diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ac5bb96d0..3dc782cfe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se * deps: set `@opentelemetry/api` dependency min version to 1.3.0 in `examples`, `experimental/packages`, `integration-tests` and `selenium-tests` [#4992](https://github.com/open-telemetry/opentelemetry-js/pull/4992) * refactor(sdk-metrics): replace `MetricsAttributes` with `Attributes` [#5021](https://github.com/open-telemetry/opentelemetry-js/pull/5021) @david-luna +* refactor(instrumentation-http): replace `SpanAttributes` and `MetricsAttributes` with `Attributes` [#5023](https://github.com/open-telemetry/opentelemetry-js/pull/5023) @david-luna ## 1.26.0 diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 81e56183a1..456b8d08e0 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -26,7 +26,7 @@ import { SpanStatusCode, trace, Histogram, - MetricAttributes, + Attributes, ValueType, } from '@opentelemetry/api'; import { @@ -331,7 +331,7 @@ export class HttpInstrumentation extends InstrumentationBase { const length = getContentLength(request.headers); if (length === null) return; @@ -231,13 +229,13 @@ export const setRequestContentLengthAttribute = ( /** * Adds attributes for response content-length and content-encoding HTTP headers * @param { IncomingMessage } Response object whose headers will be analyzed - * @param { SpanAttributes } SpanAttributes object to be modified + * @param { Attributes } Attributes object to be modified * * @deprecated this is for an older version of semconv. It is retained for compatibility using OTEL_SEMCONV_STABILITY_OPT_IN */ export const setResponseContentLengthAttribute = ( response: IncomingMessage, - attributes: SpanAttributes + attributes: Attributes ): void => { const length = getContentLength(response.headers); if (length === null) return; @@ -380,7 +378,7 @@ export const extractHostnameAndPort = ( /** * Returns outgoing request attributes scoped to the options passed to the request * @param {ParsedRequestOptions} requestOptions the same options used to make the request - * @param {{ component: string, hostname: string, hookAttributes?: SpanAttributes }} options used to pass data needed to create attributes + * @param {{ component: string, hostname: string, hookAttributes?: Attributes }} options used to pass data needed to create attributes * @param {SemconvStability} semconvStability determines which semconv version to use */ export const getOutgoingRequestAttributes = ( @@ -389,10 +387,10 @@ export const getOutgoingRequestAttributes = ( component: string; hostname: string; port: string | number; - hookAttributes?: SpanAttributes; + hookAttributes?: Attributes; }, semconvStability: SemconvStability -): SpanAttributes => { +): Attributes => { const hostname = options.hostname; const port = options.port; const method = requestOptions.method ?? 'GET'; @@ -404,7 +402,7 @@ export const getOutgoingRequestAttributes = ( headers, `${options.component}:` ); - const oldAttributes: SpanAttributes = { + const oldAttributes: Attributes = { [SEMATTRS_HTTP_URL]: urlFull, [SEMATTRS_HTTP_METHOD]: method, [SEMATTRS_HTTP_TARGET]: requestOptions.path || '/', @@ -446,12 +444,12 @@ export const getOutgoingRequestAttributes = ( /** * Returns outgoing request Metric attributes scoped to the request data - * @param {SpanAttributes} spanAttributes the span attributes + * @param {Attributes} spanAttributes the span attributes */ export const getOutgoingRequestMetricAttributes = ( - spanAttributes: SpanAttributes -): MetricAttributes => { - const metricAttributes: MetricAttributes = {}; + spanAttributes: Attributes +): Attributes => { + const metricAttributes: Attributes = {}; metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD]; metricAttributes[SEMATTRS_NET_PEER_NAME] = spanAttributes[SEMATTRS_NET_PEER_NAME]; @@ -465,7 +463,7 @@ export const getOutgoingRequestMetricAttributes = ( */ export const setAttributesFromHttpKind = ( kind: string | undefined, - attributes: SpanAttributes + attributes: Attributes ): void => { if (kind) { attributes[SEMATTRS_HTTP_FLAVOR] = kind; @@ -485,9 +483,9 @@ export const setAttributesFromHttpKind = ( export const getOutgoingRequestAttributesOnResponse = ( response: IncomingMessage, semconvStability: SemconvStability -): SpanAttributes => { +): Attributes => { const { statusCode, statusMessage, httpVersion, socket } = response; - const oldAttributes: SpanAttributes = {}; + const oldAttributes: Attributes = {}; const stableAttributes: Attributes = {}; if (statusCode != null) { @@ -527,12 +525,12 @@ export const getOutgoingRequestAttributesOnResponse = ( /** * Returns outgoing request Metric attributes scoped to the response data - * @param {SpanAttributes} spanAttributes the span attributes + * @param {Attributes} spanAttributes the span attributes */ export const getOutgoingRequestMetricAttributesOnResponse = ( - spanAttributes: SpanAttributes -): MetricAttributes => { - const metricAttributes: MetricAttributes = {}; + spanAttributes: Attributes +): Attributes => { + const metricAttributes: Attributes = {}; metricAttributes[SEMATTRS_NET_PEER_PORT] = spanAttributes[SEMATTRS_NET_PEER_PORT]; metricAttributes[SEMATTRS_HTTP_STATUS_CODE] = @@ -694,7 +692,7 @@ export function getRemoteClientAddress( /** * Returns incoming request attributes scoped to the request data * @param {IncomingMessage} request the request object - * @param {{ component: string, serverName?: string, hookAttributes?: SpanAttributes }} options used to pass data needed to create attributes + * @param {{ component: string, serverName?: string, hookAttributes?: Attributes }} options used to pass data needed to create attributes * @param {SemconvStability} semconvStability determines which semconv version to use */ export const getIncomingRequestAttributes = ( @@ -702,10 +700,10 @@ export const getIncomingRequestAttributes = ( options: { component: 'http' | 'https'; serverName?: string; - hookAttributes?: SpanAttributes; + hookAttributes?: Attributes; semconvStability: SemconvStability; } -): SpanAttributes => { +): Attributes => { const headers = request.headers; const userAgent = headers['user-agent']; const ips = headers['x-forwarded-for']; @@ -794,13 +792,13 @@ export const getIncomingRequestAttributes = ( /** * Returns incoming request Metric attributes scoped to the request data - * @param {SpanAttributes} spanAttributes the span attributes + * @param {Attributes} spanAttributes the span attributes * @param {{ component: string }} options used to pass data needed to create attributes */ export const getIncomingRequestMetricAttributes = ( - spanAttributes: SpanAttributes -): MetricAttributes => { - const metricAttributes: MetricAttributes = {}; + spanAttributes: Attributes +): Attributes => { + const metricAttributes: Attributes = {}; metricAttributes[SEMATTRS_HTTP_SCHEME] = spanAttributes[SEMATTRS_HTTP_SCHEME]; metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD]; metricAttributes[SEMATTRS_NET_HOST_NAME] = @@ -818,7 +816,7 @@ export const getIncomingRequestAttributesOnResponse = ( request: IncomingMessage, response: ServerResponse, semconvStability: SemconvStability -): SpanAttributes => { +): Attributes => { // take socket from the request, // since it may be detached from the response object in keep-alive mode const { socket } = request; @@ -829,7 +827,7 @@ export const getIncomingRequestAttributesOnResponse = ( }; const rpcMetadata = getRPCMetadata(context.active()); - const oldAttributes: SpanAttributes = {}; + const oldAttributes: Attributes = {}; if (socket) { const { localAddress, localPort, remoteAddress, remotePort } = socket; oldAttributes[SEMATTRS_NET_HOST_IP] = localAddress; @@ -858,12 +856,12 @@ export const getIncomingRequestAttributesOnResponse = ( /** * Returns incoming request Metric attributes scoped to the request data - * @param {SpanAttributes} spanAttributes the span attributes + * @param {Attributes} spanAttributes the span attributes */ export const getIncomingRequestMetricAttributesOnResponse = ( - spanAttributes: SpanAttributes -): MetricAttributes => { - const metricAttributes: MetricAttributes = {}; + spanAttributes: Attributes +): Attributes => { + const metricAttributes: Attributes = {}; metricAttributes[SEMATTRS_HTTP_STATUS_CODE] = spanAttributes[SEMATTRS_HTTP_STATUS_CODE]; metricAttributes[SEMATTRS_NET_HOST_PORT] = 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 e7270ebdc9..b6470e1f3e 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 @@ -21,7 +21,7 @@ import { Span as ISpan, SpanKind, trace, - SpanAttributes, + Attributes, DiagConsoleLogger, } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; @@ -144,13 +144,13 @@ export const responseHookFunction = ( export const startIncomingSpanHookFunction = ( request: IncomingMessage -): SpanAttributes => { +): Attributes => { return { guid: request.headers?.guid }; }; export const startOutgoingSpanHookFunction = ( request: RequestOptions -): SpanAttributes => { +): Attributes => { return { guid: request.headers?.guid }; }; 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 d64f795383..c731a296d9 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -14,13 +14,12 @@ * limitations under the License. */ import { - SpanAttributes, + Attributes, SpanStatusCode, ROOT_CONTEXT, SpanKind, TraceFlags, context, - Attributes, } from '@opentelemetry/api'; import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base'; import { @@ -362,7 +361,7 @@ describe('Utility', () => { // Verify the key in the given attributes is set to the given value, // and that no other HTTP Content Length attributes are set. function verifyValueInAttributes( - attributes: SpanAttributes, + attributes: Attributes, key: string | undefined, value: number ) { @@ -384,7 +383,7 @@ describe('Utility', () => { describe('setRequestContentLengthAttributes()', () => { it('should set request content-length uncompressed attribute with no content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const request = {} as IncomingMessage; request.headers = { @@ -400,7 +399,7 @@ describe('Utility', () => { }); it('should set request content-length uncompressed attribute with "identity" content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const request = {} as IncomingMessage; request.headers = { 'content-length': '1200', @@ -416,7 +415,7 @@ describe('Utility', () => { }); it('should set request content-length compressed attribute with "gzip" content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const request = {} as IncomingMessage; request.headers = { 'content-length': '1200', @@ -434,7 +433,7 @@ describe('Utility', () => { describe('setResponseContentLengthAttributes()', () => { it('should set response content-length uncompressed attribute with no content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const response = {} as IncomingMessage; @@ -451,7 +450,7 @@ describe('Utility', () => { }); it('should set response content-length uncompressed attribute with "identity" content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const response = {} as IncomingMessage; @@ -470,7 +469,7 @@ describe('Utility', () => { }); it('should set response content-length compressed attribute with "gzip" content-encoding header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const response = {} as IncomingMessage; @@ -489,7 +488,7 @@ describe('Utility', () => { }); it('should set no attributes with no content-length header', () => { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; const message = {} as IncomingMessage; message.headers = {