Skip to content

Commit 6bc7366

Browse files
committed
Update implement monitoring ClickHouse query durations
1 parent 4dd71b9 commit 6bc7366

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

bun.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rabbitforexapi",
3-
"version": "5.1.0",
3+
"version": "5.2.0",
44
"module": "src/index.ts",
55
"type": "module",
66
"private": true,
@@ -14,7 +14,7 @@
1414
"typescript": "^5"
1515
},
1616
"dependencies": {
17-
"@clickhouse/client": "^1.15.0",
17+
"@clickhouse/client": "^1.16.0",
1818
"@rabbit-company/openmetrics-client": "^2.0.1",
1919
"@rabbit-company/web": "^0.16.0",
2020
"@rabbit-company/web-middleware": "^0.16.0"

src/clickhouse.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createClient, type ClickHouseClient, type ClickHouseSettings } from "@clickhouse/client";
22
import { Logger } from "./logger";
33
import type { AssetType } from "./types";
4+
import { clickhouseResponseDuration } from "./metrics";
45

56
export interface ClickHouseConfig {
67
host: string;
@@ -163,6 +164,7 @@ export class ClickHouseWrapper {
163164
};
164165

165166
try {
167+
const start = process.hrtime.bigint();
166168
await this.client.insert({
167169
table: "prices_raw",
168170
values: prices.map((p) => ({
@@ -173,6 +175,8 @@ export class ClickHouseWrapper {
173175
})),
174176
format: "JSONEachRow",
175177
});
178+
const end = process.hrtime.bigint();
179+
clickhouseResponseDuration.labels({ method: "insertPrices", type: "batch" }).observe(Number(end - start) / 1_000_000_000);
176180

177181
Logger.debug(`[ClickHouse] Inserted ${prices.length} price records`);
178182
} catch (error: any) {
@@ -206,10 +210,13 @@ export class ClickHouseWrapper {
206210
ORDER BY timestamp ASC
207211
`;
208212

213+
const start = process.hrtime.bigint();
209214
const result = await this.client.query({
210215
query,
211216
format: "JSONEachRow",
212217
});
218+
const end = process.hrtime.bigint();
219+
clickhouseResponseDuration.labels({ method: "getAllRawPrices", type: assetType }).observe(Number(end - start) / 1_000_000_000);
213220

214221
return result.json();
215222
}
@@ -283,10 +290,13 @@ export class ClickHouseWrapper {
283290
ORDER BY hour ASC
284291
`;
285292

293+
const start = process.hrtime.bigint();
286294
const result = await this.client.query({
287295
query,
288296
format: "JSONEachRow",
289297
});
298+
const end = process.hrtime.bigint();
299+
clickhouseResponseDuration.labels({ method: "getAllHourlyPrices", type: assetType }).observe(Number(end - start) / 1_000_000_000);
290300

291301
return result.json();
292302
}
@@ -391,10 +401,13 @@ export class ClickHouseWrapper {
391401
ORDER BY date ASC
392402
`;
393403

404+
const start = process.hrtime.bigint();
394405
const result = await this.client.query({
395406
query,
396407
format: "JSONEachRow",
397408
});
409+
const end = process.hrtime.bigint();
410+
clickhouseResponseDuration.labels({ method: "getAllDailyPrices", type: assetType }).observe(Number(end - start) / 1_000_000_000);
398411

399412
return result.json();
400413
}

src/metrics.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Counter, Registry } from "@rabbit-company/openmetrics-client";
1+
import { Counter, Histogram, Registry } from "@rabbit-company/openmetrics-client";
22

33
export const registry = new Registry({
44
prefix: "rabbitforex",
@@ -10,3 +10,12 @@ export const httpRequests = new Counter({
1010
labelNames: ["endpoint"],
1111
registry,
1212
});
13+
14+
export const clickhouseResponseDuration = new Histogram({
15+
name: "clickhouse_response_duration_seconds",
16+
help: "ClickHouse response duration in seconds",
17+
unit: "seconds",
18+
labelNames: ["method", "type"],
19+
buckets: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
20+
registry,
21+
});

0 commit comments

Comments
 (0)