From e7650585d75303eb318f3a2a9bbd7d41020f2328 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Wed, 18 Sep 2024 17:30:36 -0400 Subject: [PATCH 1/3] update to use new ATTR --- .../src/clientUtils.ts | 10 ++--- .../src/instrumentation.ts | 37 +++++++++---------- .../src/serverUtils.ts | 10 ++--- .../test/protobuf-ts-utils.ts | 25 +++++++++---- .../test/utils/assertionUtils.ts | 14 +++---- 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts index 0974b239482..7c16f331bee 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts @@ -26,7 +26,7 @@ import type { } from './internal-types'; import { propagation, context } from '@opentelemetry/api'; -import { SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions'; +import { ATTR_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions/incubating'; import { AttributeNames } from './enums/AttributeNames'; import { GRPC_STATUS_CODE_OK } from './status-code'; import { @@ -81,14 +81,14 @@ export function patchedCallback( if (err) { if (err.code) { span.setStatus(_grpcStatusCodeToSpanStatus(err.code)); - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, err.code); } span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, }); } else { - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); } span.end(); @@ -130,7 +130,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, + [ATTR_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); @@ -142,7 +142,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { } span.setStatus(_grpcStatusCodeToSpanStatus(status.code)); - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, status.code); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, status.code); endSpan(); }); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts index 3daec0b63c5..9a23c20ccee 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -56,12 +56,12 @@ import { InstrumentationBase, } from '@opentelemetry/instrumentation'; import { - SEMATTRS_NET_PEER_NAME, - SEMATTRS_NET_PEER_PORT, - SEMATTRS_RPC_METHOD, - SEMATTRS_RPC_SERVICE, - SEMATTRS_RPC_SYSTEM, -} from '@opentelemetry/semantic-conventions'; + ATTR_NET_PEER_NAME, + ATTR_NET_PEER_PORT, + ATTR_RPC_METHOD, + ATTR_RPC_SERVICE, + ATTR_RPC_SYSTEM, +} from '@opentelemetry/semantic-conventions/incubating'; import { shouldNotTraceServerCall, @@ -241,9 +241,9 @@ export class GrpcInstrumentation extends InstrumentationBase( span.setStatus({ code: SpanStatusCode.UNSET, }); - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); endSpan(); }); @@ -101,7 +101,7 @@ function serverStreamAndBidiHandler( span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, + [ATTR_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); }); @@ -131,7 +131,7 @@ function clientStreamAndUnaryHandler( code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code), message: err.message, }); - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, err.code); } span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, @@ -139,7 +139,7 @@ function clientStreamAndUnaryHandler( }); } else { span.setStatus({ code: SpanStatusCode.UNSET }); - span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); } span.end(); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts index 95233aa52ec..15dbc863db3 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts @@ -22,9 +22,10 @@ import { } from '@opentelemetry/sdk-trace-base'; import { assertPropagation, assertSpan } from './utils/assertionUtils'; import { - SEMATTRS_RPC_METHOD, - SEMATTRS_RPC_SERVICE, -} from '@opentelemetry/semantic-conventions'; + ATTR_RPC_METHOD, + ATTR_RPC_SERVICE, +} from '@opentelemetry/semantic-conventions/incubating'; +import { AttributeValues } from '../src/enums/AttributeValues'; export type SpanAssertionFunction = ( exporter: InMemorySpanExporter, @@ -61,10 +62,20 @@ function validateSpans( ); assertPropagation(serverSpan, clientSpan); - assertSpan('grpc', serverSpan, SpanKind.SERVER, validations); - assertSpan('grpc', clientSpan, SpanKind.CLIENT, validations); - assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_METHOD], rpcMethod); - assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_SERVICE], rpcService); + assertSpan( + AttributeValues.RPC_SYSTEM, + serverSpan, + SpanKind.SERVER, + validations + ); + assertSpan( + AttributeValues.RPC_SYSTEM, + clientSpan, + SpanKind.CLIENT, + validations + ); + assert.strictEqual(clientSpan.attributes[ATTR_RPC_METHOD], rpcMethod); + assert.strictEqual(clientSpan.attributes[ATTR_RPC_SERVICE], rpcService); } export function assertNoSpansExported( diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts index ccc35cf7ad9..4597297d013 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts @@ -23,10 +23,10 @@ import { hrTimeToMicroseconds, } from '@opentelemetry/core'; import { - SEMATTRS_NET_PEER_NAME, - SEMATTRS_NET_PEER_PORT, - SEMATTRS_RPC_GRPC_STATUS_CODE, -} from '@opentelemetry/semantic-conventions'; + ATTR_NET_PEER_NAME, + ATTR_NET_PEER_PORT, + ATTR_RPC_GRPC_STATUS_CODE, +} from '@opentelemetry/semantic-conventions/incubating'; export const grpcStatusCodeToOpenTelemetryStatusCode = ( status: GrpcStatus @@ -70,11 +70,11 @@ export const assertSpan = ( validations.netPeerPort !== undefined ) { assert.strictEqual( - span.attributes[SEMATTRS_NET_PEER_NAME], + span.attributes[ATTR_NET_PEER_NAME], validations.netPeerName ); assert.strictEqual( - span.attributes[SEMATTRS_NET_PEER_PORT], + span.attributes[ATTR_NET_PEER_PORT], validations.netPeerPort ); } @@ -86,7 +86,7 @@ export const assertSpan = ( grpcStatusCodeToOpenTelemetryStatusCode(validations.status) ); assert.strictEqual( - span.attributes[SEMATTRS_RPC_GRPC_STATUS_CODE], + span.attributes[ATTR_RPC_GRPC_STATUS_CODE], validations.status ); }; From 70062cba71ed5d3f19e2f0bd062d31d7c6ae3d34 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Thu, 19 Sep 2024 17:25:11 -0400 Subject: [PATCH 2/3] use old semattrs to avoid potential breaking --- .../src/clientUtils.ts | 10 ++--- .../src/instrumentation.ts | 39 +++++++++++-------- .../src/serverUtils.ts | 10 ++--- .../test/protobuf-ts-utils.ts | 10 ++--- .../test/utils/assertionUtils.ts | 14 +++---- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts index 7c16f331bee..0974b239482 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts @@ -26,7 +26,7 @@ import type { } from './internal-types'; import { propagation, context } from '@opentelemetry/api'; -import { ATTR_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions/incubating'; +import { SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions'; import { AttributeNames } from './enums/AttributeNames'; import { GRPC_STATUS_CODE_OK } from './status-code'; import { @@ -81,14 +81,14 @@ export function patchedCallback( if (err) { if (err.code) { span.setStatus(_grpcStatusCodeToSpanStatus(err.code)); - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, err.code); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code); } span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, }); } else { - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); } span.end(); @@ -130,7 +130,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [ATTR_RPC_GRPC_STATUS_CODE]: err.code, + [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); @@ -142,7 +142,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { } span.setStatus(_grpcStatusCodeToSpanStatus(status.code)); - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, status.code); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, status.code); endSpan(); }); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts index 9a23c20ccee..139c83ee012 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -56,12 +56,14 @@ import { InstrumentationBase, } from '@opentelemetry/instrumentation'; import { - ATTR_NET_PEER_NAME, - ATTR_NET_PEER_PORT, - ATTR_RPC_METHOD, - ATTR_RPC_SERVICE, - ATTR_RPC_SYSTEM, -} from '@opentelemetry/semantic-conventions/incubating'; + ATTR_CLIENT_ADDRESS, + ATTR_CLIENT_PORT, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_RPC_METHOD, + SEMATTRS_RPC_SERVICE, + SEMATTRS_RPC_SYSTEM, +} from '@opentelemetry/semantic-conventions'; import { shouldNotTraceServerCall, @@ -241,9 +243,9 @@ export class GrpcInstrumentation extends InstrumentationBase( span.setStatus({ code: SpanStatusCode.UNSET, }); - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); endSpan(); }); @@ -101,7 +101,7 @@ function serverStreamAndBidiHandler( span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [ATTR_RPC_GRPC_STATUS_CODE]: err.code, + [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); }); @@ -131,7 +131,7 @@ function clientStreamAndUnaryHandler( code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code), message: err.message, }); - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, err.code); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code); } span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, @@ -139,7 +139,7 @@ function clientStreamAndUnaryHandler( }); } else { span.setStatus({ code: SpanStatusCode.UNSET }); - span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); } span.end(); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts index 15dbc863db3..de357226328 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts @@ -22,9 +22,9 @@ import { } from '@opentelemetry/sdk-trace-base'; import { assertPropagation, assertSpan } from './utils/assertionUtils'; import { - ATTR_RPC_METHOD, - ATTR_RPC_SERVICE, -} from '@opentelemetry/semantic-conventions/incubating'; + SEMATTRS_RPC_METHOD, + SEMATTRS_RPC_SERVICE, +} from '@opentelemetry/semantic-conventions'; import { AttributeValues } from '../src/enums/AttributeValues'; export type SpanAssertionFunction = ( @@ -74,8 +74,8 @@ function validateSpans( SpanKind.CLIENT, validations ); - assert.strictEqual(clientSpan.attributes[ATTR_RPC_METHOD], rpcMethod); - assert.strictEqual(clientSpan.attributes[ATTR_RPC_SERVICE], rpcService); + assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_METHOD], rpcMethod); + assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_SERVICE], rpcService); } export function assertNoSpansExported( diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts index 4597297d013..ccc35cf7ad9 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts @@ -23,10 +23,10 @@ import { hrTimeToMicroseconds, } from '@opentelemetry/core'; import { - ATTR_NET_PEER_NAME, - ATTR_NET_PEER_PORT, - ATTR_RPC_GRPC_STATUS_CODE, -} from '@opentelemetry/semantic-conventions/incubating'; + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_RPC_GRPC_STATUS_CODE, +} from '@opentelemetry/semantic-conventions'; export const grpcStatusCodeToOpenTelemetryStatusCode = ( status: GrpcStatus @@ -70,11 +70,11 @@ export const assertSpan = ( validations.netPeerPort !== undefined ) { assert.strictEqual( - span.attributes[ATTR_NET_PEER_NAME], + span.attributes[SEMATTRS_NET_PEER_NAME], validations.netPeerName ); assert.strictEqual( - span.attributes[ATTR_NET_PEER_PORT], + span.attributes[SEMATTRS_NET_PEER_PORT], validations.netPeerPort ); } @@ -86,7 +86,7 @@ export const assertSpan = ( grpcStatusCodeToOpenTelemetryStatusCode(validations.status) ); assert.strictEqual( - span.attributes[ATTR_RPC_GRPC_STATUS_CODE], + span.attributes[SEMATTRS_RPC_GRPC_STATUS_CODE], validations.status ); }; From 5d949bc00a0defb5c523aa4eede0cd84154b8c22 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Thu, 19 Sep 2024 17:41:26 -0400 Subject: [PATCH 3/3] wip: add semconv stability opt-in logic --- .../src/instrumentation.ts | 59 ++++++++++++++++--- .../src/types.ts | 15 +++++ .../test/helper.ts | 2 + 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts index 139c83ee012..9a1aa048021 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -41,8 +41,10 @@ import type { metadataCaptureType, } from './internal-types'; import type { GrpcInstrumentationConfig } from './types'; +import { SemconvStability } from './types'; import { + Attributes, context, propagation, ROOT_CONTEXT, @@ -51,6 +53,7 @@ import { trace, Span, } from '@opentelemetry/api'; +import { getEnv } from '@opentelemetry/core'; import { InstrumentationNodeModuleDefinition, InstrumentationBase, @@ -92,9 +95,21 @@ import { VERSION } from './version'; export class GrpcInstrumentation extends InstrumentationBase { private _metadataCapture: metadataCaptureType; + private _semconvStability = SemconvStability.OLD; + constructor(config: GrpcInstrumentationConfig = {}) { super('@opentelemetry/instrumentation-grpc', VERSION, config); this._metadataCapture = this._createMetadataCapture(); + + for (const entry in getEnv().OTEL_SEMCONV_STABILITY_OPT_IN) { + if (entry.toLowerCase() === 'http/dup') { + // http/dup takes highest precedence. If it is found, there is no need to read the rest of the list + this._semconvStability = SemconvStability.DUPLICATE; + break; + } else if (entry.toLowerCase() === 'http') { + this._semconvStability = SemconvStability.STABLE; + } + } } init() { @@ -328,7 +343,11 @@ export class GrpcInstrumentation extends InstrumentationBase