Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-grpc): set net.peer.name and net.peer.port on client spans #3430

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ 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,6 +309,18 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
});
// set net.peer.* 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,
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,6 +322,18 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
});
// set net.peer.* from target (e.g., "dns:otel-productcatalogservice:8080") as a hint to APMs
const parsedUri = URI_REGEX.exec(this.getChannel().getTarget());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specification defines that peer.service:

SHOULD be equal to the actual service.name resource attribute of the remote service if any.

Channel#getTarget returns the address that this client is connected to, rather than the desired service.name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha - I agree! I was looking at values in flight from some service which happen to set it to host:port. I'll adjust. Thank you for the reference.

Copy link
Contributor Author

@ty-elastic ty-elastic Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lemme see how other auto-instrumentation frameworks set this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@legendecas - ok, I've change this to set net.peer.name and net.peer.port per OTEL RPC conventions.

Per this, we always set net.peer.name regardless of whether is a DNS name or IP: "Sometimes host name is only available to instrumentation as a string which may contain DNS name or IP address. net.peer.name SHOULD be set to the available known hostname (e.g., "127.0.0.1" if connecting to an URL https://127.0.0.1.com/foo)."

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ 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 "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,12 @@ 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 +61,21 @@ 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