Skip to content

Commit

Permalink
chore(instrumentation-net): use strings exported for attributes
Browse files Browse the repository at this point in the history
Update strings exported for Resource Attributes

Signed-off-by: maryliag <marylia.gutierrez@grafana.com>
  • Loading branch information
maryliag committed May 10, 2024
1 parent c1c3650 commit bac2bd3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
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"
},
"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

0 comments on commit bac2bd3

Please sign in to comment.