* Basic process metrics * Enable Prometheus by an environment variable * Code formatting * Documentation for Prometheus metrics * Link from README to documentation of Prometheus * Link from README to documentation of Prometheus * Link from README to documentation of Prometheus * Separate module for metrics + tests * Metrics limited by IP * Metrics are forbidded for all requets by default * Code refactoring * allowedIps passed as a string to PrometheusMetrics * Handle missing config * METRICS_PROMETHEUS_ALLOWED_IPS added to documentation * Log info about enabled metrics * Unused code removed * package-lock.json updated * prom-client updated to 11.1.2 * Code refactoring * Do not read IP address from X-Forwarder-For header
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
'use strict'
|
|
|
|
const { expect } = require('chai')
|
|
const Camp = require('camp')
|
|
const fetch = require('node-fetch')
|
|
const config = require('../test-config')
|
|
const Metrics = require('./prometheus-metrics')
|
|
|
|
describe('Prometheus metrics route', function() {
|
|
const baseUrl = `http://127.0.0.1:${config.port}`
|
|
|
|
let camp
|
|
afterEach(function(done) {
|
|
if (camp) {
|
|
camp.close(() => done())
|
|
camp = undefined
|
|
}
|
|
})
|
|
|
|
function startServer(metricsConfig) {
|
|
return new Promise((resolve, reject) => {
|
|
camp = Camp.start({ port: config.port, hostname: '::' })
|
|
const metrics = new Metrics(metricsConfig)
|
|
metrics.initialize(camp)
|
|
camp.on('listening', () => resolve())
|
|
})
|
|
}
|
|
|
|
it('returns 404 when metrics are disabled', async function() {
|
|
startServer({ enabled: false })
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(404)
|
|
expect(await res.text()).to.not.contains('nodejs_version_info')
|
|
})
|
|
|
|
it('returns 404 when there is no configuration', async function() {
|
|
startServer()
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(404)
|
|
expect(await res.text()).to.not.contains('nodejs_version_info')
|
|
})
|
|
|
|
it('returns metrics for allowed IP', async function() {
|
|
startServer({
|
|
enabled: true,
|
|
allowedIps: '^(127\\.0\\.0\\.1|::1|::ffff:127\\.0\\.0\\.1)$',
|
|
})
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(200)
|
|
expect(await res.text()).to.contains('nodejs_version_info')
|
|
})
|
|
|
|
it('returns metrics for request from allowed remote address', async function() {
|
|
startServer({
|
|
enabled: true,
|
|
allowedIps: '^(127\\.0\\.0\\.1|::1|::ffff:127\\.0\\.0\\.1)$',
|
|
})
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(200)
|
|
expect(await res.text()).to.contains('nodejs_version_info')
|
|
})
|
|
|
|
it('returns 403 for not allowed IP', async function() {
|
|
startServer({
|
|
enabled: true,
|
|
allowedIps: '^127\\.0\\.0\\.200$',
|
|
})
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(403)
|
|
expect(await res.text()).to.not.contains('nodejs_version_info')
|
|
})
|
|
|
|
it('returns 403 for every request when list with allowed IPs not defined', async function() {
|
|
startServer({
|
|
enabled: true,
|
|
})
|
|
|
|
const res = await fetch(`${baseUrl}/metrics`)
|
|
|
|
expect(res.status).to.be.equal(403)
|
|
expect(await res.text()).to.not.contains('nodejs_version_info')
|
|
})
|
|
})
|