75 lines
2.2 KiB
TypeScript
75 lines
2.2 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;
|
|
metadata: any;
|
|
metricName: string;
|
|
subFlow?: any;
|
|
value?: number | string
|
|
flow?: string
|
|
Metadata?:GenericObject
|
|
}) {
|
|
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, metaData, subFlow}: CounterMetricPayload ) {
|
|
this._sendMetricToMetricServer({
|
|
flow: flow,
|
|
metadata: metaData,
|
|
metricType: MetricType.COUNTER,
|
|
subFlow:subFlow,
|
|
metricName,
|
|
|
|
})
|
|
}
|
|
|
|
pushHistogramMetric({metricName, value, flow, metaData, subFlow}: HistogramMetricPayload) {
|
|
this._sendMetricToMetricServer({
|
|
flow: flow,
|
|
metadata: metaData,
|
|
metricType: MetricType.HISTOGRAM,
|
|
subFlow: subFlow,
|
|
metricName,
|
|
value,
|
|
|
|
})
|
|
}
|
|
|
|
pushGaugeMetric({metricName, value, flow, metaData, subFlow}: GaugeMetricPayload) {
|
|
this._sendMetricToMetricServer({
|
|
flow: flow,
|
|
metadata: metaData,
|
|
metricType: MetricType.GAUGE,
|
|
subFlow: subFlow,
|
|
metricName,
|
|
value,
|
|
})
|
|
}
|
|
}
|
|
|
|
export default MetricsProcessor;
|