From caa991ee4bcfb9f46d3d71da0de7b3590b771e0a Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 4 Oct 2022 10:06:53 +0200 Subject: [PATCH] Measure the write time of ClickHouse (#439) --- .../services/usage-ingestor/src/metrics.ts | 6 +++++ .../services/usage-ingestor/src/writer.ts | 22 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/services/usage-ingestor/src/metrics.ts b/packages/services/usage-ingestor/src/metrics.ts index 7a9ef791b..f4d84623b 100644 --- a/packages/services/usage-ingestor/src/metrics.ts +++ b/packages/services/usage-ingestor/src/metrics.ts @@ -20,6 +20,12 @@ export const processTime = new metrics.Summary({ help: 'Time spent processing and writing reports', }); +export const writeTime = new metrics.Summary({ + name: 'usage_ingestor_write_time', + help: 'Time spent writing reports', + labelNames: ['query', 'destination', 'status'], +}); + export const errors = new metrics.Counter({ name: 'usage_ingestor_errors', help: 'Number of errors occurred during processing and writing reports', diff --git a/packages/services/usage-ingestor/src/writer.ts b/packages/services/usage-ingestor/src/writer.ts index 0c511dd8c..d42dfb958 100644 --- a/packages/services/usage-ingestor/src/writer.ts +++ b/packages/services/usage-ingestor/src/writer.ts @@ -1,5 +1,5 @@ import * as Sentry from '@sentry/node'; -import { got } from 'got'; +import { got, Response as GotResponse } from 'got'; import Agent from 'agentkeepalive'; import type { FastifyLoggerInstance } from '@hive/service-common'; import { compress } from '@hive/usage-common'; @@ -10,6 +10,13 @@ import { legacyRegistryOrder, joinIntoSingleMessage, } from './serializer'; +import { writeTime } from './metrics'; + +function hasResponse(error: unknown): error is { + response: GotResponse; +} { + return error instanceof Error && 'response' in error; +} export interface ClickHouseConfig { protocol: string; @@ -131,6 +138,10 @@ async function writeCsv( logger: FastifyLoggerInstance, maxRetry: number ) { + const stopTimer = writeTime.startTimer({ + query, + destination: config.host, + }); return got .post(`${config.protocol ?? 'https'}://${config.host}:${config.port}`, { body, @@ -168,7 +179,16 @@ async function writeCsv( https: agents.https, }, }) + .then(response => { + stopTimer({ + status: response.statusCode, + }); + return response; + }) .catch(error => { + stopTimer({ + status: hasResponse(error) && error.response.statusCode ? error.response.statusCode : 'unknown', + }); Sentry.captureException(error, { level: 'error', tags: {