Revision 56033
Added by Konstantinos Triantafyllou almost 5 years ago
server.ts | ||
---|---|---|
1 | 1 |
import 'zone.js/dist/zone-node'; |
2 | 2 |
import 'reflect-metadata'; |
3 |
import { renderModuleFactory } from '@angular/platform-server'; |
|
4 | 3 |
import { enableProdMode } from '@angular/core'; |
5 | 4 |
|
6 | 5 |
import * as express from 'express'; |
... | ... | |
26 | 25 |
import {ngExpressEngine, RenderOptions} from '@nguniversal/express-engine'; |
27 | 26 |
// Import module map for lazy loading |
28 | 27 |
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; |
29 |
import {Prometheus} from "./prometheus";
|
|
30 |
import {Counter} from "prom-client";
|
|
28 |
import {routes} from "./routes";
|
|
29 |
import * as prom from "prom-client";
|
|
31 | 30 |
|
32 | 31 |
/* |
33 | 32 |
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) |
... | ... | |
40 | 39 |
*/ |
41 | 40 |
// be able to get request and get domain from there |
42 | 41 |
|
43 |
const prometheus: Prometheus = new Prometheus();
|
|
42 |
const register = new prom.Registry();
|
|
44 | 43 |
|
44 |
const counter = new prom.Counter({ |
|
45 |
name: 'explore', |
|
46 |
help: 'Explore Counter', |
|
47 |
labelNames: ['route'], |
|
48 |
registers: [register] |
|
49 |
}); |
|
50 |
|
|
51 |
const histogram = new prom.Histogram({ |
|
52 |
name: 'exploreLatency', |
|
53 |
help: 'Explore Histogram', |
|
54 |
labelNames: ['route'], |
|
55 |
registers: [register], |
|
56 |
buckets: [0.5, 1, 5, 10] |
|
57 |
}); |
|
58 |
|
|
45 | 59 |
app.engine('html', (_, options:RenderOptions, callback) => { |
46 | 60 |
let engine = ngExpressEngine({ |
47 | 61 |
bootstrap: AppServerModuleNgFactory, |
... | ... | |
66 | 80 |
})); |
67 | 81 |
|
68 | 82 |
app.get('/metrics', (req, res) => { |
69 |
res.set('Content-Type', prometheus.register.contentType);
|
|
70 |
res.end(prometheus.register.metrics());
|
|
83 |
res.set('Content-Type', register.contentType); |
|
84 |
res.end(register.metrics()); |
|
71 | 85 |
}); |
72 | 86 |
|
73 | 87 |
// All regular routes use the Universal engine |
74 | 88 |
app.get('*', (req, res) => { |
75 |
let start = new Date(); |
|
76 |
let counter: Counter = prometheus.counters.get(req.path); |
|
77 |
if(counter !== undefined) { |
|
78 |
counter.inc(1, new Date()); |
|
79 |
res.render('index', { req }); |
|
80 |
// event triggers when express is done sending response |
|
81 |
res.on('finish', function() { |
|
82 |
// console.log(new Date().getTime() - start.getTime()); |
|
83 |
}); |
|
84 |
} else { |
|
85 |
res.render('index', { req }); |
|
86 |
} |
|
89 |
if(routes.indexOf(req.path) !== -1) { |
|
90 |
const end = histogram.startTimer({route: req.path}); |
|
91 |
counter.inc({route: req.path}); |
|
92 |
res.render('index', { req }); |
|
93 |
res.on('finish', function() { |
|
94 |
end(); |
|
95 |
}); |
|
96 |
} else { |
|
97 |
res.render('index', { req }); |
|
98 |
} |
|
87 | 99 |
}); |
88 | 100 |
|
89 | 101 |
// Start up the Node server |
Also available in: Unified diff
[Trunk|Explore]: 1. Modify prometheus usage: Remove counters and a routes variable. 2. Create a Counter and a Histogram with label route. 3. At every request of a route, counter with label the specific route is increased by 1 and latency is observed at Histogram with the same logic.