Skip to content

Commit 6e73ff3

Browse files
junminstorageyurishkuro
authored andcommitted
Add nodejs tutorial (yurishkuro#5)
1 parent 81d14cd commit 6e73ff3

File tree

15 files changed

+714
-28
lines changed

15 files changed

+714
-28
lines changed

nodejs/lesson01/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
**NOTE**
2+
3+
This README is currently incomplete / unfinished. Please refer to respective README in tutorials for one of the other languages
4+
5+
6+
# Lesson 1 - Hello World
7+
8+
## Objectives
9+
10+
Learn how to:
11+
12+
* Instantiate a Tracer
13+
* Create a simple trace
14+
* Annotate the trace
15+
16+
## Walkthrough
17+
18+
### A simple Hello-World program
19+
20+
21+
Run it:
22+
```
23+
npm install
24+
node lesson01/solution/hello.js Peter
25+
INFO Initializing Jaeger Tracer with CompositeReporter and ConstSampler
26+
Hello app listening on port 8080
27+
28+
```
29+
30+
Run the following curl command a few times:
31+
32+
```
33+
curl localhost:8080
34+
```
35+
36+
You should see something below on the console for the client app:
37+
38+
```
39+
Hello, Peter!
40+
INFO Reporting span 6d8e165388a35fb5:6d8e165388a35fb5:0:1
41+
Hello, Peter!
42+
INFO Reporting span 48b662d422dfcc86:48b662d422dfcc86:0:1
43+
Hello, Peter!
44+
INFO Reporting span c0e45d92229168c5:c0e45d92229168c5:0:1
45+
```

nodejs/lesson01/solution/hello.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
'use strict';
22

3-
var express = require('express')
4-
var app = express()
5-
var initTracer = require('../../lib/tracing').initTracer;
6-
7-
app.get('/', function (req, res) {
8-
var helloStr = sayHello(helloTo);
9-
res.send(helloStr)
10-
})
3+
const assert = require('assert');
4+
const initTracer = require('../../lib/tracing').initTracer;
115

126
function sayHello(helloTo) {
137
var span = tracer.startSpan('say-hello');
14-
15-
var helloStr = 'Hello, ' + helloTo + '!';
8+
span.setTag('hello-to', helloTo);
9+
10+
var helloStr = `Hello, ${helloTo}!`;
1611
span.log({
17-
'event': 'format-string',
12+
'event': 'string-format',
1813
'value': helloStr
1914
});
2015

2116
console.log(helloStr);
17+
2218
span.log({'event': 'print-string'})
23-
2419
span.finish();
25-
26-
return helloStr;
2720
}
2821

29-
if (process.argv.length != 3) {
30-
throw new Error('expecting one argument')
31-
}
22+
assert.ok(process.argv.length == 3, 'expecting one argument');
23+
24+
const helloTo = process.argv[2];
25+
26+
const tracer = initTracer('hello-world');
27+
28+
sayHello(helloTo);
3229

33-
var helloTo = process.argv[2];
30+
//wait for udp message sent out
31+
setTimeout( e => {tracer.close();}, 12000);
3432

35-
var tracer = initTracer('hello-world');
3633

37-
app.listen(8080, function () {
38-
console.log('Hello app listening on port 8080')
39-
})

nodejs/lesson02/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**NOTE**
2+
3+
This README is currently incomplete / unfinished. Please refer to respective README in tutorials for one of the other languages
4+
5+
6+
# Lesson 2 - Context and Tracing Functions
7+
8+
## Objectives
9+
10+
Learn how to:
11+
12+
* Trace individual functions
13+
* Combine multiple spans into a single trace
14+
* Propagate the in-process context
15+
16+
## Walkthrough
17+
18+
### A simple Hello-World program
19+
20+
```
21+
22+
Run it:
23+
```
24+
npm install
25+
node lesson02/solution/hello.js Peter
26+
INFO Initializing Jaeger Tracer with CompositeReporter and ConstSampler
27+
Hello app listening on port 8080
28+
29+
```
30+
31+
```
32+
33+
You should see something below on the console for the client app:
34+
35+
```
36+
INFO Reporting span d6c674cb77b1ba9c:933a7b0d05ed1b48:d6c674cb77b1ba9c:1
37+
Hello, Peter!
38+
INFO Reporting span d6c674cb77b1ba9c:263fe6d3cb907321:d6c674cb77b1ba9c:1
39+
INFO Reporting span d6c674cb77b1ba9c:d6c674cb77b1ba9c:0:1
40+
```

nodejs/lesson02/solution/hello.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const initTracer = require('../../lib/tracing').initTracer;
5+
6+
function sayHello(helloTo) {
7+
var span = tracer.startSpan('say-hello');
8+
span.setTag('hello-to', helloTo);
9+
10+
var helloStr = format_string(helloTo, span);
11+
12+
print_string(helloStr, span);
13+
14+
span.finish();
15+
16+
return helloStr;
17+
}
18+
19+
function format_string(helloTo, root_span) {
20+
var span = tracer.startSpan('format_string', {childOf: root_span.context()});
21+
var formattedStr = `Hello, ${helloTo}!`;
22+
23+
span.log({
24+
'event': 'format-string',
25+
'value': helloTo
26+
});
27+
28+
span.finish();
29+
return formattedStr;
30+
}
31+
32+
function print_string(helloStr, root_span) {
33+
var span = tracer.startSpan('print_string', {childOf: root_span.context()});
34+
35+
console.log(helloStr);
36+
37+
span.log({
38+
'event': 'print-string',
39+
'value': helloStr
40+
});
41+
span.finish();
42+
}
43+
44+
assert.ok(process.argv.length == 3, 'expecting one argument');
45+
46+
const helloTo = process.argv[2];
47+
48+
const tracer = initTracer('hello-world');
49+
50+
sayHello(helloTo);
51+
52+
setTimeout( e => {tracer.close();}, 12000);

nodejs/lesson03/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
**NOTE**
2+
3+
This README is currently incomplete / unfinished. Please refer to respective README in tutorials for one of the other languages
4+
5+
6+
# Lesson 3 - Tracing RPC Requests
7+
8+
## Objectives
9+
10+
Learn how to:
11+
12+
* Trace a transaction across more than one microservice
13+
* Pass the context between processes using `Inject` and `Extract`
14+
* Apply OpenTracing-recommended tags
15+
16+
## Walkthrough
17+
18+
### Hello-World Microservice App
19+
20+
To test it out, first run the formatter and publisher services in separate terminals
21+
22+
```
23+
# terminal 1
24+
$ node lesson03/solution/formatter.js
25+
* Running on http://localhost:8081/ (Press CTRL+C to quit)
26+
27+
# terminal 3
28+
$ node lesson03/solution/publisher.js
29+
* Running on http://localhost:8082/ (Press CTRL+C to quit)
30+
```
31+
32+
Finally, if we run the client app as we did in the previous lessons:
33+
34+
```
35+
$ node lesson03/solution/hello.js Peter
36+
INFO Initializing Jaeger Tracer with CompositeReporter and ConstSampler
37+
Hello app listening on port 8080
38+
INFO Reporting span 80c31f112061d86e:1def63dfd6d6755d:80c31f112061d86e:1
39+
INFO Reporting span 80c31f112061d86e:e47ca83f948cb0c4:80c31f112061d86e:1
40+
INFO Reporting span 80c31f112061d86e:80c31f112061d86e:0:1
41+
```
42+
43+
On formatter terminal screen:
44+
```
45+
INFO Initializing Jaeger Tracer with CompositeReporter and ConstSampler
46+
Formatter app listening on port 8081
47+
INFO Reporting span 80c31f112061d86e:9ad7c16acf5f0994:1def63dfd6d6755d:1
48+
```
49+
50+
On publisher terminal screen:
51+
```
52+
INFO Initializing Jaeger Tracer with CompositeReporter and ConstSampler
53+
Publisher app listening on port 8082
54+
Hello, Peter!
55+
INFO Reporting span 80c31f112061d86e:f3211e5bb77c5f2b:e47ca83f948cb0c4:1
56+
```

nodejs/lesson03/solution/formatter.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const express = require('express')
4+
const app = express()
5+
const initTracer = require('../../lib/tracing').initTracer;
6+
const { Tags, FORMAT_HTTP_HEADERS } = require('opentracing');
7+
8+
const tracer = initTracer('format-service');
9+
10+
const port = 8081;
11+
12+
app.listen(port, function () {
13+
console.log('Formatter app listening on port ' + port);
14+
})
15+
16+
app.get('/format', function (req, res) {
17+
const parentSpanContext = tracer.extract(FORMAT_HTTP_HEADERS, req.headers)
18+
const span = tracer.startSpan('http_server', {
19+
childOf: parentSpanContext,
20+
tags: {[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER}
21+
});
22+
23+
const helloTo= req.query.helloTo;
24+
25+
span.log({
26+
'event': 'format',
27+
'value': helloTo
28+
});
29+
30+
span.finish();
31+
32+
res.send(`Hello, ${helloTo}!`);
33+
})
34+
35+
36+

nodejs/lesson03/solution/hello.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const initTracer = require('../../lib/tracing').initTracer;
5+
const request = require('request-promise');
6+
const { Tags, FORMAT_HTTP_HEADERS } = require('opentracing');
7+
8+
function sayHello(helloTo) {
9+
10+
const span = tracer.startSpan('say-hello');
11+
span.setTag('hello-to', helloTo);
12+
13+
format_string(helloTo, span)
14+
.then( data => {
15+
return print_hello(data, span);
16+
})
17+
.then( data => {
18+
span.setTag(Tags.HTTP_STATUS_CODE, 200)
19+
span.finish();
20+
})
21+
.catch( err => {
22+
span.setTag(Tags.ERROR, true)
23+
span.setTag(Tags.HTTP_STATUS_CODE, err.statusCode || 500);
24+
span.finish();
25+
});
26+
27+
}
28+
29+
function format_string(input, root_span) {
30+
const url = `http://localhost:8081/format?helloTo=${input}`;
31+
const fn = 'format';
32+
33+
const span = tracer.startSpan(fn, {childOf: root_span.context()});
34+
span.log({
35+
'event': 'format-string',
36+
'value': input
37+
});
38+
39+
return http_get(fn, url, span);
40+
}
41+
42+
function print_hello(input, root_span) {
43+
const url = `http://localhost:8082/publish?helloStr=${input}`;
44+
const fn = 'publish';
45+
46+
const span = tracer.startSpan(fn, {childOf: root_span.context()});
47+
span.log({
48+
'event': 'print-string',
49+
'value': input
50+
});
51+
return http_get(fn, url, span);
52+
}
53+
54+
function http_get(fn, url, span) {
55+
const method = 'GET';
56+
const headers = {};
57+
58+
span.setTag(Tags.HTTP_URL, url);
59+
span.setTag(Tags.HTTP_METHOD, method);
60+
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_CLIENT);
61+
// Send span context via request headers (parent id etc.)
62+
tracer.inject(span, FORMAT_HTTP_HEADERS, headers);
63+
64+
return request({url, method, headers})
65+
.then( data => {
66+
span.finish();
67+
return data;
68+
}, e => {
69+
span.finish();
70+
throw e;
71+
});
72+
73+
}
74+
75+
assert.ok(process.argv.length == 3, 'expecting one argument');
76+
77+
const helloTo = process.argv[2];
78+
79+
const tracer = initTracer('hello-world');
80+
81+
sayHello(helloTo);
82+
83+
setTimeout( e => {tracer.close();}, 12000);
84+
85+
86+
87+

0 commit comments

Comments
 (0)