Skip to content

Commit

Permalink
wip: add semconv stability opt-in logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDanielson committed Sep 19, 2024
1 parent 70062cb commit 5d949bc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ import type {
metadataCaptureType,
} from './internal-types';
import type { GrpcInstrumentationConfig } from './types';
import { SemconvStability } from './types';

import {
Attributes,
context,
propagation,
ROOT_CONTEXT,
Expand All @@ -51,6 +53,7 @@ import {
trace,
Span,
} from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import {
InstrumentationNodeModuleDefinition,
InstrumentationBase,
Expand Down Expand Up @@ -92,9 +95,21 @@ import { VERSION } from './version';
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 @@ -328,7 +343,11 @@ export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentation
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 @@ -441,7 +460,11 @@ export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentation
[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 @@ -493,15 +516,33 @@ export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentation
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

0 comments on commit 5d949bc

Please sign in to comment.