From f9a0d3ec33fe48f5a9d4a649ff67d4b83efd2fa3 Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Fri, 22 Mar 2024 12:17:45 -0400 Subject: [PATCH 1/5] refactor(instr-http): use exported strings for semconv (#4573) * chore(instr-http): use exported strings for semconv * add changelog entry --- experimental/CHANGELOG.md | 2 + .../src/http.ts | 4 +- .../src/utils.ts | 135 ++++++++++-------- .../test/functionals/http-enable.test.ts | 39 +++-- .../test/functionals/http-metrics.test.ts | 33 +++-- .../test/functionals/https-enable.test.ts | 33 +++-- .../test/functionals/utils.test.ts | 52 ++++--- .../test/integrations/http-enable.test.ts | 18 +-- .../test/integrations/https-enable.test.ts | 15 +- .../test/utils/assertSpan.ts | 72 +++++----- 10 files changed, 211 insertions(+), 192 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index ad7e5593911..0db63bb216a 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -22,6 +22,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson + ### :bug: (Bug Fix) * fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540 diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 337d058033d..1bc56cb4993 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -58,7 +58,7 @@ import { } from '@opentelemetry/instrumentation'; import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core'; import { errorMonitor } from 'events'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions'; /** * Http instrumentation instrumentation for Opentelemetry @@ -746,7 +746,7 @@ export class HttpInstrumentation extends InstrumentationBase { code: utils.parseResponseStatus(SpanKind.SERVER, response.statusCode), }); - const route = attributes[SemanticAttributes.HTTP_ROUTE]; + const route = attributes[SEMATTRS_HTTP_ROUTE]; if (route) { span.updateName(`${request.method || 'GET'} ${route}`); } diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index a782b8ab60a..a36d1f44cde 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -22,8 +22,30 @@ import { SpanKind, } from '@opentelemetry/api'; import { - NetTransportValues, - SemanticAttributes, + NETTRANSPORTVALUES_IP_TCP, + NETTRANSPORTVALUES_IP_UDP, + SEMATTRS_HTTP_CLIENT_IP, + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_HTTP_HOST, + SEMATTRS_HTTP_METHOD, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_ROUTE, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_SERVER_NAME, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_HTTP_TARGET, + SEMATTRS_HTTP_URL, + SEMATTRS_HTTP_USER_AGENT, + SEMATTRS_NET_HOST_IP, + SEMATTRS_NET_HOST_NAME, + SEMATTRS_NET_HOST_PORT, + SEMATTRS_NET_PEER_IP, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import { IncomingHttpHeaders, @@ -167,10 +189,9 @@ export const setRequestContentLengthAttribute = ( if (length === null) return; if (isCompressed(request.headers)) { - attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH] = length; + attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH] = length; } else { - attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = - length; + attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length; } }; @@ -187,10 +208,9 @@ export const setResponseContentLengthAttribute = ( if (length === null) return; if (isCompressed(response.headers)) { - attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH] = length; + attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH] = length; } else { - attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = - length; + attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = length; } }; @@ -343,20 +363,19 @@ export const getOutgoingRequestAttributes = ( const headers = requestOptions.headers || {}; const userAgent = headers['user-agent']; const attributes: SpanAttributes = { - [SemanticAttributes.HTTP_URL]: getAbsoluteUrl( + [SEMATTRS_HTTP_URL]: getAbsoluteUrl( requestOptions, headers, `${options.component}:` ), - [SemanticAttributes.HTTP_METHOD]: method, - [SemanticAttributes.HTTP_TARGET]: requestOptions.path || '/', - [SemanticAttributes.NET_PEER_NAME]: hostname, - [SemanticAttributes.HTTP_HOST]: - requestOptions.headers?.host ?? `${hostname}:${port}`, + [SEMATTRS_HTTP_METHOD]: method, + [SEMATTRS_HTTP_TARGET]: requestOptions.path || '/', + [SEMATTRS_NET_PEER_NAME]: hostname, + [SEMATTRS_HTTP_HOST]: requestOptions.headers?.host ?? `${hostname}:${port}`, }; if (userAgent !== undefined) { - attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent; + attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent; } return Object.assign(attributes, options.hookAttributes); }; @@ -369,10 +388,9 @@ export const getOutgoingRequestMetricAttributes = ( spanAttributes: SpanAttributes ): MetricAttributes => { const metricAttributes: MetricAttributes = {}; - metricAttributes[SemanticAttributes.HTTP_METHOD] = - spanAttributes[SemanticAttributes.HTTP_METHOD]; - metricAttributes[SemanticAttributes.NET_PEER_NAME] = - spanAttributes[SemanticAttributes.NET_PEER_NAME]; + metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD]; + metricAttributes[SEMATTRS_NET_PEER_NAME] = + spanAttributes[SEMATTRS_NET_PEER_NAME]; //TODO: http.url attribute, it should substitute any parameters to avoid high cardinality. return metricAttributes; }; @@ -384,11 +402,11 @@ export const getOutgoingRequestMetricAttributes = ( export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => { const attributes: SpanAttributes = {}; if (kind) { - attributes[SemanticAttributes.HTTP_FLAVOR] = kind; + attributes[SEMATTRS_HTTP_FLAVOR] = kind; if (kind.toUpperCase() !== 'QUIC') { - attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_TCP; + attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_TCP; } else { - attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_UDP; + attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP; } } return attributes; @@ -406,13 +424,13 @@ export const getOutgoingRequestAttributesOnResponse = ( const attributes: SpanAttributes = {}; if (socket) { const { remoteAddress, remotePort } = socket; - attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress; - attributes[SemanticAttributes.NET_PEER_PORT] = remotePort; + attributes[SEMATTRS_NET_PEER_IP] = remoteAddress; + attributes[SEMATTRS_NET_PEER_PORT] = remotePort; } setResponseContentLengthAttribute(response, attributes); if (statusCode) { - attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode; + attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode; attributes[AttributeNames.HTTP_STATUS_TEXT] = ( statusMessage || '' ).toUpperCase(); @@ -430,12 +448,11 @@ export const getOutgoingRequestMetricAttributesOnResponse = ( spanAttributes: SpanAttributes ): MetricAttributes => { const metricAttributes: MetricAttributes = {}; - metricAttributes[SemanticAttributes.NET_PEER_PORT] = - spanAttributes[SemanticAttributes.NET_PEER_PORT]; - metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] = - spanAttributes[SemanticAttributes.HTTP_STATUS_CODE]; - metricAttributes[SemanticAttributes.HTTP_FLAVOR] = - spanAttributes[SemanticAttributes.HTTP_FLAVOR]; + metricAttributes[SEMATTRS_NET_PEER_PORT] = + spanAttributes[SEMATTRS_NET_PEER_PORT]; + metricAttributes[SEMATTRS_HTTP_STATUS_CODE] = + spanAttributes[SEMATTRS_HTTP_STATUS_CODE]; + metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR]; return metricAttributes; }; @@ -465,31 +482,31 @@ export const getIncomingRequestAttributes = ( 'localhost'; const serverName = options.serverName; const attributes: SpanAttributes = { - [SemanticAttributes.HTTP_URL]: getAbsoluteUrl( + [SEMATTRS_HTTP_URL]: getAbsoluteUrl( requestUrl, headers, `${options.component}:` ), - [SemanticAttributes.HTTP_HOST]: host, - [SemanticAttributes.NET_HOST_NAME]: hostname, - [SemanticAttributes.HTTP_METHOD]: method, - [SemanticAttributes.HTTP_SCHEME]: options.component, + [SEMATTRS_HTTP_HOST]: host, + [SEMATTRS_NET_HOST_NAME]: hostname, + [SEMATTRS_HTTP_METHOD]: method, + [SEMATTRS_HTTP_SCHEME]: options.component, }; if (typeof ips === 'string') { - attributes[SemanticAttributes.HTTP_CLIENT_IP] = ips.split(',')[0]; + attributes[SEMATTRS_HTTP_CLIENT_IP] = ips.split(',')[0]; } if (typeof serverName === 'string') { - attributes[SemanticAttributes.HTTP_SERVER_NAME] = serverName; + attributes[SEMATTRS_HTTP_SERVER_NAME] = serverName; } if (requestUrl) { - attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.path || '/'; + attributes[SEMATTRS_HTTP_TARGET] = requestUrl.path || '/'; } if (userAgent !== undefined) { - attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent; + attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent; } setRequestContentLengthAttribute(request, attributes); @@ -506,14 +523,11 @@ export const getIncomingRequestMetricAttributes = ( spanAttributes: SpanAttributes ): MetricAttributes => { const metricAttributes: MetricAttributes = {}; - metricAttributes[SemanticAttributes.HTTP_SCHEME] = - spanAttributes[SemanticAttributes.HTTP_SCHEME]; - metricAttributes[SemanticAttributes.HTTP_METHOD] = - spanAttributes[SemanticAttributes.HTTP_METHOD]; - metricAttributes[SemanticAttributes.NET_HOST_NAME] = - spanAttributes[SemanticAttributes.NET_HOST_NAME]; - metricAttributes[SemanticAttributes.HTTP_FLAVOR] = - spanAttributes[SemanticAttributes.HTTP_FLAVOR]; + metricAttributes[SEMATTRS_HTTP_SCHEME] = spanAttributes[SEMATTRS_HTTP_SCHEME]; + metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD]; + metricAttributes[SEMATTRS_NET_HOST_NAME] = + spanAttributes[SEMATTRS_NET_HOST_NAME]; + metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR]; //TODO: http.target attribute, it should substitute any parameters to avoid high cardinality. return metricAttributes; }; @@ -535,18 +549,18 @@ export const getIncomingRequestAttributesOnResponse = ( const attributes: SpanAttributes = {}; if (socket) { const { localAddress, localPort, remoteAddress, remotePort } = socket; - attributes[SemanticAttributes.NET_HOST_IP] = localAddress; - attributes[SemanticAttributes.NET_HOST_PORT] = localPort; - attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress; - attributes[SemanticAttributes.NET_PEER_PORT] = remotePort; + attributes[SEMATTRS_NET_HOST_IP] = localAddress; + attributes[SEMATTRS_NET_HOST_PORT] = localPort; + attributes[SEMATTRS_NET_PEER_IP] = remoteAddress; + attributes[SEMATTRS_NET_PEER_PORT] = remotePort; } - attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode; + attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode; attributes[AttributeNames.HTTP_STATUS_TEXT] = ( statusMessage || '' ).toUpperCase(); if (rpcMetadata?.type === RPCType.HTTP && rpcMetadata.route !== undefined) { - attributes[SemanticAttributes.HTTP_ROUTE] = rpcMetadata.route; + attributes[SEMATTRS_HTTP_ROUTE] = rpcMetadata.route; } return attributes; }; @@ -559,13 +573,12 @@ export const getIncomingRequestMetricAttributesOnResponse = ( spanAttributes: SpanAttributes ): MetricAttributes => { const metricAttributes: MetricAttributes = {}; - metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] = - spanAttributes[SemanticAttributes.HTTP_STATUS_CODE]; - metricAttributes[SemanticAttributes.NET_HOST_PORT] = - spanAttributes[SemanticAttributes.NET_HOST_PORT]; - if (spanAttributes[SemanticAttributes.HTTP_ROUTE] !== undefined) { - metricAttributes[SemanticAttributes.HTTP_ROUTE] = - spanAttributes[SemanticAttributes.HTTP_ROUTE]; + metricAttributes[SEMATTRS_HTTP_STATUS_CODE] = + spanAttributes[SEMATTRS_HTTP_STATUS_CODE]; + metricAttributes[SEMATTRS_NET_HOST_PORT] = + spanAttributes[SEMATTRS_NET_HOST_PORT]; + if (spanAttributes[SEMATTRS_HTTP_ROUTE] !== undefined) { + metricAttributes[SEMATTRS_HTTP_ROUTE] = spanAttributes[SEMATTRS_HTTP_ROUTE]; } return metricAttributes; }; 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 c3eeaf5ee92..414b1aeb818 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 @@ -30,8 +30,14 @@ import { SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import { - NetTransportValues, - SemanticAttributes, + NETTRANSPORTVALUES_IP_TCP, + SEMATTRS_HTTP_CLIENT_IP, + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_HTTP_ROUTE, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_NET_HOST_PORT, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as nock from 'nock'; @@ -211,11 +217,11 @@ describe('HttpInstrumentation', () => { assertSpan(incomingSpan, SpanKind.SERVER, validations); assertSpan(outgoingSpan, SpanKind.CLIENT, validations); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT], + incomingSpan.attributes[SEMATTRS_NET_HOST_PORT], serverPort ); assert.strictEqual( - outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT], + outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT], serverPort ); }); @@ -329,28 +335,25 @@ describe('HttpInstrumentation', () => { assert.strictEqual(spans.length, 2); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.HTTP_CLIENT_IP], + incomingSpan.attributes[SEMATTRS_HTTP_CLIENT_IP], '' ); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT], + incomingSpan.attributes[SEMATTRS_NET_HOST_PORT], serverPort ); assert.strictEqual( - outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT], + outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT], serverPort ); [ { span: incomingSpan, kind: SpanKind.SERVER }, { span: outgoingSpan, kind: SpanKind.CLIENT }, ].forEach(({ span, kind }) => { + assert.strictEqual(span.attributes[SEMATTRS_HTTP_FLAVOR], '1.1'); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_FLAVOR], - '1.1' - ); - assert.strictEqual( - span.attributes[SemanticAttributes.NET_TRANSPORT], - NetTransportValues.IP_TCP + span.attributes[SEMATTRS_NET_TRANSPORT], + NETTRANSPORTVALUES_IP_TCP ); assertSpan(span, kind, validations); }); @@ -363,10 +366,7 @@ describe('HttpInstrumentation', () => { const span = memoryExporter.getFinishedSpans()[0]; assert.strictEqual(span.kind, SpanKind.SERVER); - assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_ROUTE], - 'TheRoute' - ); + assert.strictEqual(span.attributes[SEMATTRS_HTTP_ROUTE], 'TheRoute'); assert.strictEqual(span.name, 'GET TheRoute'); }); @@ -796,10 +796,7 @@ describe('HttpInstrumentation', () => { const [span] = spans; assert.strictEqual(spans.length, 1); assert.ok(Object.keys(span.attributes).length > 6); - assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_STATUS_CODE], - 404 - ); + assert.strictEqual(span.attributes[SEMATTRS_HTTP_STATUS_CODE], 404); assert.strictEqual(span.status.code, SpanStatusCode.ERROR); done(); }); 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 0f484f08bf8..599865107c6 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 @@ -20,7 +20,16 @@ import { MeterProvider, } from '@opentelemetry/sdk-metrics'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_HTTP_METHOD, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_NET_HOST_NAME, + SEMATTRS_NET_HOST_PORT, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, +} from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import { HttpInstrumentation } from '../../src/http'; import { httpRequest } from '../utils/httpRequest'; @@ -91,27 +100,27 @@ describe('metrics', () => { requestCount ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.HTTP_SCHEME], + metrics[0].dataPoints[0].attributes[SEMATTRS_HTTP_SCHEME], 'http' ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.HTTP_METHOD], + metrics[0].dataPoints[0].attributes[SEMATTRS_HTTP_METHOD], 'GET' ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.HTTP_FLAVOR], + metrics[0].dataPoints[0].attributes[SEMATTRS_HTTP_FLAVOR], '1.1' ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.NET_HOST_NAME], + metrics[0].dataPoints[0].attributes[SEMATTRS_NET_HOST_NAME], 'localhost' ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.HTTP_STATUS_CODE], + metrics[0].dataPoints[0].attributes[SEMATTRS_HTTP_STATUS_CODE], 200 ); assert.strictEqual( - metrics[0].dataPoints[0].attributes[SemanticAttributes.NET_HOST_PORT], + metrics[0].dataPoints[0].attributes[SEMATTRS_NET_HOST_PORT], 22346 ); @@ -128,23 +137,23 @@ describe('metrics', () => { requestCount ); assert.strictEqual( - metrics[1].dataPoints[0].attributes[SemanticAttributes.HTTP_METHOD], + metrics[1].dataPoints[0].attributes[SEMATTRS_HTTP_METHOD], 'GET' ); assert.strictEqual( - metrics[1].dataPoints[0].attributes[SemanticAttributes.NET_PEER_NAME], + metrics[1].dataPoints[0].attributes[SEMATTRS_NET_PEER_NAME], 'localhost' ); assert.strictEqual( - metrics[1].dataPoints[0].attributes[SemanticAttributes.NET_PEER_PORT], + metrics[1].dataPoints[0].attributes[SEMATTRS_NET_PEER_PORT], 22346 ); assert.strictEqual( - metrics[1].dataPoints[0].attributes[SemanticAttributes.HTTP_STATUS_CODE], + metrics[1].dataPoints[0].attributes[SEMATTRS_HTTP_STATUS_CODE], 200 ); assert.strictEqual( - metrics[1].dataPoints[0].attributes[SemanticAttributes.HTTP_FLAVOR], + metrics[1].dataPoints[0].attributes[SEMATTRS_HTTP_FLAVOR], '1.1' ); }); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index 668cf9566d5..ae6358cfbca 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -30,8 +30,13 @@ import { SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import { - NetTransportValues, - SemanticAttributes, + NETTRANSPORTVALUES_IP_TCP, + SEMATTRS_HTTP_CLIENT_IP, + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_NET_HOST_PORT, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as fs from 'fs'; @@ -170,11 +175,11 @@ describe('HttpsInstrumentation', () => { assertSpan(incomingSpan, SpanKind.SERVER, validations); assertSpan(outgoingSpan, SpanKind.CLIENT, validations); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT], + incomingSpan.attributes[SEMATTRS_NET_HOST_PORT], serverPort ); assert.strictEqual( - outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT], + outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT], serverPort ); }); @@ -264,15 +269,15 @@ describe('HttpsInstrumentation', () => { assert.strictEqual(spans.length, 2); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.HTTP_CLIENT_IP], + incomingSpan.attributes[SEMATTRS_HTTP_CLIENT_IP], '' ); assert.strictEqual( - incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT], + incomingSpan.attributes[SEMATTRS_NET_HOST_PORT], serverPort ); assert.strictEqual( - outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT], + outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT], serverPort ); @@ -280,13 +285,10 @@ describe('HttpsInstrumentation', () => { { span: incomingSpan, kind: SpanKind.SERVER }, { span: outgoingSpan, kind: SpanKind.CLIENT }, ].forEach(({ span, kind }) => { + assert.strictEqual(span.attributes[SEMATTRS_HTTP_FLAVOR], '1.1'); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_FLAVOR], - '1.1' - ); - assert.strictEqual( - span.attributes[SemanticAttributes.NET_TRANSPORT], - NetTransportValues.IP_TCP + span.attributes[SEMATTRS_NET_TRANSPORT], + NETTRANSPORTVALUES_IP_TCP ); assertSpan(span, kind, validations); }); @@ -694,10 +696,7 @@ describe('HttpsInstrumentation', () => { const [span] = spans; assert.strictEqual(spans.length, 1); assert.ok(Object.keys(span.attributes).length > 6); - assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_STATUS_CODE], - 404 - ); + assert.strictEqual(span.attributes[SEMATTRS_HTTP_STATUS_CODE], 404); assert.strictEqual(span.status.code, SpanStatusCode.ERROR); done(); }); 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 4001335355a..39260fd7cce 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -23,7 +23,14 @@ import { Attributes, } from '@opentelemetry/api'; import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_ROUTE, + SEMATTRS_HTTP_TARGET, +} from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import { IncomingMessage, ServerResponse } from 'http'; import { Socket } from 'net'; @@ -301,10 +308,7 @@ describe('Utility', () => { request, {} as ServerResponse ); - assert.deepStrictEqual( - attributes[SemanticAttributes.HTTP_ROUTE], - '/user/:id' - ); + assert.deepStrictEqual(attributes[SEMATTRS_HTTP_ROUTE], '/user/:id'); context.disable(); return done(); } @@ -318,20 +322,20 @@ describe('Utility', () => { const attributes = utils.getIncomingRequestAttributesOnResponse(request, { socket: {}, } as ServerResponse & { socket: Socket }); - assert.deepEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined); + assert.deepEqual(attributes[SEMATTRS_HTTP_ROUTE], undefined); }); }); describe('getIncomingRequestMetricAttributesOnResponse()', () => { it('should correctly add http_route if span has it', () => { const spanAttributes: Attributes = { - [SemanticAttributes.HTTP_ROUTE]: '/user/:id', + [SEMATTRS_HTTP_ROUTE]: '/user/:id', }; const metricAttributes = utils.getIncomingRequestMetricAttributesOnResponse(spanAttributes); assert.deepStrictEqual( - metricAttributes[SemanticAttributes.HTTP_ROUTE], + metricAttributes[SEMATTRS_HTTP_ROUTE], '/user/:id' ); }); @@ -340,10 +344,7 @@ describe('Utility', () => { const spanAttributes: Attributes = {}; const metricAttributes = utils.getIncomingRequestMetricAttributesOnResponse(spanAttributes); - assert.deepEqual( - metricAttributes[SemanticAttributes.HTTP_ROUTE], - undefined - ); + assert.deepEqual(metricAttributes[SEMATTRS_HTTP_ROUTE], undefined); }); }); // Verify the key in the given attributes is set to the given value, @@ -354,10 +355,10 @@ describe('Utility', () => { value: number ) { const SemanticAttributess = [ - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, ]; for (const attr of SemanticAttributess) { @@ -381,7 +382,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, 1200 ); }); @@ -397,7 +398,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, 1200 ); }); @@ -413,7 +414,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, 1200 ); }); @@ -432,7 +433,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, 1200 ); }); @@ -451,7 +452,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, 1200 ); }); @@ -470,7 +471,7 @@ describe('Utility', () => { verifyValueInAttributes( attributes, - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, 1200 ); }); @@ -501,7 +502,7 @@ describe('Utility', () => { const attributes = utils.getIncomingRequestAttributes(request, { component: 'http', }); - assert.strictEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined); + assert.strictEqual(attributes[SEMATTRS_HTTP_ROUTE], undefined); }); it('should set http.target as path in http span attributes', () => { @@ -515,10 +516,7 @@ describe('Utility', () => { const attributes = utils.getIncomingRequestAttributes(request, { component: 'http', }); - assert.strictEqual( - attributes[SemanticAttributes.HTTP_TARGET], - '/user/?q=val' - ); + assert.strictEqual(attributes[SEMATTRS_HTTP_TARGET], '/user/?q=val'); }); }); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts index a04027a2c95..1f7829c4d22 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts @@ -16,9 +16,11 @@ import { SpanKind, Span, context, propagation } from '@opentelemetry/api'; import { - HttpFlavorValues, - NetTransportValues, - SemanticAttributes, + HTTPFLAVORVALUES_HTTP_1_1, + NETTRANSPORTVALUES_IP_TCP, + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_HTTP_HOST, + SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as url from 'url'; @@ -236,12 +238,12 @@ describe('HttpInstrumentation Integration tests', () => { assert.strictEqual(span.name, 'GET'); assert.strictEqual(result.reqHeaders['x-foo'], 'foo'); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_FLAVOR], - HttpFlavorValues.HTTP_1_1 + span.attributes[SEMATTRS_HTTP_FLAVOR], + HTTPFLAVORVALUES_HTTP_1_1 ); assert.strictEqual( - span.attributes[SemanticAttributes.NET_TRANSPORT], - NetTransportValues.IP_TCP + span.attributes[SEMATTRS_NET_TRANSPORT], + NETTRANSPORTVALUES_IP_TCP ); assertSpan(span, SpanKind.CLIENT, validations); }); @@ -397,7 +399,7 @@ describe('HttpInstrumentation Integration tests', () => { assert.ok(span); assert.strictEqual(span.name, 'GET'); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_HOST], + span.attributes[SEMATTRS_HTTP_HOST], `localhost:${mockServerPort}` ); }); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts index 10afe76a803..aa19a8629f9 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts @@ -16,9 +16,10 @@ import { SpanKind, Span, context, propagation } from '@opentelemetry/api'; import { - HttpFlavorValues, - NetTransportValues, - SemanticAttributes, + HTTPFLAVORVALUES_HTTP_1_1, + NETTRANSPORTVALUES_IP_TCP, + SEMATTRS_HTTP_FLAVOR, + SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as http from 'http'; @@ -240,12 +241,12 @@ describe('HttpsInstrumentation Integration tests', () => { assert.strictEqual(span.name, 'GET'); assert.strictEqual(result.reqHeaders['x-foo'], 'foo'); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_FLAVOR], - HttpFlavorValues.HTTP_1_1 + span.attributes[SEMATTRS_HTTP_FLAVOR], + HTTPFLAVORVALUES_HTTP_1_1 ); assert.strictEqual( - span.attributes[SemanticAttributes.NET_TRANSPORT], - NetTransportValues.IP_TCP + span.attributes[SEMATTRS_NET_TRANSPORT], + NETTRANSPORTVALUES_IP_TCP ); assertSpan(span, SpanKind.CLIENT, validations); }); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts b/experimental/packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts index 1f1b0518c8c..24cac71298f 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts @@ -21,7 +21,24 @@ import { } from '@opentelemetry/api'; import { hrTimeToNanoseconds } from '@opentelemetry/core'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_METHOD, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_SERVER_NAME, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_HTTP_TARGET, + SEMATTRS_HTTP_URL, + SEMATTRS_HTTP_USER_AGENT, + SEMATTRS_NET_HOST_IP, + SEMATTRS_NET_HOST_PORT, + SEMATTRS_NET_PEER_IP, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, +} from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as http from 'http'; import * as utils from '../../src/utils'; @@ -55,15 +72,15 @@ export const assertSpan = ( span.status.message ); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_METHOD], + span.attributes[SEMATTRS_HTTP_METHOD], validations.httpMethod ); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_TARGET], + span.attributes[SEMATTRS_HTTP_TARGET], validations.path || validations.pathname ); assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_STATUS_CODE], + span.attributes[SEMATTRS_HTTP_STATUS_CODE], validations.httpStatusCode ); @@ -97,10 +114,7 @@ export const assertSpan = ( if (validations.reqHeaders) { const userAgent = validations.reqHeaders['user-agent']; if (userAgent) { - assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_USER_AGENT], - userAgent - ); + assert.strictEqual(span.attributes[SEMATTRS_HTTP_USER_AGENT], userAgent); } } if (span.kind === SpanKind.CLIENT) { @@ -112,36 +126,28 @@ export const assertSpan = ( validations.resHeaders['content-encoding'] !== 'identity' ) { assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH], + span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH], contentLength ); } else { assert.strictEqual( - span.attributes[ - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED - ], + span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], contentLength ); } } assert.strictEqual( - span.attributes[SemanticAttributes.NET_PEER_NAME], + span.attributes[SEMATTRS_NET_PEER_NAME], validations.hostname, 'must be consistent (PEER_NAME and hostname)' ); if (!validations.noNetPeer) { - assert.ok( - span.attributes[SemanticAttributes.NET_PEER_IP], - 'must have PEER_IP' - ); - assert.ok( - span.attributes[SemanticAttributes.NET_PEER_PORT], - 'must have PEER_PORT' - ); + assert.ok(span.attributes[SEMATTRS_NET_PEER_IP], 'must have PEER_IP'); + assert.ok(span.attributes[SEMATTRS_NET_PEER_PORT], 'must have PEER_PORT'); } assert.ok( - (span.attributes[SemanticAttributes.HTTP_URL] as string).indexOf( - span.attributes[SemanticAttributes.NET_PEER_NAME] as string + (span.attributes[SEMATTRS_HTTP_URL] as string).indexOf( + span.attributes[SEMATTRS_NET_PEER_NAME] as string ) > -1, 'must be consistent' ); @@ -155,35 +161,27 @@ export const assertSpan = ( validations.reqHeaders['content-encoding'] !== 'identity' ) { assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH], + span.attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH], contentLength ); } else { assert.strictEqual( - span.attributes[ - SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED - ], + span.attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], contentLength ); } } if (validations.serverName) { assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_SERVER_NAME], + span.attributes[SEMATTRS_HTTP_SERVER_NAME], validations.serverName, ' must have serverName attribute' ); - assert.ok( - span.attributes[SemanticAttributes.NET_HOST_PORT], - 'must have HOST_PORT' - ); - assert.ok( - span.attributes[SemanticAttributes.NET_HOST_IP], - 'must have HOST_IP' - ); + assert.ok(span.attributes[SEMATTRS_NET_HOST_PORT], 'must have HOST_PORT'); + assert.ok(span.attributes[SEMATTRS_NET_HOST_IP], 'must have HOST_IP'); } assert.strictEqual( - span.attributes[SemanticAttributes.HTTP_SCHEME], + span.attributes[SEMATTRS_HTTP_SCHEME], validations.component, ' must have http.scheme attribute' ); From 5fb65b4145eb90b868a90a566d632e788a293b32 Mon Sep 17 00:00:00 2001 From: Ievgen Makukh Date: Mon, 25 Mar 2024 11:03:41 +0200 Subject: [PATCH 2/5] perf(export): do not allocate arrays if resource has no pending async attributes (#4550) * perf(eport): do not allocate arrays if resource has no pending async attributes * perf(export): do not use splice if fit in one batch * Update CHANGELOG.md Co-authored-by: Marc Pichler --------- Co-authored-by: Marc Pichler --- CHANGELOG.md | 2 ++ .../src/export/BatchSpanProcessorBase.ts | 31 +++++++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2980539eb0..3c68dec71a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) +* perf(sdk-trace-base): do not allocate arrays if resource has no pending async attributes + ### :bug: (Bug Fix) * fix(sdk-metrics): increase the depth of the output to the console such that objects in the metric are printed fully to the console [#4522](https://github.com/open-telemetry/opentelemetry-js/pull/4522) @JacksonWeber diff --git a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts index f069aac0c7d..19e616dc081 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts @@ -181,7 +181,13 @@ export abstract class BatchSpanProcessorBase // Reset the finished spans buffer here because the next invocations of the _flush method // could pass the same finished spans to the exporter if the buffer is cleared // outside the execution of this callback. - const spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + let spans: ReadableSpan[]; + if (this._finishedSpans.length <= this._maxExportBatchSize) { + spans = this._finishedSpans; + this._finishedSpans = []; + } else { + spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + } const doExport = () => this._exporter.export(spans, result => { @@ -195,19 +201,24 @@ export abstract class BatchSpanProcessorBase ); } }); - const pendingResources = spans - .map(span => span.resource) - .filter(resource => resource.asyncAttributesPending); + + let pendingResources: Array> | null = null; + for (let i = 0, len = spans.length; i < len; i++) { + const span = spans[i]; + if ( + span.resource.asyncAttributesPending && + span.resource.waitForAsyncAttributes + ) { + pendingResources ??= []; + pendingResources.push(span.resource.waitForAsyncAttributes()); + } + } // Avoid scheduling a promise to make the behavior more predictable and easier to test - if (pendingResources.length === 0) { + if (pendingResources === null) { doExport(); } else { - Promise.all( - pendingResources.map( - resource => resource.waitForAsyncAttributes?.() - ) - ).then(doExport, err => { + Promise.all(pendingResources).then(doExport, err => { globalErrorHandler(err); reject(err); }); From fec6286127129ec0082e5dd080242501f15ea0af Mon Sep 17 00:00:00 2001 From: Martin Kuba Date: Mon, 25 Mar 2024 02:12:25 -0700 Subject: [PATCH 3/5] Remove domain from the Events API (#4569) * removed domain from the events API * lint * updated changelog --------- Co-authored-by: Marc Pichler --- experimental/CHANGELOG.md | 1 + experimental/packages/api-events/README.md | 2 +- .../api-events/src/NoopEventEmitterProvider.ts | 1 - experimental/packages/api-events/src/api/events.ts | 2 -- .../api-events/src/types/EventEmitterProvider.ts | 2 -- experimental/packages/api-events/test/api/api.test.ts | 4 ++-- .../noop-event-emitter-provider.test.ts | 11 ++++------- .../noop-implementations/noop-event-emitter.test.ts | 10 ++-------- 8 files changed, 10 insertions(+), 23 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 0db63bb216a..0906830ea45 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -19,6 +19,7 @@ All notable changes to experimental packages in this project will be documented * was used internally to keep track of the service client used by the exporter, as a side effect it allowed end-users to modify the gRPC service client that was used * `compression` * was used internally to keep track of the compression to use but was unintentionally exposed to the users. It allowed to read and write the value, writing, however, would have no effect. +* feat(api-events)!: removed domain from the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4569) ### :rocket: (Enhancement) diff --git a/experimental/packages/api-events/README.md b/experimental/packages/api-events/README.md index 3aff88eff62..1b8d168f627 100644 --- a/experimental/packages/api-events/README.md +++ b/experimental/packages/api-events/README.md @@ -39,7 +39,7 @@ api.events.getEventEmitterProvider(); const eventEmitter = api.events.getEventEmitter(name, version); // logging an event in an instrumentation library -eventEmitter.emit({ name: 'event-name', domain: 'event-domain' }); +eventEmitter.emit({ name: 'event-name' }); ``` ## Useful links diff --git a/experimental/packages/api-events/src/NoopEventEmitterProvider.ts b/experimental/packages/api-events/src/NoopEventEmitterProvider.ts index 95731b0a8d9..4d33536ac5d 100644 --- a/experimental/packages/api-events/src/NoopEventEmitterProvider.ts +++ b/experimental/packages/api-events/src/NoopEventEmitterProvider.ts @@ -22,7 +22,6 @@ import { NoopEventEmitter } from './NoopEventEmitter'; export class NoopEventEmitterProvider implements EventEmitterProvider { getEventEmitter( _name: string, - _domain: string, _version?: string | undefined, _options?: EventEmitterOptions | undefined ): EventEmitter { diff --git a/experimental/packages/api-events/src/api/events.ts b/experimental/packages/api-events/src/api/events.ts index f17c93a2d34..0888bd684b7 100644 --- a/experimental/packages/api-events/src/api/events.ts +++ b/experimental/packages/api-events/src/api/events.ts @@ -73,13 +73,11 @@ export class EventsAPI { */ public getEventEmitter( name: string, - domain: string, version?: string, options?: EventEmitterOptions ): EventEmitter { return this.getEventEmitterProvider().getEventEmitter( name, - domain, version, options ); diff --git a/experimental/packages/api-events/src/types/EventEmitterProvider.ts b/experimental/packages/api-events/src/types/EventEmitterProvider.ts index 276b2e627dc..26c45b8306f 100644 --- a/experimental/packages/api-events/src/types/EventEmitterProvider.ts +++ b/experimental/packages/api-events/src/types/EventEmitterProvider.ts @@ -26,14 +26,12 @@ export interface EventEmitterProvider { * schemaUrl pair is not already created. * * @param name The name of the event emitter or instrumentation library. - * @param domain The domain for events created by the event emitter. * @param version The version of the event emitter or instrumentation library. * @param options The options of the event emitter or instrumentation library. * @returns EventEmitter An event emitter with the given name and version. */ getEventEmitter( name: string, - domain: string, version?: string, options?: EventEmitterOptions ): EventEmitter; diff --git a/experimental/packages/api-events/test/api/api.test.ts b/experimental/packages/api-events/test/api/api.test.ts index e091688ff87..27a4b23b5c8 100644 --- a/experimental/packages/api-events/test/api/api.test.ts +++ b/experimental/packages/api-events/test/api/api.test.ts @@ -37,7 +37,7 @@ describe('API', () => { events.setGlobalEventEmitterProvider(new TestEventEmitterProvider()); const eventEmitter = events .getEventEmitterProvider() - .getEventEmitter('name', 'domain'); + .getEventEmitter('name'); assert.deepStrictEqual(eventEmitter, dummyEventEmitter); }); @@ -58,7 +58,7 @@ describe('API', () => { it('should return a event emitter instance from global provider', () => { events.setGlobalEventEmitterProvider(new TestEventEmitterProvider()); - const eventEmitter = events.getEventEmitter('myEventEmitter', 'domain'); + const eventEmitter = events.getEventEmitter('myEventEmitter'); assert.deepStrictEqual(eventEmitter, dummyEventEmitter); }); }); diff --git a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts b/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts index ad88d370585..346a339ce99 100644 --- a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts +++ b/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts @@ -23,18 +23,15 @@ describe('NoopLoggerProvider', () => { const eventEmitterProvider = new NoopEventEmitterProvider(); assert.ok( - eventEmitterProvider.getEventEmitter('emitter-name', 'domain') instanceof + eventEmitterProvider.getEventEmitter('emitter-name') instanceof NoopEventEmitter ); assert.ok( - eventEmitterProvider.getEventEmitter( - 'emitter-name', - 'domain', - 'v1' - ) instanceof NoopEventEmitter + eventEmitterProvider.getEventEmitter('emitter-name', 'v1') instanceof + NoopEventEmitter ); assert.ok( - eventEmitterProvider.getEventEmitter('emitter-name', 'domain', 'v1', { + eventEmitterProvider.getEventEmitter('emitter-name', 'v1', { schemaUrl: 'https://opentelemetry.io/schemas/1.7.0', }) instanceof NoopEventEmitter ); diff --git a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts b/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts index 933a3e6e885..1e4f0a4382f 100644 --- a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts +++ b/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts @@ -20,18 +20,12 @@ import { NoopEventEmitterProvider } from '../../src/NoopEventEmitterProvider'; describe('NoopEventEmitter', () => { it('constructor should not crash', () => { - const logger = new NoopEventEmitterProvider().getEventEmitter( - 'test-noop', - 'test-domain' - ); + const logger = new NoopEventEmitterProvider().getEventEmitter('test-noop'); assert(logger instanceof NoopEventEmitter); }); it('calling emit should not crash', () => { - const emitter = new NoopEventEmitterProvider().getEventEmitter( - 'test-noop', - 'test-domain' - ); + const emitter = new NoopEventEmitterProvider().getEventEmitter('test-noop'); emitter.emit({ name: 'event name', }); From 97af8e6d4d6312fec84817e7b35c182e008461f1 Mon Sep 17 00:00:00 2001 From: Marylia Gutierrez Date: Mon, 25 Mar 2024 11:40:56 -0400 Subject: [PATCH 4/5] feat(sdk-node): add `HostDetector` as default resource detector (#4566) * feat(node-sdk): add `HostDetector` as default resource detector Fixes #4282 - Add `HostDetector` to the list of default resource detectors - Add test to make sure host and process values are being set by default - Update from deprecated `sdk.detectResources()` to `sdk.start()` on tests - Update from deprecated `SemanticResourceAttributes.SERVICE_NAME` to `SEMRESATTRS_SERVICE_NAME` on SDK file * Update experimental/CHANGELOG.md * fix lint remove extra lint causing lint error --------- Co-authored-by: Marc Pichler --- experimental/CHANGELOG.md | 1 + .../opentelemetry-sdk-node/src/sdk.ts | 6 ++- .../opentelemetry-sdk-node/test/sdk.test.ts | 40 ++++++++++++++++--- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 0906830ea45..34a76714910 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -24,6 +24,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson +* feat(sdk-node): add `HostDetector` as default resource detector ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index c8ebfb48a7e..5e7cd3676aa 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -31,6 +31,7 @@ import { DetectorSync, detectResourcesSync, envDetector, + hostDetector, IResource, processDetector, Resource, @@ -46,7 +47,7 @@ import { NodeTracerConfig, NodeTracerProvider, } from '@opentelemetry/sdk-trace-node'; -import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import { NodeSDKConfiguration } from './types'; import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter'; import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; @@ -123,6 +124,7 @@ export class NodeSDK { this._resourceDetectors = configuration.resourceDetectors ?? [ envDetector, processDetector, + hostDetector, ]; this._serviceName = configuration.serviceName; @@ -328,7 +330,7 @@ export class NodeSDK { ? this._resource : this._resource.merge( new Resource({ - [SemanticResourceAttributes.SERVICE_NAME]: this._serviceName, + [SEMRESATTRS_SERVICE_NAME]: this._serviceName, }) ); diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 2019fdce290..b2d7bdb909d 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -57,6 +57,7 @@ import { TracerProviderWithEnvExporters } from '../src/TracerProviderWithEnvExpo import { envDetector, processDetector, + hostDetector, Resource, } from '@opentelemetry/resources'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; @@ -66,6 +67,10 @@ import { InMemoryLogRecordExporter, LoggerProvider, } from '@opentelemetry/sdk-logs'; +import { + SEMRESATTRS_HOST_NAME, + SEMRESATTRS_PROCESS_PID, +} from '@opentelemetry/semantic-conventions'; const DefaultContextManager = semver.gte(process.version, '14.8.0') ? AsyncLocalStorageContextManager @@ -527,9 +532,10 @@ describe('Node SDK', () => { }, }, envDetector, + hostDetector, ], }); - sdk.detectResources(); + sdk.start(); const resource = sdk['_resource']; await resource.waitForAsyncAttributes?.(); @@ -544,6 +550,28 @@ describe('Node SDK', () => { }); }); + describe('default resource detectors', () => { + it('default detectors populate values properly', async () => { + const sdk = new NodeSDK(); + sdk.start(); + const resource = sdk['_resource']; + await resource.waitForAsyncAttributes?.(); + + assertServiceResource(resource, { + instanceId: '627cc493', + name: 'my-service', + namespace: 'default', + version: '0.0.1', + }); + + assert.notEqual( + resource.attributes[SEMRESATTRS_PROCESS_PID], + undefined + ); + assert.notEqual(resource.attributes[SEMRESATTRS_HOST_NAME], undefined); + }); + }); + describe('with a buggy detector', () => { it('returns a merged resource', async () => { const sdk = new NodeSDK({ @@ -556,10 +584,11 @@ describe('Node SDK', () => { }, }, envDetector, + hostDetector, ], }); - sdk.detectResources(); + sdk.start(); const resource = sdk['_resource']; await resource.waitForAsyncAttributes?.(); @@ -609,7 +638,7 @@ describe('Node SDK', () => { DiagLogLevel.VERBOSE ); - sdk.detectResources(); + sdk.start(); await sdk['_resource'].waitForAsyncAttributes?.(); // Test that the Env Detector successfully found its resource and populated it with the right values. @@ -642,7 +671,7 @@ describe('Node SDK', () => { DiagLogLevel.DEBUG ); - sdk.detectResources(); + sdk.start(); assert.ok( callArgsContains( @@ -794,9 +823,10 @@ describe('Node SDK', () => { }, }, envDetector, + hostDetector, ], }); - sdk.detectResources(); + sdk.start(); const resource = sdk['_resource']; await resource.waitForAsyncAttributes?.(); From f6a075be3204befa970cd4b7d6101419ee5d562e Mon Sep 17 00:00:00 2001 From: Martin Kuba Date: Mon, 25 Mar 2024 09:35:20 -0700 Subject: [PATCH 5/5] Rename EventEmitter to EventLogger in the Events API (#4568) * renamed EventEmitter to EventLogger * updated changelog * renamed remaining references to emitter * lint --- experimental/CHANGELOG.md | 1 + experimental/packages/api-events/README.md | 18 +++---- ...NoopEventEmitter.ts => NoopEventLogger.ts} | 4 +- ...Provider.ts => NoopEventLoggerProvider.ts} | 20 +++---- .../packages/api-events/src/api/events.ts | 46 ++++++++-------- experimental/packages/api-events/src/index.ts | 6 +-- .../api-events/src/internal/global-utils.ts | 4 +- .../types/{EventEmitter.ts => EventLogger.ts} | 2 +- ...mitterOptions.ts => EventLoggerOptions.ts} | 2 +- ...tterProvider.ts => EventLoggerProvider.ts} | 24 ++++----- .../packages/api-events/test/api/api.test.ts | 54 +++++++++---------- .../api-events/test/internal/global.test.ts | 38 ++++++------- ....ts => noop-event-logger-provider.test.ts} | 18 +++---- ...tter.test.ts => noop-event-logger.test.ts} | 14 ++--- 14 files changed, 124 insertions(+), 127 deletions(-) rename experimental/packages/api-events/src/{NoopEventEmitter.ts => NoopEventLogger.ts} (86%) rename experimental/packages/api-events/src/{NoopEventEmitterProvider.ts => NoopEventLoggerProvider.ts} (56%) rename experimental/packages/api-events/src/types/{EventEmitter.ts => EventLogger.ts} (96%) rename experimental/packages/api-events/src/types/{EventEmitterOptions.ts => EventLoggerOptions.ts} (95%) rename experimental/packages/api-events/src/types/{EventEmitterProvider.ts => EventLoggerProvider.ts} (51%) rename experimental/packages/api-events/test/noop-implementations/{noop-event-emitter-provider.test.ts => noop-event-logger-provider.test.ts} (62%) rename experimental/packages/api-events/test/noop-implementations/{noop-event-emitter.test.ts => noop-event-logger.test.ts} (65%) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 34a76714910..62588e68771 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented * `compression` * was used internally to keep track of the compression to use but was unintentionally exposed to the users. It allowed to read and write the value, writing, however, would have no effect. * feat(api-events)!: removed domain from the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4569) +* fix(events-api)!: renamed EventEmitter to EventLogger in the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4568) ### :rocket: (Enhancement) diff --git a/experimental/packages/api-events/README.md b/experimental/packages/api-events/README.md index 1b8d168f627..d2ce3fea365 100644 --- a/experimental/packages/api-events/README.md +++ b/experimental/packages/api-events/README.md @@ -28,18 +28,18 @@ If you are writing an instrumentation library, or prefer to call the API methods ```javascript const api = require("@opentelemetry/api-events"); -/* A specific implementation of EventEmitterProvider comes from an SDK */ -const eventEmitterProvider = createEventEmitterProvider(); +/* A specific implementation of EventLoggerProvider comes from an SDK */ +const eventLoggerProvider = createEventLoggerProvider(); -/* Initialize EventEmitterProvider */ -api.events.setGlobalEventEmitterProvider(eventEmitterProvider); -/* returns eventEmitterProvider (no-op if a working provider has not been initialized) */ -api.events.getEventEmitterProvider(); -/* returns an event emitter from the registered global event emitter provider (no-op if a working provider has not been initialized) */ -const eventEmitter = api.events.getEventEmitter(name, version); +/* Initialize EventLoggerProvider */ +api.events.setGlobalEventLoggerProvider(eventLoggerProvider); +/* returns eventLoggerProvider (no-op if a working provider has not been initialized) */ +api.events.getEventLoggerProvider(); +/* returns an event logger from the registered global event logger provider (no-op if a working provider has not been initialized) */ +const eventLogger = api.events.getEventLogger(name, version); // logging an event in an instrumentation library -eventEmitter.emit({ name: 'event-name' }); +eventLogger.emit({ name: 'event-name' }); ``` ## Useful links diff --git a/experimental/packages/api-events/src/NoopEventEmitter.ts b/experimental/packages/api-events/src/NoopEventLogger.ts similarity index 86% rename from experimental/packages/api-events/src/NoopEventEmitter.ts rename to experimental/packages/api-events/src/NoopEventLogger.ts index b576e02bcf0..4ee4d7e23a7 100644 --- a/experimental/packages/api-events/src/NoopEventEmitter.ts +++ b/experimental/packages/api-events/src/NoopEventLogger.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import { EventEmitter } from './types/EventEmitter'; +import { EventLogger } from './types/EventLogger'; import { Event } from './types/Event'; -export class NoopEventEmitter implements EventEmitter { +export class NoopEventLogger implements EventLogger { emit(_event: Event): void {} } diff --git a/experimental/packages/api-events/src/NoopEventEmitterProvider.ts b/experimental/packages/api-events/src/NoopEventLoggerProvider.ts similarity index 56% rename from experimental/packages/api-events/src/NoopEventEmitterProvider.ts rename to experimental/packages/api-events/src/NoopEventLoggerProvider.ts index 4d33536ac5d..4fa5630b67d 100644 --- a/experimental/packages/api-events/src/NoopEventEmitterProvider.ts +++ b/experimental/packages/api-events/src/NoopEventLoggerProvider.ts @@ -14,19 +14,19 @@ * limitations under the License. */ -import { EventEmitterProvider } from './types/EventEmitterProvider'; -import { EventEmitter } from './types/EventEmitter'; -import { EventEmitterOptions } from './types/EventEmitterOptions'; -import { NoopEventEmitter } from './NoopEventEmitter'; +import { EventLoggerProvider } from './types/EventLoggerProvider'; +import { EventLogger } from './types/EventLogger'; +import { EventLoggerOptions } from './types/EventLoggerOptions'; +import { NoopEventLogger } from './NoopEventLogger'; -export class NoopEventEmitterProvider implements EventEmitterProvider { - getEventEmitter( +export class NoopEventLoggerProvider implements EventLoggerProvider { + getEventLogger( _name: string, _version?: string | undefined, - _options?: EventEmitterOptions | undefined - ): EventEmitter { - return new NoopEventEmitter(); + _options?: EventLoggerOptions | undefined + ): EventLogger { + return new NoopEventLogger(); } } -export const NOOP_EVENT_EMITTER_PROVIDER = new NoopEventEmitterProvider(); +export const NOOP_EVENT_LOGGER_PROVIDER = new NoopEventLoggerProvider(); diff --git a/experimental/packages/api-events/src/api/events.ts b/experimental/packages/api-events/src/api/events.ts index 0888bd684b7..c5a631c8fd4 100644 --- a/experimental/packages/api-events/src/api/events.ts +++ b/experimental/packages/api-events/src/api/events.ts @@ -20,10 +20,10 @@ import { _global, makeGetter, } from '../internal/global-utils'; -import { EventEmitterProvider } from '../types/EventEmitterProvider'; -import { NOOP_EVENT_EMITTER_PROVIDER } from '../NoopEventEmitterProvider'; -import { EventEmitter } from '../types/EventEmitter'; -import { EventEmitterOptions } from '../types/EventEmitterOptions'; +import { EventLoggerProvider } from '../types/EventLoggerProvider'; +import { NOOP_EVENT_LOGGER_PROVIDER } from '../NoopEventLoggerProvider'; +import { EventLogger } from '../types/EventLogger'; +import { EventLoggerOptions } from '../types/EventLoggerOptions'; export class EventsAPI { private static _instance?: EventsAPI; @@ -38,52 +38,48 @@ export class EventsAPI { return this._instance; } - public setGlobalEventEmitterProvider( - provider: EventEmitterProvider - ): EventEmitterProvider { + public setGlobalEventLoggerProvider( + provider: EventLoggerProvider + ): EventLoggerProvider { if (_global[GLOBAL_EVENTS_API_KEY]) { - return this.getEventEmitterProvider(); + return this.getEventLoggerProvider(); } - _global[GLOBAL_EVENTS_API_KEY] = makeGetter( + _global[GLOBAL_EVENTS_API_KEY] = makeGetter( API_BACKWARDS_COMPATIBILITY_VERSION, provider, - NOOP_EVENT_EMITTER_PROVIDER + NOOP_EVENT_LOGGER_PROVIDER ); return provider; } /** - * Returns the global event emitter provider. + * Returns the global event logger provider. * - * @returns EventEmitterProvider + * @returns EventLoggerProvider */ - public getEventEmitterProvider(): EventEmitterProvider { + public getEventLoggerProvider(): EventLoggerProvider { return ( _global[GLOBAL_EVENTS_API_KEY]?.(API_BACKWARDS_COMPATIBILITY_VERSION) ?? - NOOP_EVENT_EMITTER_PROVIDER + NOOP_EVENT_LOGGER_PROVIDER ); } /** - * Returns a event emitter from the global event emitter provider. + * Returns a event logger from the global event logger provider. * - * @returns EventEmitter + * @returns EventLogger */ - public getEventEmitter( + public getEventLogger( name: string, version?: string, - options?: EventEmitterOptions - ): EventEmitter { - return this.getEventEmitterProvider().getEventEmitter( - name, - version, - options - ); + options?: EventLoggerOptions + ): EventLogger { + return this.getEventLoggerProvider().getEventLogger(name, version, options); } - /** Remove the global event emitter provider */ + /** Remove the global event logger provider */ public disable(): void { delete _global[GLOBAL_EVENTS_API_KEY]; } diff --git a/experimental/packages/api-events/src/index.ts b/experimental/packages/api-events/src/index.ts index 63fadffd48b..dbd464027ea 100644 --- a/experimental/packages/api-events/src/index.ts +++ b/experimental/packages/api-events/src/index.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -export * from './types/EventEmitter'; -export * from './types/EventEmitterProvider'; +export * from './types/EventLogger'; +export * from './types/EventLoggerProvider'; export * from './types/Event'; -export * from './types/EventEmitterOptions'; +export * from './types/EventLoggerOptions'; import { EventsAPI } from './api/events'; export const events = EventsAPI.getInstance(); diff --git a/experimental/packages/api-events/src/internal/global-utils.ts b/experimental/packages/api-events/src/internal/global-utils.ts index d58b8901279..171cb0e1c7d 100644 --- a/experimental/packages/api-events/src/internal/global-utils.ts +++ b/experimental/packages/api-events/src/internal/global-utils.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { EventEmitterProvider } from '../types/EventEmitterProvider'; +import { EventLoggerProvider } from '../types/EventLoggerProvider'; import { _globalThis } from '../platform'; export const GLOBAL_EVENTS_API_KEY = Symbol.for( @@ -23,7 +23,7 @@ export const GLOBAL_EVENTS_API_KEY = Symbol.for( type Get = (version: number) => T; type OtelGlobal = Partial<{ - [GLOBAL_EVENTS_API_KEY]: Get; + [GLOBAL_EVENTS_API_KEY]: Get; }>; export const _global = _globalThis as OtelGlobal; diff --git a/experimental/packages/api-events/src/types/EventEmitter.ts b/experimental/packages/api-events/src/types/EventLogger.ts similarity index 96% rename from experimental/packages/api-events/src/types/EventEmitter.ts rename to experimental/packages/api-events/src/types/EventLogger.ts index 1222339516a..9a465b9c481 100644 --- a/experimental/packages/api-events/src/types/EventEmitter.ts +++ b/experimental/packages/api-events/src/types/EventLogger.ts @@ -16,7 +16,7 @@ import { Event } from './Event'; -export interface EventEmitter { +export interface EventLogger { /** * Emit an event. This method should only be used by instrumentations emitting events. * diff --git a/experimental/packages/api-events/src/types/EventEmitterOptions.ts b/experimental/packages/api-events/src/types/EventLoggerOptions.ts similarity index 95% rename from experimental/packages/api-events/src/types/EventEmitterOptions.ts rename to experimental/packages/api-events/src/types/EventLoggerOptions.ts index f5b983884eb..93aa29780a4 100644 --- a/experimental/packages/api-events/src/types/EventEmitterOptions.ts +++ b/experimental/packages/api-events/src/types/EventLoggerOptions.ts @@ -16,7 +16,7 @@ import { Attributes } from '@opentelemetry/api'; -export interface EventEmitterOptions { +export interface EventLoggerOptions { /** * The schemaUrl of the tracer or instrumentation library * @default '' diff --git a/experimental/packages/api-events/src/types/EventEmitterProvider.ts b/experimental/packages/api-events/src/types/EventLoggerProvider.ts similarity index 51% rename from experimental/packages/api-events/src/types/EventEmitterProvider.ts rename to experimental/packages/api-events/src/types/EventLoggerProvider.ts index 26c45b8306f..69eebe9202e 100644 --- a/experimental/packages/api-events/src/types/EventEmitterProvider.ts +++ b/experimental/packages/api-events/src/types/EventLoggerProvider.ts @@ -14,25 +14,25 @@ * limitations under the License. */ -import { EventEmitter } from './EventEmitter'; -import { EventEmitterOptions } from './EventEmitterOptions'; +import { EventLogger } from './EventLogger'; +import { EventLoggerOptions } from './EventLoggerOptions'; /** - * A registry for creating named {@link EventEmitter}s. + * A registry for creating named {@link EventLogger}s. */ -export interface EventEmitterProvider { +export interface EventLoggerProvider { /** - * Returns an EventEmitter, creating one if one with the given name, version, and + * Returns an EventLogger, creating one if one with the given name, version, and * schemaUrl pair is not already created. * - * @param name The name of the event emitter or instrumentation library. - * @param version The version of the event emitter or instrumentation library. - * @param options The options of the event emitter or instrumentation library. - * @returns EventEmitter An event emitter with the given name and version. + * @param name The name of the event logger or instrumentation library. + * @param version The version of the event logger or instrumentation library. + * @param options The options of the event logger or instrumentation library. + * @returns EventLogger An event logger with the given name and version. */ - getEventEmitter( + getEventLogger( name: string, version?: string, - options?: EventEmitterOptions - ): EventEmitter; + options?: EventLoggerOptions + ): EventLogger; } diff --git a/experimental/packages/api-events/test/api/api.test.ts b/experimental/packages/api-events/test/api/api.test.ts index 27a4b23b5c8..4fb0509397c 100644 --- a/experimental/packages/api-events/test/api/api.test.ts +++ b/experimental/packages/api-events/test/api/api.test.ts @@ -15,57 +15,57 @@ */ import * as assert from 'assert'; -import { EventEmitter, events } from '../../src'; -import { NoopEventEmitter } from '../../src/NoopEventEmitter'; -import { NoopEventEmitterProvider } from '../../src/NoopEventEmitterProvider'; +import { EventLogger, events } from '../../src'; +import { NoopEventLogger } from '../../src/NoopEventLogger'; +import { NoopEventLoggerProvider } from '../../src/NoopEventLoggerProvider'; describe('API', () => { - const dummyEventEmitter = new NoopEventEmitter(); + const dummyEventLogger = new NoopEventLogger(); - it('should expose a event emitter provider via getEventEmitterProvider', () => { - const provider = events.getEventEmitterProvider(); + it('should expose a event logger provider via getEventLoggerProvider', () => { + const provider = events.getEventLoggerProvider(); assert.ok(provider); assert.strictEqual(typeof provider, 'object'); }); - describe('GlobalEventEmitterProvider', () => { + describe('GlobalEventLoggerProvider', () => { beforeEach(() => { events.disable(); }); - it('should use the global event emitter provider', () => { - events.setGlobalEventEmitterProvider(new TestEventEmitterProvider()); - const eventEmitter = events - .getEventEmitterProvider() - .getEventEmitter('name'); - assert.deepStrictEqual(eventEmitter, dummyEventEmitter); + it('should use the global event logger provider', () => { + events.setGlobalEventLoggerProvider(new TestEventLoggerProvider()); + const eventLogger = events + .getEventLoggerProvider() + .getEventLogger('name'); + assert.deepStrictEqual(eventLogger, dummyEventLogger); }); it('should not allow overriding global provider if already set', () => { - const provider1 = new TestEventEmitterProvider(); - const provider2 = new TestEventEmitterProvider(); - events.setGlobalEventEmitterProvider(provider1); - assert.equal(events.getEventEmitterProvider(), provider1); - events.setGlobalEventEmitterProvider(provider2); - assert.equal(events.getEventEmitterProvider(), provider1); + const provider1 = new TestEventLoggerProvider(); + const provider2 = new TestEventLoggerProvider(); + events.setGlobalEventLoggerProvider(provider1); + assert.equal(events.getEventLoggerProvider(), provider1); + events.setGlobalEventLoggerProvider(provider2); + assert.equal(events.getEventLoggerProvider(), provider1); }); }); - describe('getEventEmitter', () => { + describe('getEventLogger', () => { beforeEach(() => { events.disable(); }); - it('should return a event emitter instance from global provider', () => { - events.setGlobalEventEmitterProvider(new TestEventEmitterProvider()); - const eventEmitter = events.getEventEmitter('myEventEmitter'); - assert.deepStrictEqual(eventEmitter, dummyEventEmitter); + it('should return a event logger instance from global provider', () => { + events.setGlobalEventLoggerProvider(new TestEventLoggerProvider()); + const eventLogger = events.getEventLogger('myEventLogger'); + assert.deepStrictEqual(eventLogger, dummyEventLogger); }); }); - class TestEventEmitterProvider extends NoopEventEmitterProvider { - override getEventEmitter(): EventEmitter { - return dummyEventEmitter; + class TestEventLoggerProvider extends NoopEventLoggerProvider { + override getEventLogger(): EventLogger { + return dummyEventLogger; } } }); diff --git a/experimental/packages/api-events/test/internal/global.test.ts b/experimental/packages/api-events/test/internal/global.test.ts index 0a4eb04f579..cdf71b556cb 100644 --- a/experimental/packages/api-events/test/internal/global.test.ts +++ b/experimental/packages/api-events/test/internal/global.test.ts @@ -19,7 +19,7 @@ import { _global, GLOBAL_EVENTS_API_KEY, } from '../../src/internal/global-utils'; -import { NoopEventEmitterProvider } from '../../src/NoopEventEmitterProvider'; +import { NoopEventLoggerProvider } from '../../src/NoopEventLoggerProvider'; const api1 = require('../../src') as typeof import('../../src'); @@ -34,8 +34,8 @@ describe('Global Utils', () => { assert.notStrictEqual(api1, api2); // that return separate noop instances to start assert.notStrictEqual( - api1.events.getEventEmitterProvider(), - api2.events.getEventEmitterProvider() + api1.events.getEventLoggerProvider(), + api2.events.getEventLoggerProvider() ); beforeEach(() => { @@ -43,38 +43,38 @@ describe('Global Utils', () => { api2.events.disable(); }); - it('should change the global event emitter provider', () => { - const original = api1.events.getEventEmitterProvider(); - const newEventEmitterProvider = new NoopEventEmitterProvider(); - api1.events.setGlobalEventEmitterProvider(newEventEmitterProvider); - assert.notStrictEqual(api1.events.getEventEmitterProvider(), original); + it('should change the global event logger provider', () => { + const original = api1.events.getEventLoggerProvider(); + const newEventLoggerProvider = new NoopEventLoggerProvider(); + api1.events.setGlobalEventLoggerProvider(newEventLoggerProvider); + assert.notStrictEqual(api1.events.getEventLoggerProvider(), original); assert.strictEqual( - api1.events.getEventEmitterProvider(), - newEventEmitterProvider + api1.events.getEventLoggerProvider(), + newEventLoggerProvider ); }); it('should load an instance from one which was set in the other', () => { - api1.events.setGlobalEventEmitterProvider(new NoopEventEmitterProvider()); + api1.events.setGlobalEventLoggerProvider(new NoopEventLoggerProvider()); assert.strictEqual( - api1.events.getEventEmitterProvider(), - api2.events.getEventEmitterProvider() + api1.events.getEventLoggerProvider(), + api2.events.getEventLoggerProvider() ); }); it('should disable both if one is disabled', () => { - const original = api1.events.getEventEmitterProvider(); + const original = api1.events.getEventLoggerProvider(); - api1.events.setGlobalEventEmitterProvider(new NoopEventEmitterProvider()); + api1.events.setGlobalEventLoggerProvider(new NoopEventLoggerProvider()); - assert.notStrictEqual(original, api1.events.getEventEmitterProvider()); + assert.notStrictEqual(original, api1.events.getEventLoggerProvider()); api2.events.disable(); - assert.strictEqual(original, api1.events.getEventEmitterProvider()); + assert.strictEqual(original, api1.events.getEventLoggerProvider()); }); it('should return the module NoOp implementation if the version is a mismatch', () => { - const original = api1.events.getEventEmitterProvider(); - api1.events.setGlobalEventEmitterProvider(new NoopEventEmitterProvider()); + const original = api1.events.getEventLoggerProvider(); + api1.events.setGlobalEventLoggerProvider(new NoopEventLoggerProvider()); const afterSet = _global[GLOBAL_EVENTS_API_KEY]!(-1); assert.strictEqual(original, afterSet); diff --git a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts b/experimental/packages/api-events/test/noop-implementations/noop-event-logger-provider.test.ts similarity index 62% rename from experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts rename to experimental/packages/api-events/test/noop-implementations/noop-event-logger-provider.test.ts index 346a339ce99..995eb473a34 100644 --- a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter-provider.test.ts +++ b/experimental/packages/api-events/test/noop-implementations/noop-event-logger-provider.test.ts @@ -15,25 +15,25 @@ */ import * as assert from 'assert'; -import { NoopEventEmitter } from '../../src/NoopEventEmitter'; -import { NoopEventEmitterProvider } from '../../src/NoopEventEmitterProvider'; +import { NoopEventLogger } from '../../src/NoopEventLogger'; +import { NoopEventLoggerProvider } from '../../src/NoopEventLoggerProvider'; describe('NoopLoggerProvider', () => { it('should not crash', () => { - const eventEmitterProvider = new NoopEventEmitterProvider(); + const eventLoggerProvider = new NoopEventLoggerProvider(); assert.ok( - eventEmitterProvider.getEventEmitter('emitter-name') instanceof - NoopEventEmitter + eventLoggerProvider.getEventLogger('logger-name') instanceof + NoopEventLogger ); assert.ok( - eventEmitterProvider.getEventEmitter('emitter-name', 'v1') instanceof - NoopEventEmitter + eventLoggerProvider.getEventLogger('logger-name', 'v1') instanceof + NoopEventLogger ); assert.ok( - eventEmitterProvider.getEventEmitter('emitter-name', 'v1', { + eventLoggerProvider.getEventLogger('logger-name', 'v1', { schemaUrl: 'https://opentelemetry.io/schemas/1.7.0', - }) instanceof NoopEventEmitter + }) instanceof NoopEventLogger ); }); }); diff --git a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts b/experimental/packages/api-events/test/noop-implementations/noop-event-logger.test.ts similarity index 65% rename from experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts rename to experimental/packages/api-events/test/noop-implementations/noop-event-logger.test.ts index 1e4f0a4382f..b29a6271bf8 100644 --- a/experimental/packages/api-events/test/noop-implementations/noop-event-emitter.test.ts +++ b/experimental/packages/api-events/test/noop-implementations/noop-event-logger.test.ts @@ -15,18 +15,18 @@ */ import * as assert from 'assert'; -import { NoopEventEmitter } from '../../src/NoopEventEmitter'; -import { NoopEventEmitterProvider } from '../../src/NoopEventEmitterProvider'; +import { NoopEventLogger } from '../../src/NoopEventLogger'; +import { NoopEventLoggerProvider } from '../../src/NoopEventLoggerProvider'; -describe('NoopEventEmitter', () => { +describe('NoopEventLogger', () => { it('constructor should not crash', () => { - const logger = new NoopEventEmitterProvider().getEventEmitter('test-noop'); - assert(logger instanceof NoopEventEmitter); + const logger = new NoopEventLoggerProvider().getEventLogger('test-noop'); + assert(logger instanceof NoopEventLogger); }); it('calling emit should not crash', () => { - const emitter = new NoopEventEmitterProvider().getEventEmitter('test-noop'); - emitter.emit({ + const logger = new NoopEventLoggerProvider().getEventLogger('test-noop'); + logger.emit({ name: 'event name', }); });