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

chore(instrumentation-net): use strings exported for attributes #2194

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.51.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.23.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
"@opentelemetry/semantic-conventions": "^1.23.0"
"@opentelemetry/semantic-conventions": "^1.22.0"

Thought perhaps I'm wrong and 1.23 is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment as the other PR, I decided to get the latest version, since it also works

},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-net#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import {
SemanticAttributes,
NetTransportValues,
SEMATTRS_NET_HOST_IP,
SEMATTRS_NET_HOST_PORT,
SEMATTRS_NET_PEER_IP,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_NET_TRANSPORT,
NETTRANSPORTVALUES_IP_TCP,
} from '@opentelemetry/semantic-conventions';
import { TLSAttributes } from './types';
import { NormalizedOptions, SocketEvent } from './internal-types';
Expand Down Expand Up @@ -184,8 +189,8 @@ export class NetInstrumentation extends InstrumentationBase {
private _startIpcSpan(options: NormalizedOptions, socket: Socket) {
const span = this.tracer.startSpan('ipc.connect', {
attributes: {
[SemanticAttributes.NET_TRANSPORT]: IPC_TRANSPORT,
[SemanticAttributes.NET_PEER_NAME]: options.path,
[SEMATTRS_NET_TRANSPORT]: IPC_TRANSPORT,
[SEMATTRS_NET_PEER_NAME]: options.path,
},
});

Expand All @@ -197,9 +202,9 @@ export class NetInstrumentation extends InstrumentationBase {
private _startTcpSpan(options: NormalizedOptions, socket: Socket) {
const span = this.tracer.startSpan('tcp.connect', {
attributes: {
[SemanticAttributes.NET_TRANSPORT]: NetTransportValues.IP_TCP,
[SemanticAttributes.NET_PEER_NAME]: options.host,
[SemanticAttributes.NET_PEER_PORT]: options.port,
[SEMATTRS_NET_TRANSPORT]: NETTRANSPORTVALUES_IP_TCP,
[SEMATTRS_NET_PEER_NAME]: options.host,
[SEMATTRS_NET_PEER_PORT]: options.port,
},
});

Expand Down Expand Up @@ -240,9 +245,9 @@ function registerListeners(

const setHostAttributes = () => {
span.setAttributes({
[SemanticAttributes.NET_PEER_IP]: socket.remoteAddress,
[SemanticAttributes.NET_HOST_IP]: socket.localAddress,
[SemanticAttributes.NET_HOST_PORT]: socket.localPort,
[SEMATTRS_NET_PEER_IP]: socket.remoteAddress,
[SEMATTRS_NET_HOST_IP]: socket.localAddress,
[SEMATTRS_NET_HOST_PORT]: socket.localPort,
});
};

Expand Down
7 changes: 5 additions & 2 deletions plugins/node/opentelemetry-instrumentation-net/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/

import { NormalizedOptions } from './internal-types';
import { NetTransportValues } from '@opentelemetry/semantic-conventions';
import {
NETTRANSPORTVALUES_PIPE,
NETTRANSPORTVALUES_UNIX,
} from '@opentelemetry/semantic-conventions';
import { platform } from 'os';

export const IPC_TRANSPORT =
platform() === 'win32' ? NetTransportValues.PIPE : NetTransportValues.UNIX;
platform() === 'win32' ? NETTRANSPORTVALUES_PIPE : NETTRANSPORTVALUES_UNIX;

function getHost(args: unknown[]) {
return typeof args[1] === 'string' ? args[1] : 'localhost';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_NET_TRANSPORT } from '@opentelemetry/semantic-conventions';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import * as net from 'net';
import * as assert from 'assert';
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('NetInstrumentation', () => {
try {
const span = getSpan();
assert.strictEqual(
span.attributes[SemanticAttributes.NET_TRANSPORT],
span.attributes[SEMATTRS_NET_TRANSPORT],
undefined
);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
Expand Down
40 changes: 18 additions & 22 deletions plugins/node/opentelemetry-instrumentation-net/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import { SpanKind } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import {
NetTransportValues,
SemanticAttributes,
NETTRANSPORTVALUES_IP_TCP,
SEMATTRS_NET_HOST_IP,
SEMATTRS_NET_HOST_PORT,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_NET_TRANSPORT,
} from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import * as path from 'path';
Expand All @@ -37,21 +41,17 @@ export const IPC_PATH =

export function assertTcpSpan(span: ReadableSpan, socket: Socket) {
assertSpanKind(span);
assertAttrib(
span,
SemanticAttributes.NET_TRANSPORT,
NetTransportValues.IP_TCP
);
assertAttrib(span, SemanticAttributes.NET_PEER_NAME, HOST);
assertAttrib(span, SemanticAttributes.NET_PEER_PORT, PORT);
assertAttrib(span, SemanticAttributes.NET_HOST_IP, socket.localAddress);
assertAttrib(span, SemanticAttributes.NET_HOST_PORT, socket.localPort);
assertAttrib(span, SEMATTRS_NET_TRANSPORT, NETTRANSPORTVALUES_IP_TCP);
assertAttrib(span, SEMATTRS_NET_PEER_NAME, HOST);
assertAttrib(span, SEMATTRS_NET_PEER_PORT, PORT);
assertAttrib(span, SEMATTRS_NET_HOST_IP, socket.localAddress);
assertAttrib(span, SEMATTRS_NET_HOST_PORT, socket.localPort);
}

export function assertIpcSpan(span: ReadableSpan) {
assertSpanKind(span);
assertAttrib(span, SemanticAttributes.NET_TRANSPORT, IPC_TRANSPORT);
assertAttrib(span, SemanticAttributes.NET_PEER_NAME, IPC_PATH);
assertAttrib(span, SEMATTRS_NET_TRANSPORT, IPC_TRANSPORT);
assertAttrib(span, SEMATTRS_NET_PEER_NAME, IPC_PATH);
}

export function assertTLSSpan(
Expand All @@ -60,17 +60,13 @@ export function assertTLSSpan(
) {
assertParentChild(tlsSpan, netSpan);
assertSpanKind(netSpan);
assertAttrib(
netSpan,
SemanticAttributes.NET_TRANSPORT,
NetTransportValues.IP_TCP
);
assertAttrib(netSpan, SemanticAttributes.NET_PEER_NAME, HOST);
assertAttrib(netSpan, SemanticAttributes.NET_PEER_PORT, PORT);
assertAttrib(netSpan, SEMATTRS_NET_TRANSPORT, NETTRANSPORTVALUES_IP_TCP);
assertAttrib(netSpan, SEMATTRS_NET_PEER_NAME, HOST);
assertAttrib(netSpan, SEMATTRS_NET_PEER_PORT, PORT);
// Node.JS 10 sets socket.localAddress & socket.localPort to "undefined" when a connection is
// ended, so one of the tests fails, so we skip them for TLS
// assertAttrib(span, SemanticAttributes.NET_HOST_IP, socket.localAddress);
//assertAttrib(netSpan, SemanticAttributes.NET_HOST_PORT, socket.localPort);
// assertAttrib(span, SEMATTRS_NET_HOST_IP, socket.localAddress);
//assertAttrib(netSpan, SEMATTRS_NET_HOST_PORT, socket.localPort);

assertAttrib(tlsSpan, TLSAttributes.PROTOCOL, 'TLSv1.2');
assertAttrib(tlsSpan, TLSAttributes.AUTHORIZED, 'true');
Expand Down
Loading