@webex/internal-plugin-metrics 3.0.0-beta.37 → 3.0.0-beta.370

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/batcher.js +2 -1
  2. package/dist/batcher.js.map +1 -1
  3. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js +65 -0
  4. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js.map +1 -0
  5. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +456 -0
  6. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -0
  7. package/dist/call-diagnostic/call-diagnostic-metrics.js +838 -0
  8. package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -0
  9. package/dist/call-diagnostic/call-diagnostic-metrics.util.js +359 -0
  10. package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -0
  11. package/dist/call-diagnostic/config.js +627 -0
  12. package/dist/call-diagnostic/config.js.map +1 -0
  13. package/dist/client-metrics-batcher.js +2 -1
  14. package/dist/client-metrics-batcher.js.map +1 -1
  15. package/dist/config.js +2 -1
  16. package/dist/config.js.map +1 -1
  17. package/dist/index.js +18 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/metrics.js +23 -28
  20. package/dist/metrics.js.map +1 -1
  21. package/dist/metrics.types.js +7 -0
  22. package/dist/metrics.types.js.map +1 -0
  23. package/dist/new-metrics.js +333 -0
  24. package/dist/new-metrics.js.map +1 -0
  25. package/dist/types/batcher.d.ts +2 -0
  26. package/dist/types/call-diagnostic/call-diagnostic-metrics-batcher.d.ts +2 -0
  27. package/dist/types/call-diagnostic/call-diagnostic-metrics-latencies.d.ts +194 -0
  28. package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +418 -0
  29. package/dist/types/call-diagnostic/call-diagnostic-metrics.util.d.ts +103 -0
  30. package/dist/types/call-diagnostic/config.d.ts +178 -0
  31. package/dist/types/client-metrics-batcher.d.ts +2 -0
  32. package/dist/types/config.d.ts +36 -0
  33. package/dist/types/index.d.ts +13 -0
  34. package/dist/types/metrics.d.ts +3 -0
  35. package/dist/types/metrics.types.d.ts +104 -0
  36. package/dist/types/new-metrics.d.ts +139 -0
  37. package/dist/types/utils.d.ts +6 -0
  38. package/dist/utils.js +27 -0
  39. package/dist/utils.js.map +1 -0
  40. package/package.json +16 -8
  41. package/src/batcher.js +1 -0
  42. package/src/call-diagnostic/call-diagnostic-metrics-batcher.ts +75 -0
  43. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +416 -0
  44. package/src/call-diagnostic/call-diagnostic-metrics.ts +898 -0
  45. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +387 -0
  46. package/src/call-diagnostic/config.ts +685 -0
  47. package/src/client-metrics-batcher.js +1 -0
  48. package/src/config.js +1 -0
  49. package/src/index.ts +43 -0
  50. package/src/metrics.js +18 -24
  51. package/src/metrics.types.ts +160 -0
  52. package/src/new-metrics.ts +317 -0
  53. package/src/utils.ts +17 -0
  54. package/test/unit/spec/batcher.js +2 -0
  55. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +452 -0
  56. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +509 -0
  57. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +2174 -0
  58. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +622 -0
  59. package/test/unit/spec/client-metrics-batcher.js +2 -0
  60. package/test/unit/spec/metrics.js +66 -97
  61. package/test/unit/spec/new-metrics.ts +267 -0
  62. package/test/unit/spec/utils.ts +22 -0
  63. package/tsconfig.json +6 -0
  64. package/dist/call-diagnostic-events-batcher.js +0 -60
  65. package/dist/call-diagnostic-events-batcher.js.map +0 -1
  66. package/src/call-diagnostic-events-batcher.js +0 -62
  67. package/src/index.js +0 -17
  68. package/test/unit/spec/call-diagnostic-events-batcher.js +0 -195
package/src/index.ts ADDED
@@ -0,0 +1,43 @@
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import '@webex/internal-plugin-device';
6
+
7
+ import {registerInternalPlugin} from '@webex/webex-core';
8
+
9
+ import Metrics from './metrics';
10
+ import config from './config';
11
+ import NewMetrics from './new-metrics';
12
+ import * as Utils from './utils';
13
+ import {
14
+ ClientEvent,
15
+ ClientEventLeaveReason,
16
+ SubmitBehavioralEvent,
17
+ SubmitClientEvent,
18
+ SubmitInternalEvent,
19
+ SubmitOperationalEvent,
20
+ SubmitMQE,
21
+ } from './metrics.types';
22
+ import * as CALL_DIAGNOSTIC_CONFIG from './call-diagnostic/config';
23
+ import * as CallDiagnosticUtils from './call-diagnostic/call-diagnostic-metrics.util';
24
+
25
+ registerInternalPlugin('metrics', Metrics, {
26
+ config,
27
+ });
28
+
29
+ registerInternalPlugin('newMetrics', NewMetrics, {
30
+ config,
31
+ });
32
+
33
+ export {default, getOSNameInternal} from './metrics';
34
+ export {config, CALL_DIAGNOSTIC_CONFIG, NewMetrics, Utils, CallDiagnosticUtils};
35
+ export type {
36
+ ClientEvent,
37
+ ClientEventLeaveReason,
38
+ SubmitBehavioralEvent,
39
+ SubmitClientEvent,
40
+ SubmitInternalEvent,
41
+ SubmitMQE,
42
+ SubmitOperationalEvent,
43
+ };
package/src/metrics.js CHANGED
@@ -10,7 +10,6 @@ import {OS_NAME, OSMap, CLIENT_NAME} from './config';
10
10
 
11
11
  import Batcher from './batcher';
12
12
  import ClientMetricsBatcher from './client-metrics-batcher';
13
- import CallDiagnosticEventsBatcher from './call-diagnostic-events-batcher';
14
13
 
15
14
  const {getOSName, getOSVersion, getBrowserName, getBrowserVersion} = BrowserDetection();
16
15
 
@@ -38,7 +37,6 @@ const Metrics = WebexPlugin.extend({
38
37
  children: {
39
38
  batcher: Batcher,
40
39
  clientMetricsBatcher: ClientMetricsBatcher,
41
- callDiagnosticEventsBatcher: CallDiagnosticEventsBatcher,
42
40
  },
43
41
 
44
42
  namespace: 'Metrics',
@@ -48,13 +46,12 @@ const Metrics = WebexPlugin.extend({
48
46
  },
49
47
 
50
48
  /**
51
- * This corresponds to #sendSemiStructured() in the deprecated metrics handler
49
+ * Returns the payload for submitting client metrics.
52
50
  * @param {string} eventName
53
- * @param {Object} props
54
- * @param {string} preLoginId
55
- * @returns {Object} HttpResponse object
51
+ * @param {any} props
52
+ * @returns {any} - the payload
56
53
  */
57
- submitClientMetrics(eventName, props = {}, preLoginId) {
54
+ getClientMetricsPayload(eventName, props) {
58
55
  if (!eventName) {
59
56
  throw Error('Missing behavioral metric name. Please provide one');
60
57
  }
@@ -69,16 +66,8 @@ const Metrics = WebexPlugin.extend({
69
66
  // eslint-disable-next-line no-undef
70
67
  domain:
71
68
  typeof window !== 'undefined' ? window.location.hostname || 'non-browser' : 'non-browser', // Check what else we could measure
72
- client_id: this.webex.credentials.config.client_id,
73
- user_id: this.webex.internal.device.userId,
74
69
  };
75
70
 
76
- try {
77
- payload.tags.org_id = this.webex.credentials.getOrgId();
78
- } catch {
79
- this.logger.info('metrics: unable to get orgId');
80
- }
81
-
82
71
  payload.fields = {
83
72
  ...props.fields,
84
73
  browser_version: getBrowserVersion(),
@@ -86,6 +75,7 @@ const Metrics = WebexPlugin.extend({
86
75
  sdk_version: this.webex.version,
87
76
  platform: 'Web',
88
77
  spark_user_agent: getSparkUserAgent(this.webex),
78
+ client_id: this.webex.credentials.config.client_id,
89
79
  };
90
80
 
91
81
  payload.type = props.type || this.webex.config.metrics.type;
@@ -110,6 +100,19 @@ const Metrics = WebexPlugin.extend({
110
100
  // is impossible so unable to use Date.now()
111
101
  payload.timestamp = new Date().valueOf();
112
102
 
103
+ return payload;
104
+ },
105
+
106
+ /**
107
+ * This corresponds to #sendSemiStructured() in the deprecated metrics handler
108
+ * @param {string} eventName
109
+ * @param {Object} props
110
+ * @param {string} preLoginId
111
+ * @returns {Object} HttpResponse object
112
+ */
113
+ submitClientMetrics(eventName, props = {}, preLoginId) {
114
+ const payload = this.getClientMetricsPayload(eventName, props);
115
+
113
116
  if (preLoginId) {
114
117
  const _payload = {
115
118
  metrics: [payload],
@@ -158,15 +161,6 @@ const Metrics = WebexPlugin.extend({
158
161
  })
159
162
  );
160
163
  },
161
-
162
- submitCallDiagnosticEvents(payload) {
163
- const event = {
164
- type: 'diagnostic-event',
165
- eventPayload: payload,
166
- };
167
-
168
- return this.callDiagnosticEventsBatcher.request(event);
169
- },
170
164
  });
171
165
 
172
166
  export default Metrics;
@@ -0,0 +1,160 @@
1
+ import {
2
+ ClientEvent as RawClientEvent,
3
+ Event as RawEvent,
4
+ MediaQualityEvent as RawMediaQualityEvent,
5
+ } from '@webex/event-dictionary-ts';
6
+
7
+ export type Event = Omit<RawEvent, 'event'> & {event: RawClientEvent | RawMediaQualityEvent};
8
+
9
+ export type ClientEventError = NonNullable<RawClientEvent['errors']>[0];
10
+
11
+ export type EnvironmentType = NonNullable<RawEvent['origin']['environment']>;
12
+
13
+ export type NewEnvironmentType = NonNullable<RawEvent['origin']['newEnvironment']>;
14
+
15
+ export type ClientLaunchMethodType = NonNullable<
16
+ RawEvent['origin']['clientInfo']
17
+ >['clientLaunchMethod'];
18
+
19
+ export type SubmitClientEventOptions = {
20
+ meetingId?: string;
21
+ mediaConnections?: any[];
22
+ rawError?: any;
23
+ correlationId?: string;
24
+ preLoginId?: string;
25
+ environment?: EnvironmentType;
26
+ newEnvironmentType?: NewEnvironmentType;
27
+ clientLaunchMethod?: ClientLaunchMethodType;
28
+ webexConferenceIdStr?: string;
29
+ globalMeetingId?: string;
30
+ };
31
+
32
+ export type SubmitMQEOptions = {
33
+ meetingId: string;
34
+ mediaConnections?: any[];
35
+ networkType?: Event['origin']['networkType'];
36
+ webexConferenceIdStr?: string;
37
+ globalMeetingId?: string;
38
+ };
39
+
40
+ export type InternalEvent = {
41
+ name:
42
+ | 'internal.client.meetinginfo.request'
43
+ | 'internal.client.meetinginfo.response'
44
+ | 'internal.reset.join.latencies'
45
+ | 'internal.client.meeting.click.joinbutton'
46
+ | 'internal.host.meeting.participant.admitted'
47
+ | 'internal.client.meeting.interstitial-window.showed'
48
+ | 'internal.client.interstitial-window.click.joinbutton'
49
+ | 'internal.client.add-media.turn-discovery.start'
50
+ | 'internal.client.add-media.turn-discovery.end';
51
+
52
+ payload?: never;
53
+ options?: never;
54
+ };
55
+
56
+ export interface ClientEvent {
57
+ name: RawClientEvent['name'];
58
+ payload?: RawClientEvent;
59
+ options?: SubmitClientEventOptions;
60
+ }
61
+
62
+ export interface BehavioralEvent {
63
+ // TODO: not implemented
64
+ name: 'host.meeting.participant.admitted' | 'sdk.media-flow.started';
65
+ payload?: never;
66
+ options?: never;
67
+ }
68
+
69
+ export interface OperationalEvent {
70
+ // TODO: not implemented
71
+ name: never;
72
+ payload?: never;
73
+ options?: never;
74
+ }
75
+
76
+ export interface FeatureEvent {
77
+ // TODO: not implemented
78
+ name: never;
79
+ payload?: never;
80
+ options?: never;
81
+ }
82
+
83
+ export interface MediaQualityEvent {
84
+ name: RawMediaQualityEvent['name'];
85
+ payload?: RawMediaQualityEvent;
86
+ options: SubmitMQEOptions;
87
+ }
88
+
89
+ export type RecursivePartial<T> = {
90
+ [P in keyof T]?: T[P] extends (infer U)[]
91
+ ? RecursivePartial<U>[]
92
+ : T[P] extends object
93
+ ? RecursivePartial<T[P]>
94
+ : T[P];
95
+ };
96
+
97
+ export type MetricEventNames =
98
+ | InternalEvent['name']
99
+ | ClientEvent['name']
100
+ | BehavioralEvent['name']
101
+ | OperationalEvent['name']
102
+ | FeatureEvent['name']
103
+ | MediaQualityEvent['name'];
104
+
105
+ export type ClientInfo = NonNullable<RawEvent['origin']['clientInfo']>;
106
+ export type ClientType = NonNullable<RawEvent['origin']['clientInfo']>['clientType'];
107
+ export type SubClientType = NonNullable<RawEvent['origin']['clientInfo']>['subClientType'];
108
+ export type NetworkType = NonNullable<RawEvent['origin']>['networkType'];
109
+
110
+ export type ClientSubServiceType = ClientEvent['payload']['webexSubServiceType'];
111
+ export type ClientEventPayload = RecursivePartial<ClientEvent['payload']>;
112
+ export type ClientEventLeaveReason = ClientEvent['payload']['leaveReason'];
113
+ export type ClientEventPayloadError = ClientEvent['payload']['errors'];
114
+
115
+ export type MediaQualityEventAudioSetupDelayPayload = NonNullable<
116
+ MediaQualityEvent['payload']
117
+ >['audioSetupDelay'];
118
+ export type MediaQualityEventVideoSetupDelayPayload = NonNullable<
119
+ MediaQualityEvent['payload']
120
+ >['videoSetupDelay'];
121
+
122
+ export type SubmitMQEPayload = RecursivePartial<MediaQualityEvent['payload']> & {
123
+ intervals: NonNullable<MediaQualityEvent['payload']>['intervals'];
124
+ };
125
+
126
+ export type SubmitInternalEvent = (args: {
127
+ name: InternalEvent['name'];
128
+ payload?: RecursivePartial<InternalEvent['payload']>;
129
+ options?: any;
130
+ }) => void;
131
+
132
+ export type SubmitBehavioralEvent = (args: {
133
+ name: BehavioralEvent['name'];
134
+ payload?: RecursivePartial<BehavioralEvent['payload']>;
135
+ options?: any;
136
+ }) => void;
137
+
138
+ export type SubmitClientEvent = (args: {
139
+ name: ClientEvent['name'];
140
+ payload?: RecursivePartial<ClientEvent['payload']>;
141
+ options?: SubmitClientEventOptions;
142
+ }) => Promise<any>;
143
+
144
+ export type SubmitOperationalEvent = (args: {
145
+ name: OperationalEvent['name'];
146
+ payload?: RecursivePartial<OperationalEvent['payload']>;
147
+ options?: any;
148
+ }) => void;
149
+
150
+ export type SubmitMQE = (args: {
151
+ name: MediaQualityEvent['name'];
152
+ payload: SubmitMQEPayload;
153
+ options: any;
154
+ }) => void;
155
+
156
+ export type BuildClientEventFetchRequestOptions = (args: {
157
+ name: ClientEvent['name'];
158
+ payload?: RecursivePartial<ClientEvent['payload']>;
159
+ options?: SubmitClientEventOptions;
160
+ }) => Promise<any>;
@@ -0,0 +1,317 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ /* eslint-disable class-methods-use-this */
3
+ /* eslint-disable valid-jsdoc */
4
+
5
+ // @ts-ignore
6
+ import {WebexPlugin} from '@webex/webex-core';
7
+
8
+ import CallDiagnosticMetrics from './call-diagnostic/call-diagnostic-metrics';
9
+ import {
10
+ RecursivePartial,
11
+ ClientEvent,
12
+ FeatureEvent,
13
+ BehavioralEvent,
14
+ OperationalEvent,
15
+ MediaQualityEvent,
16
+ InternalEvent,
17
+ SubmitClientEventOptions,
18
+ } from './metrics.types';
19
+ import CallDiagnosticLatencies from './call-diagnostic/call-diagnostic-metrics-latencies';
20
+ import {setMetricTimings} from './call-diagnostic/call-diagnostic-metrics.util';
21
+ import {generateCommonErrorMetadata} from './utils';
22
+
23
+ /**
24
+ * Metrics plugin to centralize all types of metrics.
25
+ * @class
26
+ */
27
+ class Metrics extends WebexPlugin {
28
+ // eslint-disable-next-line no-use-before-define
29
+ static instance: Metrics;
30
+
31
+ // Call Diagnostic latencies
32
+ callDiagnosticLatencies: CallDiagnosticLatencies;
33
+ // Helper classes to handle the different types of metrics
34
+ callDiagnosticMetrics: CallDiagnosticMetrics;
35
+
36
+ /**
37
+ * Constructor
38
+ * @param args
39
+ * @constructor
40
+ * @private
41
+ * @returns
42
+ */
43
+ constructor(...args) {
44
+ super(...args);
45
+
46
+ this.onReady();
47
+ }
48
+
49
+ /**
50
+ * On Ready
51
+ */
52
+ private onReady() {
53
+ // @ts-ignore
54
+ this.webex.once('ready', () => {
55
+ // @ts-ignore
56
+ this.callDiagnosticMetrics = new CallDiagnosticMetrics({}, {parent: this.webex});
57
+ // @ts-ignore
58
+ this.callDiagnosticLatencies = new CallDiagnosticLatencies({}, {parent: this.webex});
59
+ });
60
+ }
61
+
62
+ /**
63
+ * Used for internal purposes only
64
+ * @param args
65
+ */
66
+ submitInternalEvent({
67
+ name,
68
+ payload,
69
+ options,
70
+ }: {
71
+ name: InternalEvent['name'];
72
+ payload?: RecursivePartial<InternalEvent['payload']>;
73
+ options?: any;
74
+ }) {
75
+ if (name === 'internal.reset.join.latencies') {
76
+ this.callDiagnosticLatencies.clearTimestamps();
77
+ } else {
78
+ this.callDiagnosticLatencies.saveTimestamp({key: name});
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Behavioral event
84
+ * @param args
85
+ */
86
+ submitBehavioralEvent({
87
+ name,
88
+ payload,
89
+ options,
90
+ }: {
91
+ name: BehavioralEvent['name'];
92
+ payload?: RecursivePartial<BehavioralEvent['payload']>;
93
+ options?: any;
94
+ }) {
95
+ this.callDiagnosticLatencies.saveTimestamp({key: name});
96
+ throw new Error('Not implemented.');
97
+ }
98
+
99
+ /**
100
+ * Operational event
101
+ * @param args
102
+ */
103
+ submitOperationalEvent({
104
+ name,
105
+ payload,
106
+ options,
107
+ }: {
108
+ name: OperationalEvent['name'];
109
+ payload?: RecursivePartial<OperationalEvent['payload']>;
110
+ options?: any;
111
+ }) {
112
+ throw new Error('Not implemented.');
113
+ }
114
+
115
+ /**
116
+ * Call Analyzer: Media Quality Event
117
+ * @param args
118
+ */
119
+ submitMQE({
120
+ name,
121
+ payload,
122
+ options,
123
+ }: {
124
+ name: MediaQualityEvent['name'];
125
+ payload: RecursivePartial<MediaQualityEvent['payload']> & {
126
+ intervals: MediaQualityEvent['payload']['intervals'];
127
+ };
128
+ options: any;
129
+ }) {
130
+ this.callDiagnosticLatencies.saveTimestamp({key: name});
131
+ this.callDiagnosticMetrics.submitMQE({name, payload, options});
132
+ }
133
+
134
+ /**
135
+ * Call Analyzer: Feature Usage Event
136
+ * @param args
137
+ */
138
+ submitFeatureEvent({
139
+ name,
140
+ payload,
141
+ options,
142
+ }: {
143
+ name: FeatureEvent['name'];
144
+ payload?: RecursivePartial<FeatureEvent['payload']>;
145
+ options: any;
146
+ }) {
147
+ throw new Error('Not implemented.');
148
+ }
149
+
150
+ /**
151
+ * Call Analyzer: Client Event
152
+ * @public
153
+ * @param args
154
+ */
155
+ public submitClientEvent({
156
+ name,
157
+ payload,
158
+ options,
159
+ }: {
160
+ name: ClientEvent['name'];
161
+ payload?: RecursivePartial<ClientEvent['payload']>;
162
+ options?: SubmitClientEventOptions;
163
+ }): Promise<any> {
164
+ if (!this.callDiagnosticLatencies || !this.callDiagnosticMetrics) {
165
+ // @ts-ignore
166
+ this.webex.logger.log(
167
+ `NewMetrics: @submitClientEvent. Attempted to submit before webex.ready. Event name: ${name}`
168
+ );
169
+
170
+ return Promise.resolve();
171
+ }
172
+ this.callDiagnosticLatencies.saveTimestamp({
173
+ key: name,
174
+ options: {meetingId: options?.meetingId},
175
+ });
176
+
177
+ return this.callDiagnosticMetrics.submitClientEvent({name, payload, options});
178
+ }
179
+
180
+ /**
181
+ * Submit a pre-login metric to clientmetrics
182
+ * @public
183
+ * @param payload
184
+ * @param preLoginId - pre-login ID of user
185
+ * @returns
186
+ */
187
+ public postPreLoginMetric(payload: any, preLoginId: string): Promise<any> {
188
+ // @ts-ignore
189
+ return this.webex
190
+ .request({
191
+ method: 'POST',
192
+ api: 'metrics',
193
+ resource: 'clientmetrics-prelogin',
194
+ headers: {
195
+ authorization: false,
196
+ 'x-prelogin-userid': preLoginId,
197
+ },
198
+ body: {
199
+ metrics: [payload],
200
+ },
201
+ })
202
+ .then((res) => {
203
+ // @ts-ignore
204
+ this.webex.logger.log(`NewMetrics: @postPreLoginMetric. Request successful.`);
205
+
206
+ return res;
207
+ })
208
+ .catch((err) => {
209
+ // @ts-ignore
210
+ this.logger.error(
211
+ `NewMetrics: @postPreLoginMetric. Request failed:`,
212
+ `err: ${generateCommonErrorMetadata(err)}`
213
+ );
214
+
215
+ return Promise.reject(err);
216
+ });
217
+ }
218
+
219
+ /**
220
+ * Issue request to alias a user's pre-login ID with their CI UUID
221
+ * @param {string} preLoginId
222
+ * @returns {Object} HttpResponse object
223
+ */
224
+ public clientMetricsAliasUser(preLoginId: string) {
225
+ // @ts-ignore
226
+ return this.webex
227
+ .request({
228
+ method: 'POST',
229
+ api: 'metrics',
230
+ resource: 'clientmetrics',
231
+ headers: {
232
+ 'x-prelogin-userid': preLoginId,
233
+ },
234
+ body: {},
235
+ qs: {
236
+ alias: true,
237
+ },
238
+ })
239
+ .then((res) => {
240
+ // @ts-ignore
241
+ this.webex.logger.log(`NewMetrics: @clientMetricsAliasUser. Request successful.`);
242
+
243
+ return res;
244
+ })
245
+ .catch((err) => {
246
+ // @ts-ignore
247
+ this.logger.error(
248
+ `NewMetrics: @clientMetricsAliasUser. Request failed:`,
249
+ `err: ${generateCommonErrorMetadata(err)}`
250
+ );
251
+
252
+ return Promise.reject(err);
253
+ });
254
+ }
255
+
256
+ /**
257
+ * Returns a promise that will resolve to fetch options for submitting a metric.
258
+ *
259
+ * This is to support quickly submitting metrics when the browser/tab is closing.
260
+ * Calling submitClientEvent will not work because there some async steps that will
261
+ * not complete before the browser is closed. Instead, we pre-gather all the
262
+ * information/options needed for the request(s), and then simply and quickly
263
+ * fire the fetch(es) when beforeUnload is triggered.
264
+ *
265
+ * We must use fetch instead of request because fetch has a keepalive option that
266
+ * allows the request it to outlive the page.
267
+ *
268
+ * Note: the timings values will be wrong, but setMetricTimingsAndFetch() will
269
+ * properly adjust them before submitting.
270
+ *
271
+ * @public
272
+ * @param {Object} arg
273
+ * @param {String} arg.name - event name
274
+ * @param {Object} arg.payload - event payload
275
+ * @param {Object} arg.options - other options
276
+ * @returns {Promise} promise that resolves to options to be used with fetch
277
+ */
278
+ public async buildClientEventFetchRequestOptions({
279
+ name,
280
+ payload,
281
+ options,
282
+ }: {
283
+ name: ClientEvent['name'];
284
+ payload?: RecursivePartial<ClientEvent['payload']>;
285
+ options?: SubmitClientEventOptions;
286
+ }): Promise<any> {
287
+ return this.callDiagnosticMetrics.buildClientEventFetchRequestOptions({
288
+ name,
289
+ payload,
290
+ options,
291
+ });
292
+ }
293
+
294
+ /**
295
+ * Submits a metric from pre-built request options via the fetch API. Updates
296
+ * the "$timings" and "originTime" values to Date.now() since the existing times
297
+ * were set when the options were built (not submitted).
298
+
299
+ * @param {any} options - the pre-built request options for submitting a metric
300
+ * @returns {Promise} promise that resolves to the response object
301
+ */
302
+ public setMetricTimingsAndFetch(options: any): Promise<any> {
303
+ // @ts-ignore
304
+ return this.webex.setTimingsAndFetch(setMetricTimings(options));
305
+ }
306
+
307
+ /**
308
+ * Returns true if the specified serviceErrorCode maps to an expected error.
309
+ * @param {number} serviceErrorCode the service error code
310
+ * @returns {boolean}
311
+ */
312
+ public isServiceErrorExpected(serviceErrorCode: number): boolean {
313
+ return this.callDiagnosticMetrics.isServiceErrorExpected(serviceErrorCode);
314
+ }
315
+ }
316
+
317
+ export default Metrics;
package/src/utils.ts ADDED
@@ -0,0 +1,17 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ /**
3
+ * Generates common metadata for errors
4
+ * @param {any} error
5
+ * @returns {object}
6
+ */
7
+ export const generateCommonErrorMetadata = (error) => {
8
+ if (error instanceof Error) {
9
+ return JSON.stringify({
10
+ message: error?.message,
11
+ name: error?.name,
12
+ stack: error?.stack,
13
+ });
14
+ }
15
+
16
+ return error;
17
+ };
@@ -37,6 +37,7 @@ describe('plugin-metrics', () => {
37
37
  return Promise.resolve({
38
38
  statusCode: 204,
39
39
  body: undefined,
40
+ waitForServiceTimeout: 30,
40
41
  options,
41
42
  });
42
43
  };
@@ -97,6 +98,7 @@ describe('plugin-metrics', () => {
97
98
  return Promise.resolve({
98
99
  statusCode: 204,
99
100
  body: undefined,
101
+ waitForServiceTimeout: 30,
100
102
  options,
101
103
  });
102
104
  });