import statsd from "../config/statsd" ; import { metricWebhooks } from "../config/metric-names" ; export const getCurrentTime = ( ) => Date . now ( ) ; param webhookReceivedTime param webhookName param contextLogger param status export const emitWebhookProcessedMetrics = ( webhookReceivedTime , webhookName , contextLogger , status ? ) : number | undefined | void => { const currentTime = getCurrentTime ( ) ; try { if ( webhookReceivedTime <= currentTime ) { const timeToProcessWebhookEvent = currentTime - webhookReceivedTime ; contextLogger . info ( { webhookName } , `Webhook processed in ${ timeToProcessWebhookEvent } milliseconds` ) ; const tags = { name : webhookName , status : status ?. toString ( ) || "none" , } ; statsd . increment ( metricWebhooks . webhookProcessed , tags ) ; statsd . histogram ( metricWebhooks . webhookProcessingTimes , timeToProcessWebhookEvent , tags ) ; const histogramBuckets = "1000_10000_30000_60000_120000_300000_600000_3000000" ; tags [ "gsd_histogram" ] = histogramBuckets ; statsd . histogram ( metricWebhooks . webhookLatency , timeToProcessWebhookEvent , tags ) ; return timeToProcessWebhookEvent ; } else { contextLogger ?. error ( { webhookReceivedTime } , `Failed to send timeToProcessWebhookEvent metric. webhookReceivedTime: ${ webhookReceivedTime }; current time: ${ currentTime }.` ) ; return undefined ; } } catch ( err ) { contextLogger ?. error ( { err } , "Failed to send webhook processing time metrics." ) ; } } ; param webhookName export const emitWebhookFailedMetrics = ( webhookName ) => { const tags = { name : webhookName , } ; statsd . increment ( metricWebhooks . webhookFailure , tags ) ; } ; param webhookName param size export const emitWebhookPayloadMetrics = ( webhookName , size ) => { const tags = { name : webhookName } ; statsd . histogram ( metricWebhooks . webhookPayloadSize , size , tags ) ; const histogramBuckets = "128000_256000_512000_1024000_2048000" ; tags [ "gsd_histogram" ] = histogramBuckets ; statsd . histogram ( metricWebhooks . webhookPayloadSize , size , tags ) ; } ;