-
Notifications
You must be signed in to change notification settings - Fork 4
/
backend.js
executable file
·51 lines (40 loc) · 1.58 KB
/
backend.js
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
/* eslint-disable import/newline-after-import */
// general variables
const rest = require('rest');
const express = require('express');
const app = express();
const sleep = require('sleep');
const {recorder} = require('./recorder');
// initialize tracer
const CLSContext = require('zipkin-context-cls');
const {Tracer} = require('zipkin');
const ctxImpl = new CLSContext('zipkin');
const tracer = new Tracer({ctxImpl, recorder});
// instrument the server
const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware;
app.use(zipkinMiddleware({
tracer,
serviceName: 'backend' // name of this application
}));
// instrument the client
const {restInterceptor} = require('zipkin-instrumentation-cujojs-rest');
const zipkinRest = rest.wrap(restInterceptor, {tracer, serviceName: 'backend'});
// Allow cross-origin, traced requests. See http://enable-cors.org/server_expressjs.html
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', [
'Origin', 'Accept', 'X-Requested-With', 'X-B3-TraceId',
'X-B3-ParentSpanId', 'X-B3-SpanId', 'X-B3-Sampled',
'Content-Type', 'Authorization'
].join(', '));
next();
});
app.get('/backend', (req, res) => {
zipkinRest('http://localhost:9002/magical?minute=' + encodeURIComponent(new Date().getMinutes().toString()) )
.then( sleep.sleep(Math.floor(Math.random() * 2)+1) )
.then(response => res.send(response.entity))
.catch(err => console.error('Error', err.stack));
});
app.listen(9000, () => {
console.log('Backend listening on port 9000!');
});