-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (63 loc) · 1.92 KB
/
server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { createProxyMiddleware } = require('http-proxy-middleware');
const express = require('express')
const app = express()
const port = 3000
const {initTracer, ZipkinB3TextMapCodec, opentracing} = require("jaeger-client");
function initTrace(serviceName) {
var config = {
serviceName: serviceName,
sampler: {
type: "const",
param: 1,
},
reporter: {
collectorEndpoint: process.env.JAEGER_URL || 'http://localhost:14268/api/traces'
},
};
var options = {
tags: {
'version': '0.0.1',
},
logger: {
info: function logInfo(msg) {
console.log("INFO ", msg);
},
error: function logError(msg) {
console.log("ERROR", msg);
},
},
};
return initTracer(config, options);
}
const tracer = initTrace('warehouse-react');
const codec = new ZipkinB3TextMapCodec({ urlEncoding: true });
tracer.registerInjector(opentracing.FORMAT_HTTP_HEADERS, codec);
tracer.registerExtractor(opentracing.FORMAT_HTTP_HEADERS, codec);
app.use('/api/*', (req, res, next) => {
const span = tracer.startSpan(req.baseUrl);
const spanCtx = {};
tracer.inject(span, opentracing.FORMAT_HTTP_HEADERS, spanCtx);
req.spanContext = spanCtx;
for(let i = 0; i < req.rawHeaders.length; i += 2){
span.setTag(`request.header.${req.rawHeaders[i]}`, req.rawHeaders[i+1]);
}
span.setTag(`request.query`, req.query);
res.on('finish', () => {
span.setTag(`http.response_code`, res.statusCode);
span.finish();
});
next();
});
app.use('/api', createProxyMiddleware({
target: process.env.WAREHOUSE_URL || 'http://localhost:3001',
changeOrigin: true,
onProxyReq(proxyReq, req, res) {
Object.keys(req.spanContext).forEach((k) => {
proxyReq.setHeader(k, req.spanContext[k]);
});
}
}));
app.use(express.static('build'))
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})