Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (123 loc) · 3.58 KB
/
Copy pathapp.js
File metadata and controls
142 lines (123 loc) · 3.58 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const swaggerUI = require('swagger-ui-express');
const YAML = require('yamljs');
const apiDocYaml = YAML.load('./open_api/OpenApi3.yaml');
const apiDocJson = require('./open_api/OpenApi3.json');
const promBundle = require("express-prom-bundle");
const initTracer = require("jaeger-client").initTracer;
const http = require("http");
const routes = require('./routes/index');
const users = require('./routes/users');
const events = require('./routes/events');
const app = express();
// INIT Prometheus metrics
const metricsMiddleware = promBundle({
buckets: [0.002, 0.005, 0.01, 0.05, 0.1, 1],
includeMethod: true,
promClient: {
collectDefaultMetrics: {
timeout: 1000
}
},
});
// INIT Jaeger tracing
// See schema https://github.com/jaegertracing/jaeger-client-node/blob/master/src/configuration.js#L37
const jaegerConfig = {
serviceName: 'user-device-db',
reporter: {
// Provide the traces endpoint; this forces the client to connect directly to the Collector and send
// spans over HTTP
collectorEndpoint: 'http://localhost:14268/api/traces',
// Provide username and password if authentication is enabled in the Collector
// username: '',
// password: '',
},
};
const jaegerOptions = {
tags: {
'user-device-db': '1.0.0',
},
//metrics: metrics,
logger: console,
};
const tracer = initTracer(jaegerConfig, jaegerOptions);
app.use(function (req, res, next) {
const span = tracer.startSpan('http_request');
const opts = {
host : 'example.com',
method: 'GET',
port : '80',
path: '/',
};
http.request(opts, res => {
res.setEncoding('utf8');
res.on('error', err => {
// assuming no retries, mark the span as failed
span.setTag(opentracing.Tags.ERROR, true);
span.log({'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack});
span.finish();
});
res.on('data', chunk => {
span.log({'event': 'data_received', 'chunk_length': chunk.length});
});
res.on('end', () => {
span.log({'event': 'request_end'});
span.finish();
});
}).end();
console.log("** Span: %j", span);
next();
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(metricsMiddleware);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// enable Swagger UI
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(apiDocJson));
app.use(function (req, res, next) {
res.header("Content-Type",'application/json');
next();
});
app.use('/', routes);
app.use('/users', users);
app.use('/events', events);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
module.exports = app;