Skip to content

Commit

Permalink
Merge branch 'main' into ts4
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored May 8, 2021
2 parents 7fabb5c + 2cbf8e7 commit 3294280
Show file tree
Hide file tree
Showing 31 changed files with 526 additions and 66 deletions.
51 changes: 51 additions & 0 deletions examples/network/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { NetInstrumentation } = require('@opentelemetry/instrumentation-net');
const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');

const provider = new NodeTracerProvider();

provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({
serviceName: 'http-client',
})));

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

provider.register();

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL);

registerInstrumentations({
instrumentations: [
new NetInstrumentation(),
new HttpInstrumentation(),
new DnsInstrumentation({
// Avoid dns lookup loop with http zipkin calls
ignoreHostnames: ['localhost'],
}),
],
tracerProvider: provider,
});

require('net');
require('dns');
const https = require('https');
const http = require('http');

http.get('http://opentelemetry.io/', () => {}).on('error', (e) => {
console.error(e);
});

https.get('https://opentelemetry.io/', () => {}).on('error', (e) => {
console.error(e);
});

https.get('https://opentelemetry.io/', { ca: [] }, () => {}).on('error', (e) => {
console.error(e);
});
44 changes: 44 additions & 0 deletions examples/network/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "tls-example",
"private": true,
"version": "0.15.0",
"description": "Example of NET & TLS integration with OpenTelemetry",
"main": "index.js",
"scripts": {
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js",
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.js"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js-contrib.git"
},
"keywords": [
"opentelemetry",
"net",
"tls",
"tracing"
],
"engines": {
"node": ">=8.5.0"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues"
},
"dependencies": {
"@opentelemetry/api": "^0.18.1",
"@opentelemetry/exporter-jaeger": "^0.18.2",
"@opentelemetry/exporter-zipkin": "^0.18.2",
"@opentelemetry/instrumentation": "^0.18.2",
"@opentelemetry/instrumentation-net": "file:../../plugins/node/opentelemetry-instrumentation-net",
"@opentelemetry/instrumentation-http": "^0.19.0",
"@opentelemetry/instrumentation-dns": "^0.15.0",
"@opentelemetry/node": "^0.18.2",
"@opentelemetry/tracing": "^0.18.2"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme",
"devDependencies": {
"cross-env": "^6.0.3"
}
}
1 change: 1 addition & 0 deletions examples/restify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@opentelemetry/exporter-zipkin": "^0.19.0",
"@opentelemetry/instrumentation": "^0.19.0",
"@opentelemetry/instrumentation-http": "^0.19.0",
"@opentelemetry/instrumentation-restify": "^0.16.0",
"@opentelemetry/node": "^0.19.0",
"@opentelemetry/tracing": "^0.19.0",
"restify": "^4.3.4"
Expand Down
2 changes: 0 additions & 2 deletions metapackages/auto-instrumentations-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"rimraf": "3.0.2",
"sinon": "10.0.0",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"devDependencies": {
"@types/node": "14.14.43",
"gts": "3.1.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
"rimraf": "3.0.2",
"sinon": "9.2.4",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/node/opentelemetry-instrumentation-dns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
"rimraf": "3.0.2",
"sinon": "10.0.0",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For automatic instrumentation see the

## Installation

This instrumentation relies on HTTP calls to also be instrumented. Make sure you install and enable both.
This instrumentation relies on HTTP calls to also be instrumented. Make sure you install and enable both, otherwise you will not see any spans being exported from the instrumentation.

```bash
npm install --save @opentelemetry/instrumentation-http @opentelemetry/instrumentation-express
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/node/opentelemetry-instrumentation-hapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
"rimraf": "3.0.2",
"semver": "7.3.5",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/node/opentelemetry-instrumentation-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/node/opentelemetry-instrumentation-mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/node/opentelemetry-instrumentation-net/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
"rimraf": "3.0.2",
"sinon": "10.0.0",
"ts-mocha": "8.0.0",
"tslint-consistent-codestyle": "1.16.0",
"tslint-microsoft-contrib": "6.2.0",
"typescript": "4.2.4"
},
"dependencies": {
Expand Down
83 changes: 77 additions & 6 deletions plugins/node/opentelemetry-instrumentation-net/src/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
SemanticAttributes,
NetTransportValues,
} from '@opentelemetry/semantic-conventions';
import { Net, NormalizedOptions, SocketEvent } from './types';
import { Net, NormalizedOptions, SocketEvent, TLSAttributes } from './types';
import { getNormalizedArgs, IPC_TRANSPORT } from './utils';
import { VERSION } from './version';
import { Socket } from 'net';
import { TLSSocket } from 'tls';

export class NetInstrumentation extends InstrumentationBase<Net> {
constructor(protected _config: InstrumentationConfig = {}) {
Expand Down Expand Up @@ -69,11 +70,10 @@ export class NetInstrumentation extends InstrumentationBase<Net> {
return function patchedConnect(this: Socket, ...args: unknown[]) {
const options = getNormalizedArgs(args);

const span = options
? options.path
? plugin._startIpcSpan(options, this)
: plugin._startTcpSpan(options, this)
: plugin._startGenericSpan(this);
const span =
this instanceof TLSSocket
? plugin._startTLSSpan(options, this)
: plugin._startSpan(options, this);

return safeExecuteInTheMiddle(
() => original.apply(this, args),
Expand All @@ -92,6 +92,77 @@ export class NetInstrumentation extends InstrumentationBase<Net> {
};
}

private _startSpan(
options: NormalizedOptions | undefined | null,
socket: Socket
) {
if (!options) {
return this._startGenericSpan(socket);
}
if (options.path) {
return this._startIpcSpan(options, socket);
}
return this._startTcpSpan(options, socket);
}

private _startTLSSpan(
options: NormalizedOptions | undefined | null,
socket: TLSSocket
) {
const tlsSpan = this.tracer.startSpan('tls.connect');

const netSpan = this._startSpan(options, socket);

const otelTlsSpanListener = () => {
const peerCertificate = socket.getPeerCertificate(true);
const cipher = socket.getCipher();
const protocol = socket.getProtocol();
const attributes = {
[TLSAttributes.PROTOCOL]: String(protocol),
[TLSAttributes.AUTHORIZED]: String(socket.authorized),
[TLSAttributes.CIPHER_NAME]: cipher.name,
[TLSAttributes.CIPHER_VERSION]: cipher.version,
[TLSAttributes.CERTIFICATE_FINGERPRINT]: peerCertificate.fingerprint,
[TLSAttributes.CERTIFICATE_SERIAL_NUMBER]: peerCertificate.serialNumber,
[TLSAttributes.CERTIFICATE_VALID_FROM]: peerCertificate.valid_from,
[TLSAttributes.CERTIFICATE_VALID_TO]: peerCertificate.valid_to,
[TLSAttributes.ALPN_PROTOCOL]: '',
};
if (socket.alpnProtocol) {
attributes[TLSAttributes.ALPN_PROTOCOL] = socket.alpnProtocol;
}

tlsSpan.setAttributes(attributes);
tlsSpan.end();
};

const otelTlsErrorListener = (e: Error) => {
tlsSpan.setStatus({
code: SpanStatusCode.ERROR,
message: e.message,
});
tlsSpan.end();
};

/* if we use once and tls.connect() uses a callback this is never executed */
socket.prependOnceListener(SocketEvent.SECURE_CONNECT, otelTlsSpanListener);
socket.once(SocketEvent.ERROR, otelTlsErrorListener);

const otelTlsRemoveListeners = () => {
socket.removeListener(SocketEvent.SECURE_CONNECT, otelTlsSpanListener);
socket.removeListener(SocketEvent.ERROR, otelTlsErrorListener);
for (const event of SOCKET_EVENTS) {
socket.removeListener(event, otelTlsRemoveListeners);
}
};

for (const event of [SocketEvent.CLOSE, SocketEvent.ERROR]) {
socket.once(event, otelTlsRemoveListeners);
}

return netSpan;
}

/* It might still be useful to pick up errors due to invalid connect arguments. */
private _startGenericSpan(socket: Socket) {
const span = this.tracer.startSpan('connect');
Expand Down
14 changes: 14 additions & 0 deletions plugins/node/opentelemetry-instrumentation-net/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ export enum SocketEvent {
CLOSE = 'close',
CONNECT = 'connect',
ERROR = 'error',
SECURE_CONNECT = 'secureConnect',
}

/* The following attributes are not offical, see open-telemetry/opentelemetry-specification#1652 */
export enum TLSAttributes {
PROTOCOL = 'tls.protocol',
AUTHORIZED = 'tls.authorized',
CIPHER_NAME = 'tls.cipher.name',
CIPHER_VERSION = 'tls.cipher.version',
CERTIFICATE_FINGERPRINT = 'tls.certificate.fingerprint',
CERTIFICATE_SERIAL_NUMBER = 'tls.certificate.serialNumber',
CERTIFICATE_VALID_FROM = 'tls.certificate.validFrom',
CERTIFICATE_VALID_TO = 'tls.certificate.validTo',
ALPN_PROTOCOL = 'tls.alpnProtocol',
}
Loading

0 comments on commit 3294280

Please sign in to comment.