refactor(prometheus): enable isolated declarations (#1204)

pull/1206/head
Jonathan Haines 2025-06-09 19:47:35 +10:00 committed by GitHub
parent aaf90c39e6
commit 1cdfdbd292
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 10 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/prometheus': patch
---
Add explicit return types

View File

@ -1,5 +1,6 @@
import type { Context } from 'hono'
import type { Context, MiddlewareHandler, TypedResponse } from 'hono'
import { createMiddleware } from 'hono/factory'
import type { ContentfulStatusCode } from 'hono/utils/http-status'
import type { DefaultMetricsCollectorConfiguration, RegistryContentType } from 'prom-client'
import { Registry, collectDefaultMetrics as promCollectDefaultMetrics } from 'prom-client'
import { createStandardMetrics } from './standardMetrics'
@ -22,7 +23,14 @@ const evaluateCustomLabels = (customLabels: MetricOptions['customLabels'], conte
return labels
}
export const prometheus = (options?: PrometheusOptions) => {
export const prometheus = (
options?: PrometheusOptions
): {
printMetrics: (
c: Context
) => Promise<Response & TypedResponse<string, ContentfulStatusCode, 'text'>>
registerMetrics: MiddlewareHandler
} => {
const {
registry = new Registry(),
collectDefaultMetrics = false,

View File

@ -2,15 +2,24 @@ import type { Context } from 'hono'
import { Counter, Histogram } from 'prom-client'
import type { CounterConfiguration, HistogramConfiguration, Metric, Registry } from 'prom-client'
export type MetricOptions = {
interface CounterOptions<T extends string> extends CounterConfiguration<T> {
type: 'counter'
disabled?: boolean
customLabels?: Record<string, (c: Context) => string>
} & (
| ({ type: 'counter' } & CounterConfiguration<string>)
| ({ type: 'histogram' } & HistogramConfiguration<string>)
)
}
const standardMetrics = {
interface HistogramOptions<T extends string> extends HistogramConfiguration<T> {
type: 'histogram'
disabled?: boolean
customLabels?: Record<string, (c: Context) => string>
}
export type MetricOptions<T extends string = string> = CounterOptions<T> | HistogramOptions<T>
const standardMetrics: {
requestDuration: HistogramOptions<string>
requestsTotal: CounterOptions<string>
} = {
requestDuration: {
type: 'histogram',
name: 'http_request_duration_seconds',
@ -26,7 +35,7 @@ const standardMetrics = {
help: 'Total number of HTTP requests',
labelNames: ['method', 'status', 'ok', 'route'],
},
} satisfies Record<string, MetricOptions>
}
export type MetricName = keyof typeof standardMetrics

View File

@ -4,7 +4,8 @@
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
"emitDeclarationOnly": false
"emitDeclarationOnly": false,
"isolatedDeclarations": true
},
"include": ["src/**/*.ts"],
"exclude": ["**/*.test.ts"],