Files
call-sdk/packages/common/lib/utils/metricsProcessor.ts
2024-12-13 19:58:30 +05:30

63 lines
1.7 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
constructor(metricUrl: string, portal: string) {
this.metricUrl = metricUrl;
this.portal = portal;
this.isMetricEnabled = true
}
_sendMetricToMetricServer(payload: {
metricType: MetricType;
metadata: any;
metricName: string;
subflow?: any;
value?: number | string
flow?: string
Metadata?:GenericObject
}) {
if(!this.isMetricEnabled) {
return;
}
fetch(this.metricUrl, {
method: 'POST',
body: JSON.stringify({group: `universal-call-sdk-${this.portal}`,...payload})
})
}
pushCounterMetric({metricName, flow, metaData}: CounterMetricPayload ) {
this._sendMetricToMetricServer({
flow: flow,
metadata: metaData,
metricType: MetricType.COUNTER,
metricName,
})
}
pushHistogramMetric({metricName, value, flow, metaData}: HistogramMetricPayload) {
this._sendMetricToMetricServer({
flow: flow,
metadata: metaData,
metricType: MetricType.HISTOGRAM,
metricName,
value
})
}
pushGaugeMetric({metricName, value, flow, metaData}: GaugeMetricPayload) {
this._sendMetricToMetricServer({
flow: flow,
metadata: metaData,
metricType: MetricType.GAUGE,
metricName,
value,
})
}
}
export default MetricsProcessor;