-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from cellajs/development
Development
- Loading branch information
Showing
50 changed files
with
680 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { MetricOptions } from '#/middlewares/observatory/types'; | ||
|
||
// Define the metrics configuration | ||
export const metricsConfig = { | ||
requestDuration: { | ||
type: 'histogram', | ||
name: 'Request_duration', | ||
help: 'Duration of HTTP requests in seconds', | ||
labelNames: ['method', 'status', 'ok', 'route'], | ||
buckets: [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10], | ||
}, | ||
requestsTotal: { | ||
type: 'counter', | ||
name: 'Requests_count', | ||
help: 'Total number and date of requests', | ||
labelNames: ['requestsNumber', 'date'], | ||
}, | ||
} satisfies Record<string, MetricOptions>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { MiddlewareHandler } from 'hono/types'; | ||
import { Counter, Histogram } from 'prom-client'; | ||
import { metricsConfig } from '#/middlewares/observatory/config'; | ||
|
||
// Prometheus metrics Initialize | ||
const observatoryRequestDurationHistogram = new Histogram({ | ||
name: metricsConfig.requestDuration.name, | ||
help: metricsConfig.requestDuration.help, | ||
labelNames: metricsConfig.requestDuration.labelNames, | ||
buckets: metricsConfig.requestDuration.buckets, | ||
}); | ||
|
||
const observatoryRequestsCounter = new Counter({ | ||
name: metricsConfig.requestsTotal.name, | ||
help: metricsConfig.requestsTotal.help, | ||
labelNames: metricsConfig.requestsTotal.labelNames, | ||
}); | ||
|
||
export const observatoryMiddleware: MiddlewareHandler = async (ctx, next) => { | ||
const start = Date.now(); | ||
|
||
// Incrementing request count | ||
const currentCounter = await observatoryRequestsCounter.get(); | ||
observatoryRequestsCounter.inc({ requestsNumber: currentCounter.values.length + 1, date: new Date().getTime() }); | ||
|
||
// Measure request duration and record it in the histogram | ||
const duration = (Date.now() - start) / 1000; // Convert milliseconds to seconds | ||
observatoryRequestDurationHistogram.observe( | ||
{ | ||
method: ctx.req.method, | ||
status: ctx.res.status.toString(), | ||
ok: ctx.res.status, | ||
route: ctx.req.url, | ||
}, | ||
duration, | ||
); | ||
|
||
await next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Context } from 'hono'; | ||
import type { CounterConfiguration, HistogramConfiguration } from 'prom-client'; | ||
|
||
export type MetricOptions = { | ||
disabled?: boolean; | ||
customLabels?: Record<string, (c: Context) => string>; | ||
} & (({ type: 'counter' } & CounterConfiguration<string>) | ({ type: 'histogram' } & HistogramConfiguration<string>)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.