Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /* istanbul ignore file */
'use strict';
const prometheus = require('prom-client');
const os = require('os');
const path = require('path');
const { ServiceProvider } = use('@adonisjs/fold');
class PrometheusProvider extends ServiceProvider {
/**
* Register namespaces to the IoC container
*
* @return {void}
*/
register() {
this.app.autoload(path.join(__dirname, '../src'), 'Prometheus');
/**
* Configure Prometheus.
*/
const prometheusConfig = require('../config');
const Config = this.app.use('Config');
Config.set('addons.prometheus', prometheusConfig);
this.app.singleton('C2C/Addons/Prometheus', () => {
/**
* Configure system metrics collection.
*/
if (prometheusConfig.systemMetrics.enabled) {
const { enabled, ...params } = prometheusConfig.systemMetrics;
prometheus.collectDefaultMetrics(params);
}
/**
* Set default label: hostname & processId
*/
prometheus.register.setDefaultLabels({
hostname: os.hostname(),
pid: `${process.pid}`,
});
return prometheus;
});
}
/**
* On boot add commands with ace.
*
* @return {void}
*/
boot() {
/**
* Register alias.
*/
this.app.alias('C2C/Addons/Prometheus', 'Prometheus');
// initialize singleton instance Prometheus & import metrics
const customMetrics = require('../src/Metrics');
const Config = this.app.use('Config');
/**
* Expose metrics via API endpoint.
*/
if (Config.get('addons.prometheus.exposeHttpEndpoint')) {
this._exposeMetricViaApi(Config.get('addons.prometheus.endpoint', '/metrics'));
}
/**
* Setup uptime metrics.
*/
if (Config.get('addons.prometheus.uptimeMetric.enabled')) {
customMetrics.uptimeMetric.inc(1);
}
/**
* Register middleware
*/
this._registerMiddleware();
}
/**
* Expose metrics via API endpoint.
*/
_exposeMetricViaApi(urlPath) {
const Route = this.app.use('Route');
/**
* Create route.
*/
Route.get(urlPath, async ({ response }) =>
response
.header('Content-type', prometheus.register.contentType)
.ok(await prometheus.register.metrics()),
);
const Logger = this.app.use('Logger');
Logger.info(`[PrometheusProvider] - Prometheus metrics is on path ${urlPath}`);
}
_registerMiddleware() {
require('../start/kernel');
}
}
module.exports = PrometheusProvider;
|