Files
call-sdk/packages/common/lib/utils/metricsProcessor.ts
2024-12-17 13:22:40 +05:30

70 lines
2.0 KiB
TypeScript

import {CounterMetricPayload, GaugeMetricPayload, HistogramMetricPayload, MetricType} from "../types/MetricPayload.ts";
import GenericObject from "../types/GenericObject.ts";
class MetricsProcessor {
private metricUrl : string
private portal : string
private isMetricEnabled : boolean
private metricTransporter: ((payload: GenericObject)=>void) | undefined
constructor(metricUrl: string, portal: string, metricTransporter: ((payload: GenericObject)=>void) | undefined) {
this.metricUrl = metricUrl;
this.portal = portal;
this.isMetricEnabled = true
this.metricTransporter = metricTransporter;
}
_sendMetricToMetricServer(payload: {
metricType: MetricType;
metricName: string;
subFlow?: any;
value?: number | string
flow?: string
}) {
if(!this.isMetricEnabled) {
return;
}
const finalPayload = {group: `${this.portal}`,...payload}
if(this.metricTransporter){
this.metricTransporter(finalPayload)
return;
}
fetch(this.metricUrl, {
method: 'POST',
body: JSON.stringify(finalPayload)
})
}
pushCounterMetric({metricName, flow, subFlow}: CounterMetricPayload ) {
this._sendMetricToMetricServer({
flow: flow,
metricType: MetricType.COUNTER,
subFlow:subFlow,
metricName,
})
}
pushHistogramMetric({metricName, value, flow, subFlow}: HistogramMetricPayload) {
this._sendMetricToMetricServer({
flow: flow,
metricType: MetricType.HISTOGRAM,
subFlow: subFlow,
metricName,
value,
})
}
pushGaugeMetric({metricName, value, flow, subFlow}: GaugeMetricPayload) {
this._sendMetricToMetricServer({
flow: flow,
metricType: MetricType.GAUGE,
subFlow: subFlow,
metricName,
value,
})
}
}
export default MetricsProcessor;