Skip to content

Commit cd4324c

Browse files
committed
add lab6 using express-prom-bundle
1 parent dfa49a6 commit cd4324c

14 files changed

+2439
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ Combined all examples [lab5](./lab5/README.md)
2626

2727
Putting all the examples together, collects all metrics, sends to push gateway and available via scrape endpoint on the Node.js web server
2828

29+
## Lab 6
2930

31+
Using the prom-client express middleware wrapper [express-prom-bundle](https://www.npmjs.com/package/express-prom-bundle)

lab1/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ npm install
2727
npm start
2828
```
2929

30-
Get the metrics via `/metrics`
30+
Use the app
31+
```bash
32+
open http://localhost:8080/
3133
```
32-
curl localhost:8080/metrics
34+
35+
Get the metrics via `/metrics`
36+
```bash
37+
curl http://localhost:8080/metrics
3338
```
3439

3540
Here is an example of the output:

lab2/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ npm install
2727
npm start
2828
```
2929

30-
Get the metrics via `/metrics`
30+
Use the app
31+
```bash
32+
open http://localhost:8080/
3133
```
32-
curl localhost:8080/metrics
34+
35+
Get the metrics via `/metrics`
36+
```bash
37+
curl http://localhost:8080/metrics
3338
```
3439

3540
Here is an example of the output:

lab3/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ npm install
4444
npm start
4545
```
4646

47-
Get the metrics via `/metrics`
47+
Use the app
48+
```bash
49+
open http://localhost:8080/
4850
```
49-
curl localhost:8080/metrics
51+
52+
Get the metrics via `/metrics`
53+
```bash
54+
curl http://localhost:8080/metrics
5055
```
5156

5257
Here is an example of the output:

lab4/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ npm install
5858
npm start
5959
```
6060

61+
Use the app
62+
```bash
63+
open http://localhost:8080/
64+
```
65+
6166
The web server will push the metrics every 5 seconds as configured in the code.
6267

6368
Metrics are not available via scrape endpoint `/metrics` on the Node.js web server.

lab5/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,16 @@ npm install
7676
npm start
7777
```
7878

79+
Use the app
80+
```bash
81+
open http://localhost:8080/
82+
```
83+
7984
The web server will push the metrics every 5 seconds as configured in the code.
8085

8186
You can access the metrics from the gateway using http://localhost:9091/metrics
8287

83-
Metrics are also available via scrape endpoint http://localhost/metrics on the Node.js web server.
88+
Metrics are also available via scrape endpoint http://localhost:8080/metrics on the Node.js web server.
8489

8590
Open the UI on Prometheus Metrics http://localhost:9091/
8691

lab5/pushgateway.png

-426 KB
Binary file not shown.

lab6/.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

lab6/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM registry.access.redhat.com/ubi8/nodejs-12
2+
3+
COPY package*.json ./
4+
5+
ENV NODE_ENV=production
6+
7+
RUN npm ci
8+
9+
COPY . .
10+
11+
EXPOSE 8080
12+
13+
CMD [ "npm", "start" ]

lab6/README.md

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Using the prom-client express middleware wrapper express-prom-bundle
2+
3+
4+
See the content of [./metrics.js](./metrics.js)
5+
```js
6+
const promBundle = require("express-prom-bundle");
7+
8+
const metricsMiddleware = promBundle({
9+
includeMethod: true,
10+
includePath: true,
11+
includeUp: false,
12+
metricType: 'histogram',
13+
buckets: [0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.45, 0.5, 0.55, 0.6, 0.75, 1, 2.5, 10],
14+
promClient: {
15+
collectDefaultMetrics: {
16+
timestamps: false
17+
}
18+
}
19+
});
20+
21+
module.exports = (app) => {
22+
app.use(metricsMiddleware)
23+
}
24+
```
25+
26+
Start the application
27+
```bash
28+
npm install
29+
```
30+
31+
```bash
32+
npm start
33+
```
34+
35+
Use the app
36+
```bash
37+
open http://localhost:8080/
38+
```
39+
40+
Get the metrics via `/metrics`
41+
```bash
42+
curl http://localhost:8080/metrics
43+
```
44+
45+
Here is an example of the output:
46+
```
47+
# HELP process_cpu_user_seconds_total Total user CPU time spent in seconds.
48+
# TYPE process_cpu_user_seconds_total counter
49+
process_cpu_user_seconds_total 0.050724 1580409960992
50+
51+
# HELP process_cpu_system_seconds_total Total system CPU time spent in seconds.
52+
# TYPE process_cpu_system_seconds_total counter
53+
process_cpu_system_seconds_total 0.00534 1580409960992
54+
55+
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
56+
# TYPE process_cpu_seconds_total counter
57+
process_cpu_seconds_total 0.056064 1580409960992
58+
59+
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
60+
# TYPE process_start_time_seconds gauge
61+
process_start_time_seconds 1580409821
62+
63+
# HELP process_resident_memory_bytes Resident memory size in bytes.
64+
# TYPE process_resident_memory_bytes gauge
65+
process_resident_memory_bytes 27602944 1580409960992
66+
67+
# HELP nodejs_eventloop_lag_seconds Lag of event loop in seconds.
68+
# TYPE nodejs_eventloop_lag_seconds gauge
69+
nodejs_eventloop_lag_seconds 0.000334835 1580409960992
70+
71+
# HELP nodejs_active_handles Number of active libuv handles grouped by handle type. Every handle type is C++ class name.
72+
# TYPE nodejs_active_handles gauge
73+
nodejs_active_handles{type="WriteStream"} 2
74+
nodejs_active_handles{type="ReadStream"} 1
75+
nodejs_active_handles{type="Server"} 1
76+
nodejs_active_handles{type="Socket"} 2
77+
78+
# HELP nodejs_active_handles_total Total number of active handles.
79+
# TYPE nodejs_active_handles_total gauge
80+
nodejs_active_handles_total 6
81+
82+
# HELP nodejs_active_requests Number of active libuv requests grouped by request type. Every request type is C++ class name.
83+
# TYPE nodejs_active_requests gauge
84+
85+
# HELP nodejs_active_requests_total Total number of active requests.
86+
# TYPE nodejs_active_requests_total gauge
87+
nodejs_active_requests_total 0 1580409960992
88+
89+
# HELP nodejs_heap_size_total_bytes Process heap size from node.js in bytes.
90+
# TYPE nodejs_heap_size_total_bytes gauge
91+
nodejs_heap_size_total_bytes 7970816
92+
93+
# HELP nodejs_heap_size_used_bytes Process heap size used from node.js in bytes.
94+
# TYPE nodejs_heap_size_used_bytes gauge
95+
nodejs_heap_size_used_bytes 6371536
96+
97+
# HELP nodejs_external_memory_bytes Nodejs external memory size in bytes.
98+
# TYPE nodejs_external_memory_bytes gauge
99+
nodejs_external_memory_bytes 1346123
100+
101+
# HELP nodejs_heap_space_size_total_bytes Process heap space size total from node.js in bytes.
102+
# TYPE nodejs_heap_space_size_total_bytes gauge
103+
nodejs_heap_space_size_total_bytes{space="read_only"} 262144 1580409960992
104+
nodejs_heap_space_size_total_bytes{space="new"} 1048576 1580409960992
105+
nodejs_heap_space_size_total_bytes{space="old"} 4857856 1580409960992
106+
nodejs_heap_space_size_total_bytes{space="code"} 430080 1580409960992
107+
nodejs_heap_space_size_total_bytes{space="map"} 790528 1580409960992
108+
nodejs_heap_space_size_total_bytes{space="large_object"} 532480 1580409960992
109+
nodejs_heap_space_size_total_bytes{space="code_large_object"} 49152 1580409960992
110+
nodejs_heap_space_size_total_bytes{space="new_large_object"} 0 1580409960992
111+
112+
# HELP nodejs_heap_space_size_used_bytes Process heap space size used from node.js in bytes.
113+
# TYPE nodejs_heap_space_size_used_bytes gauge
114+
nodejs_heap_space_size_used_bytes{space="read_only"} 33040 1580409960992
115+
nodejs_heap_space_size_used_bytes{space="new"} 1005984 1580409960992
116+
nodejs_heap_space_size_used_bytes{space="old"} 4272040 1580409960992
117+
nodejs_heap_space_size_used_bytes{space="code"} 181120 1580409960992
118+
nodejs_heap_space_size_used_bytes{space="map"} 354240 1580409960992
119+
nodejs_heap_space_size_used_bytes{space="large_object"} 524344 1580409960992
120+
nodejs_heap_space_size_used_bytes{space="code_large_object"} 2784 1580409960992
121+
nodejs_heap_space_size_used_bytes{space="new_large_object"} 0 1580409960992
122+
123+
# HELP nodejs_heap_space_size_available_bytes Process heap space size available from node.js in bytes.
124+
# TYPE nodejs_heap_space_size_available_bytes gauge
125+
nodejs_heap_space_size_available_bytes{space="read_only"} 0 1580409960992
126+
nodejs_heap_space_size_available_bytes{space="new"} 41440 1580409960992
127+
nodejs_heap_space_size_available_bytes{space="old"} 570664 1580409960992
128+
nodejs_heap_space_size_available_bytes{space="code"} 12000 1580409960992
129+
nodejs_heap_space_size_available_bytes{space="map"} 434640 1580409960992
130+
nodejs_heap_space_size_available_bytes{space="large_object"} 0 1580409960992
131+
nodejs_heap_space_size_available_bytes{space="code_large_object"} 0 1580409960992
132+
nodejs_heap_space_size_available_bytes{space="new_large_object"} 1047424 1580409960992
133+
134+
# HELP nodejs_version_info Node.js version info.
135+
# TYPE nodejs_version_info gauge
136+
nodejs_version_info{version="v13.7.0",major="13",minor="7",patch="0"} 1
137+
138+
# HELP http_request_duration_seconds duration histogram of http responses labeled with: status_code, method, path
139+
# TYPE http_request_duration_seconds histogram
140+
http_request_duration_seconds_bucket{le="0.025",status_code="200",method="GET",path="/"} 12
141+
http_request_duration_seconds_bucket{le="0.05",status_code="200",method="GET",path="/"} 12
142+
http_request_duration_seconds_bucket{le="0.1",status_code="200",method="GET",path="/"} 12
143+
http_request_duration_seconds_bucket{le="0.15",status_code="200",method="GET",path="/"} 17
144+
http_request_duration_seconds_bucket{le="0.2",status_code="200",method="GET",path="/"} 17
145+
http_request_duration_seconds_bucket{le="0.25",status_code="200",method="GET",path="/"} 17
146+
http_request_duration_seconds_bucket{le="0.3",status_code="200",method="GET",path="/"} 17
147+
http_request_duration_seconds_bucket{le="0.35",status_code="200",method="GET",path="/"} 17
148+
http_request_duration_seconds_bucket{le="0.45",status_code="200",method="GET",path="/"} 17
149+
http_request_duration_seconds_bucket{le="0.5",status_code="200",method="GET",path="/"} 17
150+
http_request_duration_seconds_bucket{le="0.55",status_code="200",method="GET",path="/"} 17
151+
http_request_duration_seconds_bucket{le="0.6",status_code="200",method="GET",path="/"} 17
152+
http_request_duration_seconds_bucket{le="0.75",status_code="200",method="GET",path="/"} 17
153+
http_request_duration_seconds_bucket{le="1",status_code="200",method="GET",path="/"} 17
154+
http_request_duration_seconds_bucket{le="2.5",status_code="200",method="GET",path="/"} 17
155+
http_request_duration_seconds_bucket{le="10",status_code="200",method="GET",path="/"} 17
156+
http_request_duration_seconds_bucket{le="+Inf",status_code="200",method="GET",path="/"} 17
157+
http_request_duration_seconds_sum{status_code="200",method="GET",path="/"} 0.5371957699999999
158+
http_request_duration_seconds_count{status_code="200",method="GET",path="/"} 17
159+
```
160+

lab6/app.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require('express')
2+
const app = express()
3+
const port = process.env.PORT | 8080
4+
5+
// Add /metrics endpoint
6+
require('./metrics.js')(app)
7+
8+
let counter = 1
9+
app.get('/', async (req, res) => {
10+
// simulate a slow request every 3 requests
11+
setTimeout(async () => {
12+
res.send('Hello World')
13+
}, counter++ % 3 === 0 ? 100 : 0)
14+
})
15+
app.disable('etag')
16+
app.listen(port, () => console.log(`Service listening on port ${port}!`))
17+
18+
module.exports = app

lab6/metrics.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const promBundle = require("express-prom-bundle");
2+
3+
const metricsMiddleware = promBundle({
4+
includeMethod: true,
5+
includePath: true,
6+
includeUp: false,
7+
metricType: 'histogram',
8+
buckets: [0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.45, 0.5, 0.55, 0.6, 0.75, 1, 2.5, 10],
9+
promClient: {
10+
collectDefaultMetrics: {
11+
timestamps: false
12+
}
13+
}
14+
});
15+
16+
module.exports = (app) => {
17+
app.use(metricsMiddleware)
18+
}

0 commit comments

Comments
 (0)