import { ITrace } from "@fluidframework/protocol-definitions" ; import * as telegraf from "telegrafjs" ; import { DefaultMetricClient , IMetricClient } from "@fluidframework/server-services-core" ; class TelegrafClient implements IMetricClient { private readonly telegrafClient : any ; private connected : boolean = false ; constructor ( config ) { this . telegrafClient = new telegraf . TelegrafTCPClient ( { host : config . host , port : config . port } ) ; this . telegrafClient . connect ( ) . then ( ( ) => { this . connected = true ; } ) ; } public writeLatencyMetric ( series , traces ) { if ( ! this . connected || ! traces || traces . length === 0 ) { return Promise . resolve ( ) ; } else { return this . writeToTelegraf ( series , this . createTelegrafRow ( traces ) ) ; } } private createTelegrafRow ( traces ) { const row = new Object ( ) ; const Float = telegraf . Float ; for ( const trace of traces ) { const column = `${ trace . service }${ trace . action ? `-${ trace . action }` : "" }` ; row [ column ] = new Float ( trace . timestamp ) ; } return row ; } private writeToTelegraf ( series , row ) { const Measurement = telegraf . Measurement ; return this . telegrafClient . sendMeasurement ( new Measurement ( series , { } , row ) ) ; } } export function createMetricClient ( config ) { return ( config !== undefined && config . client === "telegraf" ) ? new TelegrafClient ( config . telegraf ) : new DefaultMetricClient ( ) ; }