forked from openzipkin/zipkin-js-example
/
frontend.js
43 lines (36 loc) · 1.36 KB
/
frontend.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
/* eslint-disable import/newline-after-import */
// initialize tracer
const rest = require('rest');
const express = require('express');
const CLSContext = require('zipkin-context-cls');
const {Tracer} = require('zipkin');
const {recorder} = require('./recorder');
const ctxImpl = new CLSContext('zipkin');
const tracer = new Tracer({ctxImpl, recorder});
const app = express();
// instrument the server
const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware;
app.use(zipkinMiddleware({
tracer,
serviceName: 'frontend' // name of this application
}));
// instrument the client
const {restInterceptor} = require('zipkin-instrumentation-cujojs-rest');
const zipkinRest = rest.wrap(restInterceptor, {tracer, serviceName: 'frontend'});
// 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'
].join(', '));
next();
});
app.get('/', (req, res) => {
zipkinRest('http://localhost:9000/api')
.then(response => res.send(response.entity))
.catch(err => console.error('Error', err.stack));
});
app.listen(8081, () => {
console.log('Frontend listening on port 8081!');
});