Add Blink Prometheus listener (#11509)
This commit is contained in:
+24
-2
@@ -4,10 +4,12 @@ import type http from 'node:http'
|
||||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import {createHttpTerminator, type HttpTerminator} from 'http-terminator'
|
||||
import {type Registry} from 'prom-client'
|
||||
|
||||
import {type Config} from './config.js'
|
||||
import {AppContext} from './context.js'
|
||||
import i18n from './i18n.js'
|
||||
import {createPrometheusRegistry} from './prometheus.js'
|
||||
import {default as routes, errorHandler} from './routes/index.js'
|
||||
|
||||
export * from './config.js'
|
||||
@@ -16,12 +18,17 @@ export * from './logger.js'
|
||||
|
||||
export class LinkService {
|
||||
public server?: http.Server
|
||||
public metricsServer?: http.Server
|
||||
private terminator?: HttpTerminator
|
||||
private metricsTerminator?: HttpTerminator
|
||||
private metricsRegistry: Registry
|
||||
|
||||
constructor(
|
||||
public app: express.Application,
|
||||
public ctx: AppContext,
|
||||
) {}
|
||||
) {
|
||||
this.metricsRegistry = createPrometheusRegistry(ctx)
|
||||
}
|
||||
|
||||
static async create(cfg: Config): Promise<LinkService> {
|
||||
let app = express()
|
||||
@@ -41,11 +48,26 @@ export class LinkService {
|
||||
this.server.keepAliveTimeout = 90000
|
||||
this.terminator = createHttpTerminator({server: this.server})
|
||||
await events.once(this.server, 'listening')
|
||||
|
||||
const metricsApp = express()
|
||||
metricsApp.get('/metrics', (_req, res, next) => {
|
||||
res.set('Content-Type', this.metricsRegistry.contentType)
|
||||
this.metricsRegistry.metrics().then(metrics => res.end(metrics), next)
|
||||
})
|
||||
this.metricsServer = metricsApp.listen(this.ctx.cfg.service.metricsPort)
|
||||
this.metricsTerminator = createHttpTerminator({
|
||||
server: this.metricsServer,
|
||||
gracefulTerminationTimeout: 2000,
|
||||
})
|
||||
await events.once(this.metricsServer, 'listening')
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
this.ctx.abortController.abort()
|
||||
await this.terminator?.terminate()
|
||||
await Promise.all([
|
||||
this.terminator?.terminate(),
|
||||
this.metricsTerminator?.terminate(),
|
||||
])
|
||||
await this.ctx.db.close()
|
||||
this.ctx.metrics.stop()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user