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(instr-grpc): update semconv to 1.27 #5002

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
metadataCaptureType,
} from './internal-types';
import type { GrpcInstrumentationConfig } from './types';
import { SemconvStability } from './types';

import {
Attributes,
context,
propagation,
ROOT_CONTEXT,
Expand All @@ -51,11 +53,14 @@
trace,
Span,
} from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import {
InstrumentationNodeModuleDefinition,
InstrumentationBase,
} from '@opentelemetry/instrumentation';
import {
ATTR_CLIENT_ADDRESS,
ATTR_CLIENT_PORT,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_RPC_METHOD,
Expand Down Expand Up @@ -90,9 +95,21 @@
export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentationConfig> {
private _metadataCapture: metadataCaptureType;

private _semconvStability = SemconvStability.OLD;

constructor(config: GrpcInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-grpc', VERSION, config);
this._metadataCapture = this._createMetadataCapture();

for (const entry in getEnv().OTEL_SEMCONV_STABILITY_OPT_IN) {
if (entry.toLowerCase() === 'http/dup') {

Check warning on line 105 in experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts#L105

Added line #L105 was not covered by tests
// http/dup takes highest precedence. If it is found, there is no need to read the rest of the list
this._semconvStability = SemconvStability.DUPLICATE;
break;
} else if (entry.toLowerCase() === 'http') {
this._semconvStability = SemconvStability.STABLE;

Check warning on line 110 in experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts#L107-L110

Added lines #L107 - L110 were not covered by tests
}
}
}

init() {
Expand Down Expand Up @@ -326,7 +343,11 @@
service,
metadata
);
instrumentation.extractNetMetadata(this, span);
instrumentation.extractNetMetadata(
this,
span,
instrumentation._semconvStability
);

// Callback is only present when there is no responseStream
if (!hasResponseStream) {
Expand Down Expand Up @@ -435,11 +456,15 @@
const span = instrumentation.tracer
.startSpan(name, { kind: SpanKind.CLIENT })
.setAttributes({
[SEMATTRS_RPC_SYSTEM]: 'grpc',
[SEMATTRS_RPC_SYSTEM]: AttributeValues.RPC_SYSTEM,
[SEMATTRS_RPC_METHOD]: method,
[SEMATTRS_RPC_SERVICE]: service,
});
instrumentation.extractNetMetadata(this, span);
instrumentation.extractNetMetadata(
this,
span,
instrumentation._semconvStability
);

instrumentation._metadataCapture.client.captureRequestMetadata(
span,
Expand Down Expand Up @@ -480,7 +505,7 @@
const span = this.tracer
.startSpan(name, { kind: SpanKind.CLIENT })
.setAttributes({
[SEMATTRS_RPC_SYSTEM]: 'grpc',
[SEMATTRS_RPC_SYSTEM]: AttributeValues.RPC_SYSTEM,
[SEMATTRS_RPC_METHOD]: methodAttributeValue,
[SEMATTRS_RPC_SERVICE]: service,
});
Expand All @@ -491,15 +516,33 @@
return span;
}

private extractNetMetadata(client: grpcJs.Client, span: Span) {
private extractNetMetadata(
client: grpcJs.Client,
span: Span,
semconvStability: SemconvStability
) {
// 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(
SEMATTRS_NET_PEER_PORT,
parseInt(parsedUri.groups['port'])
);
const hostname = parsedUri?.groups?.name;
const port = parseInt(parsedUri?.groups?.port ?? '');
const oldAttributes: Attributes = {
[SEMATTRS_NET_PEER_NAME]: hostname,
[SEMATTRS_NET_PEER_PORT]: port,
};
const newAttributes: Attributes = {
[ATTR_CLIENT_ADDRESS]: hostname,
[ATTR_CLIENT_PORT]: port,
};
switch (semconvStability) {
case SemconvStability.STABLE:
span.setAttributes(newAttributes);
break;

Check warning on line 539 in experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts#L537-L539

Added lines #L537 - L539 were not covered by tests
case SemconvStability.OLD:
span.setAttributes(oldAttributes);
break;
case SemconvStability.DUPLICATE:
span.setAttributes({ ...oldAttributes, ...newAttributes });
break;

Check warning on line 545 in experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts#L543-L545

Added lines #L543 - L545 were not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ export interface GrpcInstrumentationConfig extends InstrumentationConfig {
};
};
}

/**
* Tracks whether this instrumentation emits old experimental,
* new stable, or both semantic conventions.
*
* Enum values chosen such that the enum may be used as a bitmask.
*/
export const enum SemconvStability {
/** Emit only stable semantic conventions */
STABLE = 0x1,
/** Emit only old semantic convetions */
OLD = 0x2,
/** Emit both stable and old semantic convetions */
DUPLICATE = 0x1 | 0x2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { assertPropagation, assertSpan } from './utils/assertionUtils';
import { promisify } from 'util';
import type { GrpcInstrumentation } from '../src';
import * as path from 'path';
import { SemconvStability } from '../src/types';

const PROTO_PATH = path.resolve(__dirname, './fixtures/grpc-test.proto');
const memoryExporter = new InMemorySpanExporter();
Expand Down Expand Up @@ -969,6 +970,7 @@ export const runTests = (
},
},
});
plugin['_semconvStability'] = SemconvStability.OLD;

plugin.setTracerProvider(provider);
plugin.enable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
} from '@opentelemetry/semantic-conventions';
import { AttributeValues } from '../src/enums/AttributeValues';

export type SpanAssertionFunction = (
exporter: InMemorySpanExporter,
Expand Down Expand Up @@ -61,8 +62,18 @@ function validateSpans(
);
assertPropagation(serverSpan, clientSpan);

assertSpan('grpc', serverSpan, SpanKind.SERVER, validations);
assertSpan('grpc', clientSpan, SpanKind.CLIENT, validations);
assertSpan(
AttributeValues.RPC_SYSTEM,
serverSpan,
SpanKind.SERVER,
validations
);
assertSpan(
AttributeValues.RPC_SYSTEM,
clientSpan,
SpanKind.CLIENT,
validations
);
assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_METHOD], rpcMethod);
assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_SERVICE], rpcService);
}
Expand Down
Loading