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 20, 2022
1 parent e315f53 commit 2e48472
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
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 @@ -37,7 +37,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 +55,10 @@ export const assertSpan = (
assert.ok(span.spanContext());
}

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

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

0 comments on commit 2e48472

Please sign in to comment.