-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
/metrics
endpoint for exposing API metrics (#24)
* Add `/metrics` endpoint for exposing API metrics Implemented five metrics: - server_errors: 500 errors - validation_errors: 422 errors from failed Zod validations - successful_queries: 200 responses - failed_queries: all queries resulting in error (4xx, 5xx) - rows_received: number of rows returned by the DB * Remove `cursor.lock` * Remove metrics response schema comment * Update metrics for better tracking Remove `failed_queries` and add `notfound_errors`, `total_queries`. * Add test for `total_queries`
- Loading branch information
Showing
10 changed files
with
195 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,7 @@ dist | |
|
||
# Local clickhouse DB | ||
clickhouse/ | ||
cursor.lock | ||
|
||
# CLI | ||
substreams-clock-api |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// From https://github.com/pinax-network/substreams-sink-websockets/blob/main/src/prometheus.ts | ||
import client, { Counter, CounterConfiguration, Gauge, GaugeConfiguration } from 'prom-client'; | ||
|
||
export const registry = new client.Registry(); | ||
|
||
// Metrics | ||
export function registerCounter(name: string, help = "help", labelNames: string[] = [], config?: CounterConfiguration<string>) { | ||
try { | ||
registry.registerMetric(new Counter({ name, help, labelNames, ...config })); | ||
return registry.getSingleMetric(name) as Counter; | ||
} catch (e) { | ||
console.error({name, e}); | ||
throw new Error(`${e}`); | ||
} | ||
} | ||
|
||
export function registerGauge(name: string, help = "help", labelNames: string[] = [], config?: GaugeConfiguration<string>) { | ||
try { | ||
registry.registerMetric(new Gauge({ name, help, labelNames, ...config })); | ||
return registry.getSingleMetric(name) as Gauge; | ||
} catch (e) { | ||
console.error({name, e}); | ||
throw new Error(`${e}`); | ||
} | ||
} | ||
|
||
export async function getSingleMetric(name: string) { | ||
const metric = registry.getSingleMetric(name); | ||
const get = await metric?.get(); | ||
return get?.values[0].value; | ||
} | ||
|
||
// REST API metrics | ||
export const api_server_errors = registerCounter('server_errors', 'Total of server errors', ['path']); | ||
export const api_validation_errors = registerCounter('validation_errors', 'Total of query parameters validation errors', ['path']); | ||
export const api_notfound_errors = registerCounter('notfound_errors', 'Total of not found errors', ['path']); | ||
export const api_successful_queries = registerCounter('successful_queries', 'Total of successful queries', ['path']); | ||
export const api_total_queries = registerCounter('total_queries', 'Total of queries'); | ||
export const api_rows_received = registerCounter('rows_received', 'Total of rows received from Clickhouse DB'); | ||
|
||
// Gauge example | ||
// export const connection_active = registerGauge('connection_active', 'Total WebSocket active connections'); |
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.