@@ -8,6 +8,7 @@ import { Registry, Counter, Histogram, metric } from 'prom-client';
88import { MethodKind } from '@bufbuild/protobuf' ;
99import { StreamResponse , UnaryResponse , Code , connectErrorFromReason , Interceptor , StreamRequest , UnaryRequest } from '@bufbuild/connect' ;
1010import { ILogService } from './services/logService' ;
11+ import { addCounter , addHistogram } from './common/metrics' ;
1112
1213export type GrpcMethodType = 'unary' | 'client_stream' | 'server_stream' | 'bidi_stream' ;
1314
@@ -255,16 +256,12 @@ export function getGrpcMetricsInterceptor(): grpc.Interceptor {
255256export class MetricsReporter {
256257 private static readonly REPORT_INTERVAL = 60000 ;
257258
258- private metricsHost : string ;
259259 private intervalHandler : NodeJS . Timer | undefined ;
260260
261261 constructor (
262- gitpodHost : string ,
262+ private readonly gitpodHost : string ,
263263 private readonly logger : ILogService
264- ) {
265- const serviceUrl = new URL ( gitpodHost ) ;
266- this . metricsHost = `ide.${ serviceUrl . hostname } ` ;
267- }
264+ ) { }
268265
269266 startReporting ( ) {
270267 if ( this . intervalHandler ) {
@@ -295,7 +292,7 @@ export class MetricsReporter {
295292 const counterMetric = metric as metric & { values : [ { value : number ; labels : Record < string , string > } ] } ;
296293 for ( const { value, labels } of counterMetric . values ) {
297294 if ( value > 0 ) {
298- await this . doAddCounter ( counterMetric . name , labels , value ) ;
295+ await addCounter ( this . gitpodHost , counterMetric . name , labels , value , this . logger ) ;
299296 }
300297 }
301298 }
@@ -316,64 +313,14 @@ export class MetricsReporter {
316313 sum = value ;
317314 } else if ( metricName . endsWith ( '_count' ) ) {
318315 if ( value > 0 ) {
319- await this . doAddHistogram ( histogramMetric . name , labels , value , sum , buckets ) ;
316+ await addHistogram ( this . gitpodHost , histogramMetric . name , labels , value , sum , buckets , this . logger ) ;
320317 }
321318 sum = 0 ;
322319 buckets = [ ] ;
323320 }
324321 }
325322 }
326323
327- private async doAddCounter ( name : string , labels : Record < string , string > , value : number ) {
328- const data = {
329- name,
330- labels,
331- value,
332- } ;
333-
334- const resp = await fetch (
335- `https://${ this . metricsHost } /metrics-api/metrics/counter/add/${ name } ` ,
336- {
337- method : 'POST' ,
338- headers : {
339- 'Content-Type' : 'application/json' ,
340- 'X-Client' : 'vscode-desktop-extension'
341- } ,
342- body : JSON . stringify ( data )
343- }
344- ) ;
345-
346- if ( ! resp . ok ) {
347- throw new Error ( `Metrics endpoint responded with ${ resp . status } ${ resp . statusText } ` ) ;
348- }
349- }
350-
351- private async doAddHistogram ( name : string , labels : Record < string , string > , count : number , sum : number , buckets : number [ ] ) {
352- const data = {
353- name,
354- labels,
355- count,
356- sum,
357- buckets,
358- } ;
359-
360- const resp = await fetch (
361- `https://${ this . metricsHost } /metrics-api/metrics/histogram/add/${ name } ` ,
362- {
363- method : 'POST' ,
364- headers : {
365- 'Content-Type' : 'application/json' ,
366- 'X-Client' : 'vscode-desktop-extension'
367- } ,
368- body : JSON . stringify ( data )
369- }
370- ) ;
371-
372- if ( ! resp . ok ) {
373- throw new Error ( `Metrics endpoint responded with ${ resp . status } ${ resp . statusText } ` ) ;
374- }
375- }
376-
377324 stopReporting ( ) {
378325 if ( this . intervalHandler ) {
379326 clearInterval ( this . intervalHandler ) ;
0 commit comments