Skip to content

Commit 3f8b5cd

Browse files
authored
chore(instrumentation-net): update semconv usage to ATTR_ exports (#3056)
Refs: #2377
1 parent 6419094 commit 3f8b5cd

File tree

6 files changed

+150
-47
lines changed

6 files changed

+150
-47
lines changed

packages/instrumentation-net/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
"typescript": "5.0.4"
6161
},
6262
"dependencies": {
63-
"@opentelemetry/instrumentation": "^0.205.0",
64-
"@opentelemetry/semantic-conventions": "^1.27.0"
63+
"@opentelemetry/instrumentation": "^0.205.0"
6564
},
6665
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-net#readme"
6766
}

packages/instrumentation-net/src/instrumentation.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import {
2323
safeExecuteInTheMiddle,
2424
} from '@opentelemetry/instrumentation';
2525
import {
26-
SEMATTRS_NET_HOST_IP,
27-
SEMATTRS_NET_HOST_PORT,
28-
SEMATTRS_NET_PEER_IP,
29-
SEMATTRS_NET_PEER_NAME,
30-
SEMATTRS_NET_PEER_PORT,
31-
SEMATTRS_NET_TRANSPORT,
32-
NETTRANSPORTVALUES_IP_TCP,
33-
} from '@opentelemetry/semantic-conventions';
26+
ATTR_NET_HOST_IP,
27+
ATTR_NET_HOST_PORT,
28+
ATTR_NET_PEER_IP,
29+
ATTR_NET_PEER_NAME,
30+
ATTR_NET_PEER_PORT,
31+
ATTR_NET_TRANSPORT,
32+
NET_TRANSPORT_VALUE_IP_TCP,
33+
} from './semconv';
3434
import { TLSAttributes } from './types';
3535
import { NormalizedOptions, SocketEvent } from './internal-types';
3636
import { getNormalizedArgs, IPC_TRANSPORT } from './utils';
@@ -190,8 +190,8 @@ export class NetInstrumentation extends InstrumentationBase {
190190
private _startIpcSpan(options: NormalizedOptions, socket: Socket) {
191191
const span = this.tracer.startSpan('ipc.connect', {
192192
attributes: {
193-
[SEMATTRS_NET_TRANSPORT]: IPC_TRANSPORT,
194-
[SEMATTRS_NET_PEER_NAME]: options.path,
193+
[ATTR_NET_TRANSPORT]: IPC_TRANSPORT,
194+
[ATTR_NET_PEER_NAME]: options.path,
195195
},
196196
});
197197

@@ -203,9 +203,9 @@ export class NetInstrumentation extends InstrumentationBase {
203203
private _startTcpSpan(options: NormalizedOptions, socket: Socket) {
204204
const span = this.tracer.startSpan('tcp.connect', {
205205
attributes: {
206-
[SEMATTRS_NET_TRANSPORT]: NETTRANSPORTVALUES_IP_TCP,
207-
[SEMATTRS_NET_PEER_NAME]: options.host,
208-
[SEMATTRS_NET_PEER_PORT]: options.port,
206+
[ATTR_NET_TRANSPORT]: NET_TRANSPORT_VALUE_IP_TCP,
207+
[ATTR_NET_PEER_NAME]: options.host,
208+
[ATTR_NET_PEER_PORT]: options.port,
209209
},
210210
});
211211

@@ -246,9 +246,9 @@ function registerListeners(
246246

247247
const setHostAttributes = () => {
248248
span.setAttributes({
249-
[SEMATTRS_NET_PEER_IP]: socket.remoteAddress,
250-
[SEMATTRS_NET_HOST_IP]: socket.localAddress,
251-
[SEMATTRS_NET_HOST_PORT]: socket.localPort,
249+
[ATTR_NET_PEER_IP]: socket.remoteAddress,
250+
[ATTR_NET_HOST_IP]: socket.localAddress,
251+
[ATTR_NET_HOST_PORT]: socket.localPort,
252252
});
253253
};
254254

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* This file contains a copy of unstable semantic convention definitions
19+
* used by this package.
20+
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
21+
*/
22+
23+
/**
24+
* Deprecated, use `network.local.address`.
25+
*
26+
* @example "192.168.0.1"
27+
*
28+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
29+
*
30+
* @deprecated Replaced by `network.local.address`.
31+
*/
32+
export const ATTR_NET_HOST_IP = 'net.host.ip' as const;
33+
34+
/**
35+
* Deprecated, use `server.port`.
36+
*
37+
* @example 8080
38+
*
39+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
40+
*
41+
* @deprecated Replaced by `server.port`.
42+
*/
43+
export const ATTR_NET_HOST_PORT = 'net.host.port' as const;
44+
45+
/**
46+
* Deprecated, use `network.peer.address`.
47+
*
48+
* @example "127.0.0.1"
49+
*
50+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
51+
*
52+
* @deprecated Replaced by `network.peer.address`.
53+
*/
54+
export const ATTR_NET_PEER_IP = 'net.peer.ip' as const;
55+
56+
/**
57+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
58+
*
59+
* @example example.com
60+
*
61+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
62+
*
63+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
64+
*/
65+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
66+
67+
/**
68+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
69+
*
70+
* @example 8080
71+
*
72+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
73+
*
74+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
75+
*/
76+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;
77+
78+
/**
79+
* Deprecated, use `network.transport`.
80+
*
81+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
82+
*
83+
* @deprecated Replaced by `network.transport`.
84+
*/
85+
export const ATTR_NET_TRANSPORT = 'net.transport' as const;
86+
87+
/**
88+
* Enum value "ip_tcp" for attribute {@link ATTR_NET_TRANSPORT}.
89+
*
90+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
91+
*/
92+
export const NET_TRANSPORT_VALUE_IP_TCP = 'ip_tcp' as const;
93+
94+
/**
95+
* Enum value "pipe" for attribute {@link ATTR_NET_TRANSPORT}.
96+
*
97+
* Named or anonymous pipe.
98+
*
99+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
100+
*/
101+
export const NET_TRANSPORT_VALUE_PIPE = 'pipe' as const;

packages/instrumentation-net/src/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
*/
1616

1717
import { NormalizedOptions } from './internal-types';
18-
import {
19-
NETTRANSPORTVALUES_PIPE,
20-
NETTRANSPORTVALUES_UNIX,
21-
} from '@opentelemetry/semantic-conventions';
18+
import { NET_TRANSPORT_VALUE_PIPE } from './semconv';
2219
import { platform } from 'os';
2320

21+
// Currently the `IPC_TRANSPORT` values are for 'net.transport'. In semconv
22+
// v1.21.0 a breaking change (https://github.com/open-telemetry/opentelemetry-specification/pull/3426)
23+
// replaced 'net.transport' with 'network.transport'. The deprecated
24+
// 'net.transport' *removed* the 'unix' value (not sure if intentional). As a
25+
// result, the JS `@opentelemetry/semantic-conventions` package does not export
26+
// a `NET_TRANSPORT_VALUE_UNIX`.
27+
//
28+
// (TODO: update instrumentation-net (per PR-3426) to use 'network.transport',
29+
// then the `NETWORK_TRANSPORT_VALUE_UNIX` constant can be used.)
2430
export const IPC_TRANSPORT =
25-
platform() === 'win32' ? NETTRANSPORTVALUES_PIPE : NETTRANSPORTVALUES_UNIX;
31+
platform() === 'win32' ? NET_TRANSPORT_VALUE_PIPE : 'unix';
2632

2733
function getHost(args: unknown[]) {
2834
return typeof args[1] === 'string' ? args[1] : 'localhost';

packages/instrumentation-net/test/connect.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
InMemorySpanExporter,
2020
SimpleSpanProcessor,
2121
} from '@opentelemetry/sdk-trace-base';
22-
import { SEMATTRS_NET_TRANSPORT } from '@opentelemetry/semantic-conventions';
22+
import { ATTR_NET_TRANSPORT } from '../src/semconv';
2323
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2424
import * as net from 'net';
2525
import * as assert from 'assert';
@@ -186,10 +186,7 @@ describe('NetInstrumentation', () => {
186186
const assertSpan = () => {
187187
try {
188188
const span = getSpan();
189-
assert.strictEqual(
190-
span.attributes[SEMATTRS_NET_TRANSPORT],
191-
undefined
192-
);
189+
assert.strictEqual(span.attributes[ATTR_NET_TRANSPORT], undefined);
193190
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
194191
done();
195192
} catch (e) {

packages/instrumentation-net/test/utils.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import { SpanKind } from '@opentelemetry/api';
1818
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
1919
import {
20-
NETTRANSPORTVALUES_IP_TCP,
21-
SEMATTRS_NET_HOST_IP,
22-
SEMATTRS_NET_HOST_PORT,
23-
SEMATTRS_NET_PEER_NAME,
24-
SEMATTRS_NET_PEER_PORT,
25-
SEMATTRS_NET_TRANSPORT,
26-
} from '@opentelemetry/semantic-conventions';
20+
ATTR_NET_HOST_IP,
21+
ATTR_NET_HOST_PORT,
22+
ATTR_NET_PEER_NAME,
23+
ATTR_NET_PEER_PORT,
24+
ATTR_NET_TRANSPORT,
25+
NET_TRANSPORT_VALUE_IP_TCP,
26+
} from '../src/semconv';
2727
import * as assert from 'assert';
2828
import * as path from 'path';
2929
import * as os from 'os';
@@ -41,17 +41,17 @@ export const IPC_PATH =
4141

4242
export function assertTcpSpan(span: ReadableSpan, socket: Socket) {
4343
assertSpanKind(span);
44-
assertAttrib(span, SEMATTRS_NET_TRANSPORT, NETTRANSPORTVALUES_IP_TCP);
45-
assertAttrib(span, SEMATTRS_NET_PEER_NAME, HOST);
46-
assertAttrib(span, SEMATTRS_NET_PEER_PORT, PORT);
47-
assertAttrib(span, SEMATTRS_NET_HOST_IP, socket.localAddress);
48-
assertAttrib(span, SEMATTRS_NET_HOST_PORT, socket.localPort);
44+
assertAttrib(span, ATTR_NET_TRANSPORT, NET_TRANSPORT_VALUE_IP_TCP);
45+
assertAttrib(span, ATTR_NET_PEER_NAME, HOST);
46+
assertAttrib(span, ATTR_NET_PEER_PORT, PORT);
47+
assertAttrib(span, ATTR_NET_HOST_IP, socket.localAddress);
48+
assertAttrib(span, ATTR_NET_HOST_PORT, socket.localPort);
4949
}
5050

5151
export function assertIpcSpan(span: ReadableSpan) {
5252
assertSpanKind(span);
53-
assertAttrib(span, SEMATTRS_NET_TRANSPORT, IPC_TRANSPORT);
54-
assertAttrib(span, SEMATTRS_NET_PEER_NAME, IPC_PATH);
53+
assertAttrib(span, ATTR_NET_TRANSPORT, IPC_TRANSPORT);
54+
assertAttrib(span, ATTR_NET_PEER_NAME, IPC_PATH);
5555
}
5656

5757
export function assertTLSSpan(
@@ -60,13 +60,13 @@ export function assertTLSSpan(
6060
) {
6161
assertParentChild(tlsSpan, netSpan);
6262
assertSpanKind(netSpan);
63-
assertAttrib(netSpan, SEMATTRS_NET_TRANSPORT, NETTRANSPORTVALUES_IP_TCP);
64-
assertAttrib(netSpan, SEMATTRS_NET_PEER_NAME, HOST);
65-
assertAttrib(netSpan, SEMATTRS_NET_PEER_PORT, PORT);
63+
assertAttrib(netSpan, ATTR_NET_TRANSPORT, NET_TRANSPORT_VALUE_IP_TCP);
64+
assertAttrib(netSpan, ATTR_NET_PEER_NAME, HOST);
65+
assertAttrib(netSpan, ATTR_NET_PEER_PORT, PORT);
6666
// Node.JS 10 sets socket.localAddress & socket.localPort to "undefined" when a connection is
6767
// ended, so one of the tests fails, so we skip them for TLS
68-
// assertAttrib(span, SEMATTRS_NET_HOST_IP, socket.localAddress);
69-
//assertAttrib(netSpan, SEMATTRS_NET_HOST_PORT, socket.localPort);
68+
// assertAttrib(span, ATTR_NET_HOST_IP, socket.localAddress);
69+
//assertAttrib(netSpan, ATTR_NET_HOST_PORT, socket.localPort);
7070

7171
assertAttrib(tlsSpan, TLSAttributes.PROTOCOL, 'TLSv1.2');
7272
assertAttrib(tlsSpan, TLSAttributes.AUTHORIZED, 'true');

0 commit comments

Comments
 (0)