Skip to content

Commit 8567061

Browse files
OlivierAlbertinimayurkale22
authored andcommitted
feat(plugin-https): patch https requests (open-telemetry#379)
* feat(plugin-https): patch https requests closes open-telemetry#375 add tests Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca> * docs(plugin-https): add jaeger image Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca> * fix: add mayurkale22 recommendations Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca> * fix: add markwolff recommendations Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca> * fix: file name utils * fix: add danielkhan and bg451 recommendations Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>
1 parent 32572c6 commit 8567061

32 files changed

+1797
-43
lines changed

examples/https/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Overview
2+
3+
OpenTelemetry HTTPS Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example), to give observability to distributed systems.
4+
5+
This is a simple example that demonstrates tracing HTTPS request from client to server. The example
6+
shows key aspects of tracing such as
7+
- Root Span (on Client)
8+
- Child Span (on Client)
9+
- Child Span from a Remote Parent (on Server)
10+
- SpanContext Propagation (from Client to Server)
11+
- Span Events
12+
- Span Attributes
13+
14+
## Installation
15+
16+
```sh
17+
$ # from this directory
18+
$ npm install
19+
```
20+
21+
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
22+
or
23+
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)
24+
25+
## Run the Application
26+
27+
### Zipkin
28+
29+
- Run the server
30+
31+
```sh
32+
$ # from this directory
33+
$ npm run zipkin:server
34+
```
35+
36+
- Run the client
37+
38+
```sh
39+
$ # from this directory
40+
$ npm run zipkin:client
41+
```
42+
43+
#### Zipkin UI
44+
`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
45+
Go to Zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6)
46+
47+
<p align="center"><img src="./images/zipkin-ui.png?raw=true"/></p>
48+
49+
### Jaeger
50+
51+
- Run the server
52+
53+
```sh
54+
$ # from this directory
55+
$ npm run jaeger:server
56+
```
57+
58+
- Run the client
59+
60+
```sh
61+
$ # from this directory
62+
$ npm run jaeger:client
63+
```
64+
#### Jaeger UI
65+
66+
`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
67+
Go to Jaeger with your browser [http://localhost:16686/trace/(your-trace-id)]() (e.g http://localhost:16686/trace/4815c3d576d930189725f1f1d1bdfcc6)
68+
69+
<p align="center"><img src="images/jaeger-ui.png?raw=true"/></p>
70+
71+
## Useful links
72+
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
73+
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node-sdk>
74+
75+
## LICENSE
76+
77+
Apache License 2.0

examples/https/client.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const opentelemetry = require('@opentelemetry/core');
4+
const config = require('./setup');
5+
/**
6+
* The trace instance needs to be initialized first, if you want to enable
7+
* automatic tracing for built-in plugins (HTTPs in this case).
8+
*/
9+
config.setupTracerAndExporters('https-client-service');
10+
11+
const https = require('https');
12+
const tracer = opentelemetry.getTracer();
13+
14+
/** A function which makes requests and handles response. */
15+
function makeRequest() {
16+
// span corresponds to outgoing requests. Here, we have manually created
17+
// the span, which is created to track work that happens outside of the
18+
// request lifecycle entirely.
19+
const span = tracer.startSpan('makeRequest');
20+
tracer.withSpan(span, () => {
21+
https.get({
22+
host: 'localhost',
23+
port: 443,
24+
path: '/helloworld'
25+
}, (response) => {
26+
let body = [];
27+
response.on('data', chunk => body.push(chunk));
28+
response.on('end', () => {
29+
console.log(body.toString());
30+
span.end();
31+
});
32+
});
33+
});
34+
35+
// The process must live for at least the interval past any traces that
36+
// must be exported, or some risk being lost if they are recorded after the
37+
// last export.
38+
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.')
39+
setTimeout(() => { console.log('Completed.'); }, 5000);
40+
}
41+
42+
makeRequest();
48.2 KB
Loading
49.2 KB
Loading

examples/https/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "https-example",
3+
"private": true,
4+
"version": "0.1.0",
5+
"description": "Example of HTTPs integration with OpenTelemetry",
6+
"main": "build/src/index.js",
7+
"types": "build/src/index.d.ts",
8+
"scripts": {
9+
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js",
10+
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js",
11+
"jaeger:server": "cross-env EXPORTER=jaeger node ./server.js",
12+
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.js"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git"
17+
},
18+
"keywords": [
19+
"opentelemetry",
20+
"https",
21+
"tracing"
22+
],
23+
"engines": {
24+
"node": ">=8"
25+
},
26+
"author": "OpenTelemetry Authors",
27+
"license": "Apache-2.0",
28+
"bugs": {
29+
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
30+
},
31+
"dependencies": {
32+
"@opentelemetry/core": "^0.1.0",
33+
"@opentelemetry/exporter-jaeger": "^0.1.0",
34+
"@opentelemetry/exporter-zipkin": "^0.1.0",
35+
"@opentelemetry/node-sdk": "^0.1.0",
36+
"@opentelemetry/plugin-https": "^0.1.0",
37+
"@opentelemetry/tracer-basic": "^0.1.0"
38+
},
39+
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme",
40+
"devDependencies": {
41+
"cross-env": "^6.0.0"
42+
}
43+
}

examples/https/server-cert.pem

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBqzCCARQCCQDLcUeJsLDL5jANBgkqhkiG9w0BAQUFADAaMQswCQYDVQQGEwJD
3+
QTELMAkGA1UECAwCUUMwHhcNMTkwOTI5MjIwMDI2WhcNMTkxMDI5MjIwMDI2WjAa
4+
MQswCQYDVQQGEwJDQTELMAkGA1UECAwCUUMwgZ8wDQYJKoZIhvcNAQEBBQADgY0A
5+
MIGJAoGBALhfi1dwIyC1Jha4N/j/VtlPPi+j+SZQGZqLNVVgzzGY7+cc3VkCySZD
6+
yXh3Z+/ftp9DDKdHRutJQE0R4peSDussC/IQDJKzuKN/O9S6tnNlgUr5YZLRENxL
7+
FSJIY5cIkty50IrEhlN5QeDJP8p4yrYq9J6M0yzyfdqIWI3CBqbzAgMBAAEwDQYJ
8+
KoZIhvcNAQEFBQADgYEArnOeXmXXJTK39Ma25elHxlYUZiYOBu/truy5zmx4umyS
9+
GyehAv+jRIanoCRWtOBnrjS5CY/6cC64aIVLMoqXEFIL7q/GD0wEM/DS8rN7KTcp
10+
w+nIX98srYaAFeQZScPioS6WpXz5AjbTVhvAwkIm2/s6dOlX31+1zu6Zu6ASSuQ=
11+
-----END CERTIFICATE-----

examples/https/server-key.pem

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICXQIBAAKBgQC4X4tXcCMgtSYWuDf4/1bZTz4vo/kmUBmaizVVYM8xmO/nHN1Z
3+
AskmQ8l4d2fv37afQwynR0brSUBNEeKXkg7rLAvyEAySs7ijfzvUurZzZYFK+WGS
4+
0RDcSxUiSGOXCJLcudCKxIZTeUHgyT/KeMq2KvSejNMs8n3aiFiNwgam8wIDAQAB
5+
AoGBAKBztcYQduGeBFm9VCjDvgc8KTg4kTlAeCfAglec+nOFTzJoMlGmVPuR/qFx
6+
+OgOXtXW+goRw6w7gVQQ/os9tvCCp7awSC5UCfPejHh6bW2B0BF2lZJ6B9y+u5Fa
7+
/p8oKoJGcC4eagVnDojuoYJHSqWBf7d7V/U54NpxwgBTsHAhAkEA8PJROgWzjMl2
8+
Gs5j8oBldEqzrC/d4K1uMEvCTb4RJ+t6jWq+Ug/vqvCfIcLfxHbOmTbOHTfhpv/d
9+
NUf9eDyBGwJBAMPkZaHP5vPDd900MqypLVasollzxgPnMUg35EEQJLAbb/5xG3X9
10+
ZbaVDTRtLQYNFvDZLlTpRpCPxZCgrn9hJwkCQQDPEVChLrkpqxFm5CydAZ8vG+vh
11+
dJmYNzPVKaZorYmM5yBBXJUHbU6pd3UqzJEGBJx0q9bi4V156bYvzhiVNlo1AkBu
12+
1hbvFCwPtoRmg3c8nEhL50fApzHd2XzX6M/cRF8Nyah3ZdXsz6AyS2l6RV+ZMeTO
13+
B4QghRDpEH/vUgsJhZXJAkB5GQZPJh6/kozc5+Ffc60ThN/58SX0KEFeKnWRlzfr
14+
vfBXwcmaz1oNXN+kcWdLnKbr/tx+3UQ6weRRmeYX/hOi
15+
-----END RSA PRIVATE KEY-----

examples/https/server.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const opentelemetry = require('@opentelemetry/core');
5+
const config = require('./setup');
6+
/**
7+
* The trace instance needs to be initialized first, if you want to enable
8+
* automatic tracing for built-in plugins (HTTPs in this case).
9+
*/
10+
config.setupTracerAndExporters('https-server-service');
11+
12+
const https = require('https');
13+
const tracer = opentelemetry.getTracer();
14+
15+
/** Starts a HTTPs server that receives requests on sample server port. */
16+
function startServer (port) {
17+
const options = {
18+
key: fs.readFileSync('./server-key.pem'),
19+
cert: fs.readFileSync('./server-cert.pem')
20+
};
21+
// Creates a server
22+
const server = https.createServer(options, handleRequest);
23+
// Starts the server
24+
server.listen(port, err => {
25+
if (err) {
26+
throw err;
27+
}
28+
console.log(`Node HTTPs listening on ${port}`);
29+
});
30+
}
31+
32+
/** A function which handles requests and send response. */
33+
function handleRequest (request, response) {
34+
const currentSpan = tracer.getCurrentSpan();
35+
// display traceid in the terminal
36+
console.log(`traceid: ${currentSpan.context().traceId}`);
37+
const span = tracer.startSpan('handleRequest', {
38+
parent: currentSpan,
39+
kind: 1, // server
40+
attributes: { key:'value' }
41+
});
42+
// Annotate our span to capture metadata about the operation
43+
span.addEvent('invoking handleRequest');
44+
try {
45+
let body = [];
46+
request.on('error', err => console.log(err));
47+
request.on('data', chunk => body.push(chunk));
48+
request.on('end', () => {
49+
// deliberately sleeping to mock some action.
50+
setTimeout(() => {
51+
span.end();
52+
response.end('Hello World!');
53+
}, 2000);
54+
});
55+
} catch (err) {
56+
console.log(err);
57+
span.end();
58+
}
59+
}
60+
61+
startServer(443);

examples/https/setup.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const opentelemetry = require('@opentelemetry/core');
4+
const { NodeTracer } = require('@opentelemetry/node-sdk');
5+
const { SimpleSpanProcessor } = require('@opentelemetry/tracer-basic');
6+
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
7+
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
8+
const EXPORTER = process.env.EXPORTER || '';
9+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
10+
function setupTracerAndExporters(service) {
11+
let exporter;
12+
const tracer = new NodeTracer();
13+
14+
if (EXPORTER.toLowerCase().startsWith('z')) {
15+
exporter = new ZipkinExporter({
16+
serviceName: service
17+
});
18+
} else {
19+
exporter = new JaegerExporter({
20+
serviceName: service,
21+
// The default flush interval is 5 seconds.
22+
flushInterval: 2000
23+
});
24+
}
25+
26+
tracer.addSpanProcessor(new SimpleSpanProcessor(exporter));
27+
28+
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
29+
opentelemetry.initGlobalTracer(tracer);
30+
}
31+
32+
exports.setupTracerAndExporters = setupTracerAndExporters;

packages/opentelemetry-plugin-http/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install --save @opentelemetry/plugin-http
1717

1818
## Usage
1919

20-
OpenTelemetry HTTP Instrumentation allows the user to automatically collect trace data and export them to the backend of choice, to give observability to distributed systems.
20+
OpenTelemetry HTTP Instrumentation allows the user to automatically collect trace data and export them to their backend of choice, to give observability to distributed systems.
2121

2222
To load a specific plugin (HTTP in this case), specify it in the Node Tracer's configuration.
2323
```js

0 commit comments

Comments
 (0)