forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
155 lines (131 loc) · 4.82 KB
/
metrics.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const Prometheus = require('prom-client')
const onFinished = require('on-finished')
const orders = require('../data/mongodb').orders
const reviews = require('../data/mongodb').reviews
const challenges = require('../data/datacache').challenges
const utils = require('../lib/utils')
const config = require('config')
const models = require('../models')
const Op = models.Sequelize.Op
const register = Prometheus.register
const fileUploadsCountMetric = new Prometheus.Counter({
name: 'file_uploads_count',
help: 'Total number of successful file uploads grouped by file type.',
labelNames: ['file_type']
})
const fileUploadErrorsMetric = new Prometheus.Counter({
name: 'file_upload_errors',
help: 'Total number of failed file uploads grouped by file type.',
labelNames: ['file_type']
})
exports.observeRequestMetricsMiddleware = function observeRequestMetricsMiddleware () {
const httpRequestsMetric = new Prometheus.Counter({
name: 'http_requests_count',
help: 'Total HTTP request count grouped by status code.',
labelNames: ['status_code']
})
return (req, res, next) => {
onFinished(res, () => {
const statusCode = `${Math.floor(res.statusCode / 100)}XX`
httpRequestsMetric.labels(statusCode).inc()
})
next()
}
}
exports.observeFileUploadMetricsMiddleware = function observeFileUploadMetricsMiddleware () {
return ({ file }, res, next) => {
onFinished(res, () => {
if (file) {
res.statusCode < 400 ? fileUploadsCountMetric.labels(file.mimetype).inc() : fileUploadErrorsMetric.labels(file.mimetype).inc()
}
})
next()
}
}
exports.serveMetrics = function serveMetrics () {
return (req, res, next) => {
utils.solveIf(challenges.exposedMetricsChallenge, () => {
const userAgent = req.headers['user-agent'] || ''
return !userAgent.includes('Prometheus')
})
res.set('Content-Type', register.contentType)
res.end(register.metrics())
}
}
exports.observeMetrics = function observeMetrics () {
const app = config.get('application.customMetricsPrefix')
const intervalCollector = Prometheus.collectDefaultMetrics({ timeout: 5000 })
register.setDefaultLabels({ app })
const challengeSolvedMetrics = new Prometheus.Gauge({
name: `${app}_challenges_solved`,
help: 'Number of solved challenges grouped by difficulty.',
labelNames: ['difficulty']
})
const challengeTotalMetrics = new Prometheus.Gauge({
name: `${app}_challenges_total`,
help: 'Total number of challenges grouped by difficulty.',
labelNames: ['difficulty']
})
const orderMetrics = new Prometheus.Gauge({
name: `${app}_orders_placed_total`,
help: `Number of orders placed in ${config.get('application.name')}.`
})
const userMetrics = new Prometheus.Gauge({
name: `${app}_users_registered`,
help: 'Number of registered users grouped by customer type.',
labelNames: ['type']
})
const userTotalMetrics = new Prometheus.Gauge({
name: `${app}_users_registered_total`,
help: 'Total number of registered users.'
})
const walletMetrics = new Prometheus.Gauge({
name: `${app}_wallet_balance_total`,
help: 'Total balance of all users\' digital wallets.'
})
const interactionsMetrics = new Prometheus.Gauge({
name: `${app}_user_social_interactions`,
help: 'Number of social interactions with users grouped by type.',
labelNames: ['type']
})
const updateLoop = setInterval(() => {
const challengeKeys = Object.keys(challenges)
for (let difficulty = 1; difficulty <= 6; difficulty++) {
challengeSolvedMetrics.set({ difficulty }, challengeKeys.filter((key) => (challenges[key].difficulty === difficulty && challenges[key].solved)).length)
challengeTotalMetrics.set({ difficulty }, challengeKeys.filter((key) => (challenges[key].difficulty === difficulty)).length)
}
orders.count({}).then(orders => {
orderMetrics.set(orders)
})
reviews.count({}).then(reviews => {
interactionsMetrics.set({ type: 'review' }, reviews)
})
models.User.count({ where: { role: { [Op.eq]: ['customer'] } } }).then(count => {
userMetrics.set({ type: 'standard' }, count)
})
models.User.count({ where: { role: { [Op.eq]: 'deluxe' } } }).then(count => {
userMetrics.set({ type: 'deluxe' }, count)
})
models.User.count().then(count => {
userTotalMetrics.set(count)
})
models.Wallet.sum('balance').then(totalBalance => {
walletMetrics.set(totalBalance)
})
models.Feedback.count().then(count => {
interactionsMetrics.set({ type: 'feedback' }, count)
})
models.Complaint.count().then(count => {
interactionsMetrics.set({ type: 'complaint' }, count)
})
}, 5000)
return {
register: register,
probe: intervalCollector,
updateLoop: updateLoop
}
}