forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation-tls): wrap tls.connect API (open-telemetry#447)
- Loading branch information
Showing
6 changed files
with
491 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
plugins/node/opentelemetry-instrumentation-net/test/tls.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { SpanStatusCode } from '@opentelemetry/api'; | ||
import { | ||
InMemorySpanExporter, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/tracing'; | ||
import { NodeTracerProvider } from '@opentelemetry/node'; | ||
import * as assert from 'assert'; | ||
import * as tls from 'tls'; | ||
import { NetInstrumentation } from '../src/net'; | ||
import { SocketEvent } from '../src/types'; | ||
import { | ||
assertTLSSpan, | ||
HOST, | ||
TLS_SERVER_CERT, | ||
TLS_SERVER_KEY, | ||
PORT, | ||
} from './utils'; | ||
|
||
const memoryExporter = new InMemorySpanExporter(); | ||
const provider = new NodeTracerProvider(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); | ||
|
||
function getTLSSpans() { | ||
const spans = memoryExporter.getFinishedSpans(); | ||
assert.strictEqual(spans.length, 2); | ||
const [netSpan, tlsSpan] = spans; | ||
return { | ||
netSpan, | ||
tlsSpan, | ||
}; | ||
} | ||
|
||
describe('NetInstrumentation', () => { | ||
let instrumentation: NetInstrumentation; | ||
let tlsServer: tls.Server; | ||
let tlsSocket: tls.TLSSocket; | ||
|
||
before(() => { | ||
instrumentation = new NetInstrumentation(); | ||
instrumentation.setTracerProvider(provider); | ||
require('net'); | ||
}); | ||
|
||
before(done => { | ||
tlsServer = tls.createServer({ | ||
cert: TLS_SERVER_CERT, | ||
key: TLS_SERVER_KEY, | ||
// Make sure tests run on nodejs v8 and v10 the same as on v12+ | ||
maxVersion: 'TLSv1.2', | ||
}); | ||
tlsServer.listen(PORT, done); | ||
}); | ||
|
||
afterEach(() => { | ||
memoryExporter.reset(); | ||
tlsSocket.destroy(); | ||
}); | ||
|
||
after(() => { | ||
instrumentation.disable(); | ||
tlsServer.close(); | ||
}); | ||
|
||
describe('successful tls.connect produces a span', () => { | ||
it('should produce a span with "onSecure" callback', done => { | ||
tlsSocket = tls.connect( | ||
PORT, | ||
HOST, | ||
{ | ||
ca: [TLS_SERVER_CERT], | ||
checkServerIdentity: () => { | ||
return undefined; | ||
}, | ||
}, | ||
() => { | ||
assertTLSSpan(getTLSSpans(), tlsSocket); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it('should produce a span without "onSecure" callback', done => { | ||
tlsSocket = tls.connect(PORT, HOST, { | ||
ca: [TLS_SERVER_CERT], | ||
checkServerIdentity: () => { | ||
return undefined; | ||
}, | ||
}); | ||
tlsServer.once('secureConnection', c => { | ||
c.end(); | ||
}); | ||
tlsSocket.once('end', () => { | ||
assertTLSSpan(getTLSSpans(), tlsSocket); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should produce an error span when certificate is not trusted', done => { | ||
tlsSocket = tls.connect( | ||
PORT, | ||
HOST, | ||
{ | ||
ca: [], | ||
checkServerIdentity: () => { | ||
return undefined; | ||
}, | ||
}, | ||
() => { | ||
assertTLSSpan(getTLSSpans(), tlsSocket); | ||
done(); | ||
} | ||
); | ||
tlsSocket.on('error', error => { | ||
const { tlsSpan } = getTLSSpans(); | ||
assert.strictEqual(tlsSpan.status.message, 'self signed certificate'); | ||
assert.strictEqual(tlsSpan.status.code, SpanStatusCode.ERROR); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('cleanup', () => { | ||
function assertNoDanglingListeners(tlsSocket: tls.TLSSocket) { | ||
const events = new Set(tlsSocket.eventNames()); | ||
|
||
for (const event of [ | ||
SocketEvent.CONNECT, | ||
SocketEvent.SECURE_CONNECT, | ||
SocketEvent.ERROR, | ||
]) { | ||
assert.equal(events.has(event), false); | ||
} | ||
assert.strictEqual(tlsSocket.listenerCount(SocketEvent.CLOSE), 1); | ||
} | ||
|
||
it('should clean up listeners for tls.connect', done => { | ||
tlsSocket = tls.connect( | ||
PORT, | ||
HOST, | ||
{ | ||
ca: [TLS_SERVER_CERT], | ||
checkServerIdentity: () => { | ||
return undefined; | ||
}, | ||
}, | ||
() => { | ||
tlsSocket.destroy(); | ||
tlsSocket.once(SocketEvent.CLOSE, () => { | ||
assertNoDanglingListeners(tlsSocket); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.