TP-5555 | metrics integration

This commit is contained in:
varnit goyal
2024-12-13 19:58:30 +05:30
parent fbebb1e0cb
commit 441d0be56d
7 changed files with 664 additions and 359 deletions

1
.npmrc
View File

@@ -0,0 +1 @@
registry=http://localhost:4873/

View File

@@ -4,7 +4,8 @@
"packages/*"
],
"scripts": {
"build": "lerna run build"
"build": "lerna run build",
"local-publish" : "lerna publish --no-git-tag-version"
},
"devDependencies": {
"eslint": "^9.13.0",

View File

@@ -30,6 +30,7 @@ import {
sipRegister, sipUnmuteCall
} from "./assets/js/sip5ml.service.ts";
import registerEventProcessor from "./eventsProcessor.ts";
import MetricsProcessor from "@universal-call-sdk/common/lib/utils/metricsProcessor.ts";
class AmeyoAdapter implements IAdapter {
private callbacks: {
@@ -53,6 +54,11 @@ class AmeyoAdapter implements IAdapter {
private currentCallMetadata: GenericObject;
private sipAccountInfo: GenericObject;
private isAgentAvailable: boolean;
private isMetricsEnabled : boolean
private metricFlowName: string
private metricMetadata: GenericObject
private metricProcessor: any
private metricUrl: string
constructor(options: AmeyoInitializationOptions) {
console.log('AmeyoAdapter constructor');
@@ -94,6 +100,10 @@ class AmeyoAdapter implements IAdapter {
this.sipAccountInfo = {};
this.currentCallMetadata = {};
this.isAgentAvailable = false;
this.isMetricsEnabled = options?.isMetricEnabled || false;
this.metricFlowName = options?.metricFlowName || '';
this.metricMetadata = options?.metricMetadata || {}
this.metricUrl = options?.metricUrl || ''
window.BASE_AMEYO_URL = this.baseUrl;
window.AMEYO_LOGIN_URL = options.loginUrl
@@ -103,7 +113,11 @@ class AmeyoAdapter implements IAdapter {
console.log('initializing ameyo adapter');
window.addEventListener('message', this._registerMessageListener);
this._initializeAmeyo();
if(this.isMetricsEnabled) {
this.metricProcessor = new MetricsProcessor(this.metricUrl, this.metricFlowName);
}
this.metricProcessor.pushCounterMetric({metricName: 'initSdk', flow: "", metaData: this.metricMetadata})
}
@@ -115,15 +129,25 @@ class AmeyoAdapter implements IAdapter {
loadCredentials({accountName, userName, domain, password});
loadCallOptions();
sipRegister({domain: domainOnly, port});
this.metricProcessor.pushCounterMetric({metricName: 'sipStackInitialised',flow: 'ameyo-adapter', metaData: this.metricMetadata})
}
_initializeAmeyo = () => {
loginInAmeyo(this.userName?.toLowerCase(), this.password);
}
_onListenForCorsBypassResponse = (payload: GenericObject) => {
console.log('universal sdk', payload);
this.metricProcessor.pushCounterMetric({metricName: `ameyo-api-call-count`, metaData: this.metricMetadata, flow: payload?.data?.requestKey})
this.metricProcessor.pushHistogramMetric({metricName: `ameyo-api-latency-${payload?.data?.requestKey}`, metaData: this.metricMetadata, flow:'api-latency', value: payload?.data?.time} )
if(payload?.data?.err) {
this.metricProcessor.pushCounterMetric({metricName: `ameyo-api-err`, metaData: this.metricMetadata, flow: payload?.data?.requestKey})
}
if (payload?.data?.requestKey === RequestKeys.AMEYO_LOGIN) {
if(payload?.data?.err) {
console.log('on login failed', payload?.data?.err);

View File

@@ -1,4 +1,3 @@
export type GenericObject = {
[key: string]: any;
};
@@ -33,8 +32,12 @@ export type AmeyoInitializationOptions = {
userName: string,
password: string,
eventListenerUrl: string,
baseUrl: string
loginUrl: string
baseUrl: string,
loginUrl: string,
isMetricEnabled ?:boolean,
metricFlowName ?:string,
metricMetadata ?:GenericObject,
metricUrl ?:string
}
declare global {

View File

@@ -0,0 +1,45 @@
import GenericObject from "./GenericObject.ts";
export enum MetricType {
COUNTER = 'COUNTER',
GAUGE = 'GAUGE',
HISTOGRAM = 'HISTOGRAM'
}
export type MetricPayload = {
metricName: string;
metricType: MetricType;
value: number | string;
group: string;
subFlow?: string;
metaData?: Record<string, any>;
}
export type CounterMetricPayload = {
metricName: string;
metaData?: Record<string, any>;
flow: string;
}
export type HistogramMetricPayload = {
metricName: string;
value: number | string;
subFlow?: string;
metaData?: Record<string, any>;
flow: string;
}
export type GaugeMetricPayload = {
metricName: string;
value: number | string;
subFlow?: string;
metaData?: Record<string, any>;
flow: string;
metadata: GenericObject
}
export default MetricPayload;

View File

@@ -0,0 +1,62 @@
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;

879
yarn.lock

File diff suppressed because it is too large Load Diff