diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index c94f75f1ad..694ecfabfe 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -15,6 +15,7 @@ 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) * feat(sdk-logs): print message when attributes are dropped due to attribute count limit [#4614](https://github.com/open-telemetry/opentelemetry-js/pull/4614) @HyunnoH 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 ); };