forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (42 loc) · 1.28 KB
/
server.js
File metadata and controls
58 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const http = require('http');
const opentracing = require('opentracing');
const utils = require('./utils');
const shim = require('./shim').shim('http_server_opentracing');
opentracing.initGlobalTracer(shim);
const tracer = opentracing.globalTracer();
startServer(3000);
function startServer(port) {
const server = http.createServer(handleRequest);
server.listen(port, (err) => {
if (err) throw err;
console.log(`Server is listening on ${port}`);
});
}
async function handleRequest(req, res) {
const parentSpan = tracer.extract(
opentracing.FORMAT_HTTP_HEADERS,
req.headers,
);
const span = tracer.startSpan('handle_request', {
childOf: parentSpan,
});
span.setTag('custom', 'tag value');
span.setTag('alpha', '1000');
await doSomething(span);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(
JSON.stringify({ status: 'OK', traceId: span.spanContext().toTraceId() }),
);
res.end();
span.finish();
}
async function doSomething(parentSpan) {
const span = tracer.startSpan('do_something', { childOf: parentSpan });
span.setTag('alpha', '200');
span.setTag('beta', '50');
span.log({ state: 'waiting' });
// deliberately sleeping to mock some action.
await utils.sleep(1000);
span.finish();
}