Skip to content

Commit

Permalink
feat(instrumentation-grpc): set peer.service on client spans
Browse files Browse the repository at this point in the history
  • Loading branch information
ty-elastic committed Nov 21, 2022
1 parent e315f53 commit 4d65da9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* feat(instrumentation-grpc): set peer.service on client spans [#3430](https://github.com/open-telemetry/opentelemetry-js/pull/3430)

### :bug: (Bug Fix)

* fix(sdk-metrics): use default Resource to comply with semantic conventions [#3411](https://github.com/open-telemetry/opentelemetry-js/pull/3411) @pichlermarc
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 } from '../utils';
import { _extractMethodAndService, URI_REGEX } from '../utils';
import { AttributeValues } from '../enums/AttributeValues';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

Expand Down Expand Up @@ -298,6 +298,11 @@ 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.PEER_SERVICE, parsedUri.groups['addr']);
}
return context.with(trace.setSpan(context.active(), span), () =>
makeGrpcClientRemoteCall(original, args, metadata, this)(span)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
serverStreamAndBidiHandler,
} from './serverUtils';
import { makeGrpcClientRemoteCall, getMetadata } from './clientUtils';
import { _extractMethodAndService, _methodIsIgnored } from '../utils';
import { _extractMethodAndService, _methodIsIgnored, URI_REGEX } from '../utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {AttributeValues} from '../enums/AttributeValues';

Expand Down Expand Up @@ -306,6 +306,11 @@ 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.PEER_SERVICE, parsedUri.groups['addr']);
}
return context.with(trace.setSpan(context.active(), span), () =>
makeGrpcClientRemoteCall(
grpcClient,
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+.-]+):(?:\/\/)?)?(?<addr>[A-Za-z0-9+.-]+:[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 @@ -481,6 +481,7 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: grpc.status.OK,
peerService: 'localhost:' + grpcPort,
};

assert.strictEqual(spans.length, 2);
Expand Down Expand Up @@ -530,6 +531,7 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: grpc.status.OK,
peerService: 'localhost:' + grpcPort,
};
assertSpan(
moduleName,
Expand Down Expand Up @@ -590,6 +592,7 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: errorCode,
peerService: 'localhost:' + grpcPort,
};
const serverRoot = spans[0];
const clientRoot = spans[1];
Expand Down Expand Up @@ -629,6 +632,7 @@ export const runTests = (
const validations = {
name: `grpc.pkg_test.GrpcTester/${method.methodName}`,
status: errorCode,
peerService: 'localhost:' + 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 @@ -23,6 +23,7 @@ import {
hrTimeToMilliseconds,
hrTimeToMicroseconds,
} from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

export const grpcStatusCodeToOpenTelemetryStatusCode = (
status: grpc.status | grpcJs.status
Expand All @@ -37,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; peerService?: string }
) => {
assert.strictEqual(span.spanContext().traceId.length, 32);
assert.strictEqual(span.spanContext().spanId.length, 16);
Expand All @@ -55,6 +56,10 @@ export const assertSpan = (
assert.ok(span.spanContext());
}

if (span.kind === SpanKind.CLIENT && validations.peerService !== undefined) {
assert.strictEqual(span.attributes[SemanticAttributes.PEER_SERVICE], validations.peerService);
}

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

0 comments on commit 4d65da9

Please sign in to comment.