refactor(prometheus): enable isolated declarations (#1204)
parent
aaf90c39e6
commit
1cdfdbd292
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/prometheus': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add explicit return types
|
|
@ -1,5 +1,6 @@
|
||||||
import type { Context } from 'hono'
|
import type { Context, MiddlewareHandler, TypedResponse } from 'hono'
|
||||||
import { createMiddleware } from 'hono/factory'
|
import { createMiddleware } from 'hono/factory'
|
||||||
|
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
||||||
import type { DefaultMetricsCollectorConfiguration, RegistryContentType } from 'prom-client'
|
import type { DefaultMetricsCollectorConfiguration, RegistryContentType } from 'prom-client'
|
||||||
import { Registry, collectDefaultMetrics as promCollectDefaultMetrics } from 'prom-client'
|
import { Registry, collectDefaultMetrics as promCollectDefaultMetrics } from 'prom-client'
|
||||||
import { createStandardMetrics } from './standardMetrics'
|
import { createStandardMetrics } from './standardMetrics'
|
||||||
|
@ -22,7 +23,14 @@ const evaluateCustomLabels = (customLabels: MetricOptions['customLabels'], conte
|
||||||
return labels
|
return labels
|
||||||
}
|
}
|
||||||
|
|
||||||
export const prometheus = (options?: PrometheusOptions) => {
|
export const prometheus = (
|
||||||
|
options?: PrometheusOptions
|
||||||
|
): {
|
||||||
|
printMetrics: (
|
||||||
|
c: Context
|
||||||
|
) => Promise<Response & TypedResponse<string, ContentfulStatusCode, 'text'>>
|
||||||
|
registerMetrics: MiddlewareHandler
|
||||||
|
} => {
|
||||||
const {
|
const {
|
||||||
registry = new Registry(),
|
registry = new Registry(),
|
||||||
collectDefaultMetrics = false,
|
collectDefaultMetrics = false,
|
||||||
|
|
|
@ -2,15 +2,24 @@ import type { Context } from 'hono'
|
||||||
import { Counter, Histogram } from 'prom-client'
|
import { Counter, Histogram } from 'prom-client'
|
||||||
import type { CounterConfiguration, HistogramConfiguration, Metric, Registry } 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
|
disabled?: boolean
|
||||||
customLabels?: Record<string, (c: Context) => string>
|
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: {
|
requestDuration: {
|
||||||
type: 'histogram',
|
type: 'histogram',
|
||||||
name: 'http_request_duration_seconds',
|
name: 'http_request_duration_seconds',
|
||||||
|
@ -26,7 +35,7 @@ const standardMetrics = {
|
||||||
help: 'Total number of HTTP requests',
|
help: 'Total number of HTTP requests',
|
||||||
labelNames: ['method', 'status', 'ok', 'route'],
|
labelNames: ['method', 'status', 'ok', 'route'],
|
||||||
},
|
},
|
||||||
} satisfies Record<string, MetricOptions>
|
}
|
||||||
|
|
||||||
export type MetricName = keyof typeof standardMetrics
|
export type MetricName = keyof typeof standardMetrics
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
|
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
|
||||||
"emitDeclarationOnly": false
|
"emitDeclarationOnly": false,
|
||||||
|
"isolatedDeclarations": true
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts"],
|
"include": ["src/**/*.ts"],
|
||||||
"exclude": ["**/*.test.ts"],
|
"exclude": ["**/*.test.ts"],
|
||||||
|
|
Loading…
Reference in New Issue