Skip to content

Commit

Permalink
chore(instrumentation-net): use strings exported for attributes (#2194)
Browse files Browse the repository at this point in the history
Refs: #2025
Signed-off-by: maryliag <marylia.gutierrez@grafana.com>
  • Loading branch information
maryliag committed May 14, 2024
1 parent 7795ff6 commit 416337e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 47 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

19 changes: 11 additions & 8 deletions plugins/node/opentelemetry-instrumentation-net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ registerInstrumentations({
});
```

### Attributes added to `connect` spans
## Semantic Conventions

* `net.transport`: `IP.TCP`, `pipe` or `Unix`
* `net.peer.name`: host name or the IPC file path
This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)

For TCP:
Attributes added to `connect` spans:

* `net.peer.ip`
* `net.peer.port`
* `net.host.ip`
* `net.host.port`
| Attribute | Short Description |
|---------------------------|--------------------------------------------------------------------------|
| `net.transport` | `IP.TCP`, `pipe` or `Unix` |
| `net.peer.name` | Host name or the IPC file path |
| `net.peer.ip` (for TCP) | Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6) |
| `net.peer.port` (for TCP) | Remote port number |
| `net.host.ip` (for TCP) | Like net.peer.ip but for the host IP. Useful in case of a multi-IP host |
| `net.host.port` (for TCP) | Like net.peer.port but for the host port |

## Useful links

Expand Down
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 416337e

Please sign in to comment.