Files
speckle-server/packages/server/observability/components/httpServer/httpServerMonitoring.ts
T
Kristaps Fabians Geikins 4b06f42db7 chore(server): run TS files directly (no compilation) (#5134)
* sort of works

* type fixes

* added option to run old way too
2025-07-23 11:20:40 +02:00

34 lines
1.0 KiB
TypeScript

/* istanbul ignore file */
import type { Nullable } from '@speckle/shared'
import type { Registry } from 'prom-client'
import { Gauge } from 'prom-client'
import type { Server } from 'http'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let metricActiveConnections: Nullable<Gauge<any>> = null
export const monitorActiveConnections = (params: {
httpServer: Server
registers: Registry[]
}) => {
const { httpServer, registers } = params
if (metricActiveConnections !== null) {
registers.forEach((r) => r.removeSingleMetric('speckle_server_active_connections'))
}
metricActiveConnections = new Gauge({
name: 'speckle_server_active_connections',
help: 'Number of active http connections',
async collect() {
let connectionCount = await new Promise<number>((resolve) => {
httpServer.getConnections(function (error, count) {
if (error) resolve(-1)
else resolve(count)
})
})
if (isNaN(connectionCount)) connectionCount = -1
this.set(connectionCount)
}
})
}