add metrics middleware to ogcard service
This commit is contained in:
@@ -2,7 +2,9 @@ import events from 'node:events'
|
||||
import type http from 'node:http'
|
||||
|
||||
import express from 'express'
|
||||
import promBundle from 'express-prom-bundle'
|
||||
import {createHttpTerminator, type HttpTerminator} from 'http-terminator'
|
||||
import {register} from 'prom-client'
|
||||
|
||||
import {type Config} from './config.js'
|
||||
import {AppContext} from './context.js'
|
||||
@@ -13,7 +15,9 @@ export * from './logger.js'
|
||||
|
||||
export class CardService {
|
||||
public server?: http.Server
|
||||
public metricsServer?: http.Server
|
||||
private terminator?: HttpTerminator
|
||||
private metricsTerminator?: HttpTerminator
|
||||
|
||||
constructor(
|
||||
public app: express.Application,
|
||||
@@ -24,6 +28,23 @@ export class CardService {
|
||||
let app = express()
|
||||
|
||||
const ctx = await AppContext.fromConfig(cfg)
|
||||
|
||||
// Add Prometheus middleware for automatic HTTP instrumentation
|
||||
const metricsMiddleware = promBundle({
|
||||
includeMethod: true,
|
||||
includePath: true,
|
||||
includeStatusCode: true,
|
||||
includeUp: true,
|
||||
promClient: {
|
||||
collectDefaultMetrics: {
|
||||
timeout: 5000,
|
||||
},
|
||||
},
|
||||
// Don't expose /metrics on main app - we'll use separate server
|
||||
autoregister: false,
|
||||
})
|
||||
app.use(metricsMiddleware)
|
||||
|
||||
app = routes(ctx, app)
|
||||
app.use(errorHandler)
|
||||
|
||||
@@ -31,14 +52,27 @@ export class CardService {
|
||||
}
|
||||
|
||||
async start() {
|
||||
// Start main application server
|
||||
this.server = this.app.listen(this.ctx.cfg.service.port)
|
||||
this.server.keepAliveTimeout = 90000
|
||||
this.terminator = createHttpTerminator({server: this.server})
|
||||
await events.once(this.server, 'listening')
|
||||
|
||||
// Start separate metrics server
|
||||
const metricsApp = express()
|
||||
metricsApp.get('/metrics', (_req, res) => {
|
||||
res.set('Content-Type', register.contentType)
|
||||
res.end(register.metrics())
|
||||
})
|
||||
|
||||
this.metricsServer = metricsApp.listen(this.ctx.cfg.service.metricsPort)
|
||||
this.metricsTerminator = createHttpTerminator({server: this.metricsServer})
|
||||
await events.once(this.metricsServer, 'listening')
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
this.ctx.abortController.abort()
|
||||
await this.terminator?.terminate()
|
||||
await this.metricsTerminator?.terminate()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user