Skip to content

Commit

Permalink
feat(instrumentation-grpc): set net.peer.name,net.peer.port on client…
Browse files Browse the repository at this point in the history
… spans
  • Loading branch information
ty-elastic committed Dec 2, 2022
1 parent 369b07e commit fcaee74
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
### :rocket: (Enhancement)

* feat(api): add `getActiveBaggage` API [#3385](https://github.com/open-telemetry/opentelemetry-js/pull/3385)
* feat(instrumentation-grpc): set net.peer.name and net.peer.port on client spans [#3430](https://github.com/open-telemetry/opentelemetry-js/pull/3430)

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ import {
getMetadata,
} from './clientUtils';
import { EventEmitter } from 'events';
import { _extractMethodAndService, metadataCapture } from '../utils';
import {
_extractMethodAndService,
metadataCapture,
URI_REGEX
} from '../utils';
import { AttributeValues } from '../enums/AttributeValues';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

Expand Down Expand Up @@ -309,7 +313,13 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
});

// set peer.service from target (e.g., "dns:otel-productcatalogservice:8080") as a hint to APMs
const parsedUri = URI_REGEX.exec(this.getChannel().getTarget());
if (parsedUri != null && parsedUri.groups != null) {
span.setAttribute(SemanticAttributes.NET_PEER_NAME, parsedUri.groups['name']);
span.setAttribute(SemanticAttributes.NET_PEER_PORT, parseInt(parsedUri.groups['port']));
}

instrumentation._metadataCapture.client.captureRequestMetadata(
span,
metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
_extractMethodAndService,
_methodIsIgnored,
metadataCapture,
URI_REGEX
} from '../utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { AttributeValues } from '../enums/AttributeValues';
Expand Down Expand Up @@ -321,7 +322,13 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
});

// set peer.service from target (e.g., "dns:otel-productcatalogservice:8080") as a hint to APMs
const parsedUri = URI_REGEX.exec(this.getChannel().getTarget());
if (parsedUri != null && parsedUri.groups != null) {
span.setAttribute(SemanticAttributes.NET_PEER_NAME, parsedUri.groups['name']);
span.setAttribute(SemanticAttributes.NET_PEER_PORT, parseInt(parsedUri.groups['port']));
}

instrumentation._metadataCapture.client.captureRequestMetadata(
span,
metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import type * as grpcTypes from 'grpc';
import type * as grpcJsTypes from '@grpc/grpc-js';
import { IgnoreMatcher } from './types';

// e.g., "dns:otel-productcatalogservice:8080" or "127.0.0.1:8080"
export const URI_REGEX = /(?:([A-Za-z0-9+.-]+):(?:\/\/)?)?(?<name>[A-Za-z0-9+.-]+):(?<port>[0-9+.-]+)$/;

// Equivalent to lodash _.findIndex
export const findIndex: <T>(args: T[], fn: (arg: T) => boolean) => number = (
args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${methodName}`,
status: grpc.status.OK,
netPeerName: 'localhost',
netPeerPort: grpcPort
};

assertSpan(moduleName, serverSpan, SpanKind.SERVER, validations);
Expand Down Expand Up @@ -699,6 +701,8 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: errorCode,
netPeerName: 'localhost',
netPeerPort: grpcPort
};
const serverRoot = spans[0];
const clientRoot = spans[1];
Expand Down Expand Up @@ -738,6 +742,8 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: errorCode,
netPeerName: 'localhost',
netPeerPort: grpcPort
};
assertSpan(moduleName, serverSpan, SpanKind.SERVER, validations);
assertSpan(moduleName, clientSpan, SpanKind.CLIENT, validations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const assertSpan = (
component: string,
span: ReadableSpan,
kind: SpanKind,
validations: { name: string; status: grpc.status | grpcJs.status }
validations: { name: string; status: grpc.status | grpcJs.status; netPeerName?: string; netPeerPort?: number}
) => {
assert.strictEqual(span.spanContext().traceId.length, 32);
assert.strictEqual(span.spanContext().spanId.length, 16);
Expand All @@ -56,6 +56,11 @@ export const assertSpan = (
assert.ok(span.spanContext());
}

if (span.kind === SpanKind.CLIENT && validations.netPeerName !== undefined && validations.netPeerPort !== undefined) {
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_NAME], validations.netPeerName);
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_PORT], validations.netPeerPort);
}

// validations
assert.strictEqual(span.name, validations.name);
assert.strictEqual(
Expand Down

0 comments on commit fcaee74

Please sign in to comment.