Skip to content

Commit

Permalink
refactor(instr-grpc): move to use SEMATTRS (#4633)
Browse files Browse the repository at this point in the history
* refactor(instr-grpc): move to use SEMATTRS

* Update experimental/CHANGELOG.md

* Update experimental/CHANGELOG.md

---------

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
pahiz and pichlermarc authored Apr 16, 2024
1 parent 7f82b80 commit 0a750eb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 48 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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'])
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -81,10 +81,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
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();
});
Expand All @@ -104,7 +101,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
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();
});
Expand Down Expand Up @@ -134,18 +131,15 @@ function clientStreamAndUnaryHandler<RequestType, ResponseType>(
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,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
Expand All @@ -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
);
};
Expand Down

0 comments on commit 0a750eb

Please sign in to comment.