From 7b31b1e5ea6b8317f0ec7048ebde2248f6ddb7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martti=20Mouruj=C3=A4rvi?= Date: Sun, 14 Apr 2024 19:16:27 +0300 Subject: [PATCH 1/3] refactor(instr-grpc): move to use SEMATTRS --- experimental/CHANGELOG.md | 2 ++ .../src/clientUtils.ts | 13 +++---- .../src/instrumentation.ts | 34 ++++++++++--------- .../src/serverUtils.ts | 16 +++------ .../test/protobuf-ts-utils.ts | 15 ++++---- .../test/utils/assertionUtils.ts | 12 ++++--- 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 48dc912ca2..a9f1f8daaa 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -15,6 +15,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* refactor(instr-grpc): move to use SEMATTRS [#4633](https://github.com/open-telemetry/opentelemetry-js/pull/4633) + * feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600) ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/clientUtils.ts index 281e884d3f..0974b23948 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 { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +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,17 +81,14 @@ export function patchedCallback( if (err) { if (err.code) { span.setStatus(_grpcStatusCodeToSpanStatus(err.code)); - span.setAttribute(SemanticAttributes.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( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - GRPC_STATUS_CODE_OK - ); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); } span.end(); @@ -133,7 +130,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [SemanticAttributes.RPC_GRPC_STATUS_CODE]: err.code, + [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); @@ -145,7 +142,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) { } span.setStatus(_grpcStatusCodeToSpanStatus(status.code)); - span.setAttribute(SemanticAttributes.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 3a04c24e1f..6a14069402 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -56,7 +56,13 @@ import { isWrapped, InstrumentationBase, } from '@opentelemetry/instrumentation'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_RPC_METHOD, + SEMATTRS_RPC_SERVICE, + SEMATTRS_RPC_SYSTEM, +} from '@opentelemetry/semantic-conventions'; import { shouldNotTraceServerCall, @@ -274,10 +280,9 @@ export class GrpcInstrumentation extends InstrumentationBase { const span = instrumentation.tracer .startSpan(spanName, spanOptions) .setAttributes({ - [SemanticAttributes.RPC_SYSTEM]: - AttributeValues.RPC_SYSTEM, - [SemanticAttributes.RPC_METHOD]: method, - [SemanticAttributes.RPC_SERVICE]: service, + [SEMATTRS_RPC_SYSTEM]: AttributeValues.RPC_SYSTEM, + [SEMATTRS_RPC_METHOD]: method, + [SEMATTRS_RPC_SERVICE]: service, }); instrumentation._metadataCapture.server.captureRequestMetadata( @@ -469,9 +474,9 @@ export class GrpcInstrumentation extends InstrumentationBase { const span = instrumentation.tracer .startSpan(name, { kind: SpanKind.CLIENT }) .setAttributes({ - [SemanticAttributes.RPC_SYSTEM]: 'grpc', - [SemanticAttributes.RPC_METHOD]: method, - [SemanticAttributes.RPC_SERVICE]: service, + [SEMATTRS_RPC_SYSTEM]: 'grpc', + [SEMATTRS_RPC_METHOD]: method, + [SEMATTRS_RPC_SERVICE]: service, }); instrumentation.extractNetMetadata(this, span); @@ -514,9 +519,9 @@ export class GrpcInstrumentation extends InstrumentationBase { const span = this.tracer .startSpan(name, { kind: SpanKind.CLIENT }) .setAttributes({ - [SemanticAttributes.RPC_SYSTEM]: 'grpc', - [SemanticAttributes.RPC_METHOD]: methodAttributeValue, - [SemanticAttributes.RPC_SERVICE]: service, + [SEMATTRS_RPC_SYSTEM]: 'grpc', + [SEMATTRS_RPC_METHOD]: methodAttributeValue, + [SEMATTRS_RPC_SERVICE]: service, }); if (metadata != null) { @@ -529,12 +534,9 @@ export class GrpcInstrumentation extends InstrumentationBase { // set net.peer.* from target (e.g., "dns:otel-productcatalogservice:8080") as a hint to APMs const parsedUri = URI_REGEX.exec(client.getChannel().getTarget()); if (parsedUri != null && parsedUri.groups != null) { + span.setAttribute(SEMATTRS_NET_PEER_NAME, parsedUri.groups['name']); span.setAttribute( - SemanticAttributes.NET_PEER_NAME, - parsedUri.groups['name'] - ); - span.setAttribute( - SemanticAttributes.NET_PEER_PORT, + SEMATTRS_NET_PEER_PORT, parseInt(parsedUri.groups['port']) ); } diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/serverUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/serverUtils.ts index 142c65f3ce..c22d9dfca6 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/serverUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/serverUtils.ts @@ -38,7 +38,7 @@ import type { import type { IgnoreMatcher } from './types'; import { context, SpanStatusCode } from '@opentelemetry/api'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions'; import { _grpcStatusCodeToOpenTelemetryStatusCode, @@ -81,10 +81,7 @@ function serverStreamAndBidiHandler( span.setStatus({ code: SpanStatusCode.UNSET, }); - span.setAttribute( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - GRPC_STATUS_CODE_OK - ); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK); endSpan(); }); @@ -104,7 +101,7 @@ function serverStreamAndBidiHandler( span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, [AttributeNames.GRPC_ERROR_MESSAGE]: err.message, - [SemanticAttributes.RPC_GRPC_STATUS_CODE]: err.code, + [SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code, }); endSpan(); }); @@ -134,7 +131,7 @@ function clientStreamAndUnaryHandler( code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code), message: err.message, }); - span.setAttribute(SemanticAttributes.RPC_GRPC_STATUS_CODE, err.code); + span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code); } span.setAttributes({ [AttributeNames.GRPC_ERROR_NAME]: err.name, @@ -142,10 +139,7 @@ function clientStreamAndUnaryHandler( }); } else { span.setStatus({ code: SpanStatusCode.UNSET }); - span.setAttribute( - SemanticAttributes.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 2fc2a3d1be..95233aa52e 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts @@ -21,7 +21,10 @@ import { ReadableSpan, } from '@opentelemetry/sdk-trace-base'; import { assertPropagation, assertSpan } from './utils/assertionUtils'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_RPC_METHOD, + SEMATTRS_RPC_SERVICE, +} from '@opentelemetry/semantic-conventions'; export type SpanAssertionFunction = ( exporter: InMemorySpanExporter, @@ -60,14 +63,8 @@ function validateSpans( assertSpan('grpc', serverSpan, SpanKind.SERVER, validations); assertSpan('grpc', clientSpan, SpanKind.CLIENT, validations); - assert.strictEqual( - clientSpan.attributes[SemanticAttributes.RPC_METHOD], - rpcMethod - ); - assert.strictEqual( - clientSpan.attributes[SemanticAttributes.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 1b7966639e..ccc35cf7ad 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts @@ -22,7 +22,11 @@ import { hrTimeToMilliseconds, hrTimeToMicroseconds, } from '@opentelemetry/core'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_RPC_GRPC_STATUS_CODE, +} from '@opentelemetry/semantic-conventions'; export const grpcStatusCodeToOpenTelemetryStatusCode = ( status: GrpcStatus @@ -66,11 +70,11 @@ export const assertSpan = ( validations.netPeerPort !== undefined ) { assert.strictEqual( - span.attributes[SemanticAttributes.NET_PEER_NAME], + span.attributes[SEMATTRS_NET_PEER_NAME], validations.netPeerName ); assert.strictEqual( - span.attributes[SemanticAttributes.NET_PEER_PORT], + span.attributes[SEMATTRS_NET_PEER_PORT], validations.netPeerPort ); } @@ -82,7 +86,7 @@ export const assertSpan = ( grpcStatusCodeToOpenTelemetryStatusCode(validations.status) ); assert.strictEqual( - span.attributes[SemanticAttributes.RPC_GRPC_STATUS_CODE], + span.attributes[SEMATTRS_RPC_GRPC_STATUS_CODE], validations.status ); }; From 8d24c620d3ef0dd5404e4f4a04e8061322a68cb9 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 16 Apr 2024 10:57:55 +0200 Subject: [PATCH 2/3] Update experimental/CHANGELOG.md --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index a9f1f8daaa..5d5592d473 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -15,7 +15,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) -* refactor(instr-grpc): move to use SEMATTRS [#4633](https://github.com/open-telemetry/opentelemetry-js/pull/4633) +* refactor(instrumentation-grpc): move to use SEMATTRS [#4633](https://github.com/open-telemetry/opentelemetry-js/pull/4633) * feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600) From 2230896eaec60c7be21147a75df66bb937d5c1a2 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 16 Apr 2024 10:58:03 +0200 Subject: [PATCH 3/3] Update experimental/CHANGELOG.md --- experimental/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 5d5592d473..a25f8e8b3b 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -16,7 +16,6 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * refactor(instrumentation-grpc): move to use SEMATTRS [#4633](https://github.com/open-telemetry/opentelemetry-js/pull/4633) - * feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600) ### :bug: (Bug Fix)