@webex/internal-plugin-metrics 3.0.0-beta.3 → 3.0.0-beta.31

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.
package/src/batcher.js CHANGED
@@ -23,11 +23,13 @@ const MetricsBatcher = Batcher.extend({
23
23
  },
24
24
 
25
25
  prepareRequest(queue) {
26
- return Promise.resolve(queue.map((item) => {
27
- item.postTime = item.postTime || Date.now();
26
+ return Promise.resolve(
27
+ queue.map((item) => {
28
+ item.postTime = item.postTime || Date.now();
28
29
 
29
- return item;
30
- }));
30
+ return item;
31
+ })
32
+ );
31
33
  },
32
34
 
33
35
  submitHttpRequest(payload) {
@@ -36,8 +38,8 @@ const MetricsBatcher = Batcher.extend({
36
38
  service: 'metrics',
37
39
  resource: 'metrics',
38
40
  body: {
39
- metrics: payload
40
- }
41
+ metrics: payload,
42
+ },
41
43
  });
42
44
  },
43
45
 
@@ -47,38 +49,43 @@ const MetricsBatcher = Batcher.extend({
47
49
 
48
50
  handleHttpError(reason) {
49
51
  if (reason instanceof WebexHttpError.NetworkOrCORSError) {
50
- this.logger.warn('metrics-batcher: received network error submitting metrics, reenqueuing payload');
51
-
52
- return Promise.all(reason.options.body.metrics.map((item) => new Promise((resolve) => {
53
- const delay = item[sym].nextDelay;
54
-
55
- if (delay < this.config.batcherRetryPlateau) {
56
- item[sym].nextDelay *= 2;
57
- }
58
- safeSetTimeout(() => {
59
- resolve(this.rerequest(item));
60
- }, delay);
61
- })));
52
+ this.logger.warn(
53
+ 'metrics-batcher: received network error submitting metrics, reenqueuing payload'
54
+ );
55
+
56
+ return Promise.all(
57
+ reason.options.body.metrics.map(
58
+ (item) =>
59
+ new Promise((resolve) => {
60
+ const delay = item[sym].nextDelay;
61
+
62
+ if (delay < this.config.batcherRetryPlateau) {
63
+ item[sym].nextDelay *= 2;
64
+ }
65
+ safeSetTimeout(() => {
66
+ resolve(this.rerequest(item));
67
+ }, delay);
68
+ })
69
+ )
70
+ );
62
71
  }
63
72
 
64
73
  return Reflect.apply(Batcher.prototype.handleHttpError, this, [reason]);
65
74
  },
66
75
 
67
76
  rerequest(item) {
68
- return Promise.all([
69
- this.getDeferredForRequest(item),
70
- this.prepareItem(item)
71
- ])
72
- .then(([defer, req]) => {
77
+ return Promise.all([this.getDeferredForRequest(item), this.prepareItem(item)]).then(
78
+ ([defer, req]) => {
73
79
  this.enqueue(req)
74
80
  .then(() => this.bounce())
75
81
  .catch((reason) => defer.reject(reason));
76
- });
82
+ }
83
+ );
77
84
  },
78
85
 
79
86
  fingerprintRequest(item) {
80
87
  item[sym] = item[sym] || {
81
- nextDelay: 1000
88
+ nextDelay: 1000,
82
89
  };
83
90
 
84
91
  return Promise.resolve(item[sym]);
@@ -86,7 +93,7 @@ const MetricsBatcher = Batcher.extend({
86
93
 
87
94
  fingerprintResponse(item) {
88
95
  return Promise.resolve(item[sym]);
89
- }
96
+ },
90
97
  });
91
98
 
92
99
  export default MetricsBatcher;
@@ -10,7 +10,7 @@ const CallDiagnosticEventsBatcher = Batcher.extend({
10
10
  /**
11
11
  * @param {string} webClientDomain
12
12
  * @returns {string}
13
- */
13
+ */
14
14
  getBuildType(webClientDomain) {
15
15
  if (
16
16
  webClientDomain?.includes('teams.webex.com') ||
@@ -29,7 +29,7 @@ const CallDiagnosticEventsBatcher = Batcher.extend({
29
29
  // Browsers cannot provide such information right now. However, it is a required field.
30
30
  const origin = {
31
31
  buildType: this.getBuildType(item.event?.eventData?.webClientDomain),
32
- networkType: 'unknown'
32
+ networkType: 'unknown',
33
33
  };
34
34
 
35
35
  item.eventPayload.origin = Object.assign(origin, item.eventPayload.origin);
@@ -53,10 +53,10 @@ const CallDiagnosticEventsBatcher = Batcher.extend({
53
53
  service: 'metrics',
54
54
  resource: 'clientmetrics',
55
55
  body: {
56
- metrics: payload
57
- }
56
+ metrics: payload,
57
+ },
58
58
  });
59
- }
59
+ },
60
60
  });
61
61
 
62
62
  export default CallDiagnosticEventsBatcher;
@@ -22,11 +22,10 @@ const ClientMetricsBatcher = Batcher.extend({
22
22
  service: 'metrics',
23
23
  resource: 'clientmetrics',
24
24
  body: {
25
- metrics: payload
26
- }
25
+ metrics: payload,
26
+ },
27
27
  });
28
- }
29
-
28
+ },
30
29
  });
31
30
 
32
31
  export default ClientMetricsBatcher;
package/src/config.js CHANGED
@@ -8,15 +8,16 @@ export const CLIENT_NAME = 'webex-js-sdk';
8
8
  export default {
9
9
  device: {
10
10
  preDiscoveryServices: {
11
- metricsServiceUrl: process.env.METRICS_SERVICE_URL || 'https://metrics-a.wbx2.com/metrics/api/v1',
12
- metrics: process.env.METRICS_SERVICE_URL || 'https://metrics-a.wbx2.com/metrics/api/v1'
13
- }
11
+ metricsServiceUrl:
12
+ process.env.METRICS_SERVICE_URL || 'https://metrics-a.wbx2.com/metrics/api/v1',
13
+ metrics: process.env.METRICS_SERVICE_URL || 'https://metrics-a.wbx2.com/metrics/api/v1',
14
+ },
14
15
  },
15
16
  metrics: {
16
17
  appType: inBrowser ? 'browser' : 'nodejs',
17
18
  batcherWait: 500,
18
19
  batcherMaxCalls: 50,
19
20
  batcherMaxWait: 1500,
20
- batcherRetryPlateau: 32000
21
- }
21
+ batcherRetryPlateau: 32000,
22
+ },
22
23
  };
package/src/index.js CHANGED
@@ -10,7 +10,7 @@ import Metrics from './metrics';
10
10
  import config from './config';
11
11
 
12
12
  registerInternalPlugin('metrics', Metrics, {
13
- config
13
+ config,
14
14
  });
15
15
 
16
16
  export {default} from './metrics';
package/src/metrics.js CHANGED
@@ -1,3 +1,5 @@
1
+ /* eslint-disable default-param-last */
2
+
1
3
  /*!
2
4
  * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
5
  */
@@ -10,19 +12,10 @@ import Batcher from './batcher';
10
12
  import ClientMetricsBatcher from './client-metrics-batcher';
11
13
  import CallDiagnosticEventsBatcher from './call-diagnostic-events-batcher';
12
14
 
13
- const {
14
- getOSName,
15
- getOSVersion,
16
- getBrowserName,
17
- getBrowserVersion
18
- } = BrowserDetection();
15
+ const {getOSName, getOSVersion, getBrowserName, getBrowserVersion} = BrowserDetection();
19
16
 
20
17
  function getSparkUserAgent(webex) {
21
- const {
22
- appName,
23
- appVersion,
24
- appPlatform
25
- } = webex?.config ?? {};
18
+ const {appName, appVersion, appPlatform} = webex?.config ?? {};
26
19
 
27
20
  let sparkUserAgent = CLIENT_NAME;
28
21
 
@@ -37,21 +30,19 @@ function getSparkUserAgent(webex) {
37
30
  return sparkUserAgent;
38
31
  }
39
32
 
40
-
41
33
  const Metrics = WebexPlugin.extend({
42
34
  children: {
43
35
  batcher: Batcher,
44
36
  clientMetricsBatcher: ClientMetricsBatcher,
45
- callDiagnosticEventsBatcher: CallDiagnosticEventsBatcher
37
+ callDiagnosticEventsBatcher: CallDiagnosticEventsBatcher,
46
38
  },
47
39
 
48
40
  namespace: 'Metrics',
49
41
 
50
42
  submit(key, value) {
51
- return this.batcher.request(Object.assign({key}, value));
43
+ return this.batcher.request({key, ...value});
52
44
  },
53
45
 
54
-
55
46
  /**
56
47
  * This corresponds to #sendSemiStructured() in the deprecated metrics handler
57
48
  * @param {string} eventName
@@ -72,45 +63,41 @@ const Metrics = WebexPlugin.extend({
72
63
 
73
64
  // Node does not like this so we need to check if it exists or not
74
65
  // eslint-disable-next-line no-undef
75
- domain: typeof window !== 'undefined' ? window.location.hostname || 'non-browser' : 'non-browser', // Check what else we could measure
66
+ domain:
67
+ typeof window !== 'undefined' ? window.location.hostname || 'non-browser' : 'non-browser', // Check what else we could measure
76
68
  client_id: this.webex.credentials.config.client_id,
77
- user_id: this.webex.internal.device.userId
69
+ user_id: this.webex.internal.device.userId,
78
70
  };
79
71
 
80
72
  try {
81
73
  payload.tags.org_id = this.webex.credentials.getOrgId();
82
- }
83
- catch {
74
+ } catch {
84
75
  this.logger.info('metrics: unable to get orgId');
85
76
  }
86
77
 
87
-
88
78
  payload.fields = {
89
79
  ...props.fields,
90
80
  browser_version: getBrowserVersion(),
91
81
  os_version: getOSVersion(),
92
82
  sdk_version: this.webex.version,
93
83
  platform: 'Web',
94
- spark_user_agent: getSparkUserAgent(this.webex)
84
+ spark_user_agent: getSparkUserAgent(this.webex),
95
85
  };
96
86
 
97
-
98
87
  payload.type = props.type || this.webex.config.metrics.type;
99
88
 
100
-
101
89
  payload.context = {
102
90
  ...props.context,
103
91
  app: {
104
- version: this.webex.version
92
+ version: this.webex.version,
105
93
  },
106
94
  locale: 'en-US',
107
95
  os: {
108
96
  name: getOSName(),
109
- version: getOSVersion()
110
- }
97
+ version: getOSVersion(),
98
+ },
111
99
  };
112
100
 
113
-
114
101
  if (props.eventPayload) {
115
102
  payload.eventPayload = props.eventPayload;
116
103
  }
@@ -121,7 +108,7 @@ const Metrics = WebexPlugin.extend({
121
108
 
122
109
  if (preLoginId) {
123
110
  const _payload = {
124
- metrics: [payload]
111
+ metrics: [payload],
125
112
  };
126
113
 
127
114
  // Do not batch these because pre-login events occur during onboarding, so we will be partially blind
@@ -144,12 +131,12 @@ const Metrics = WebexPlugin.extend({
144
131
  api: 'metrics',
145
132
  resource: 'clientmetrics',
146
133
  headers: {
147
- 'x-prelogin-userid': preLoginId
134
+ 'x-prelogin-userid': preLoginId,
148
135
  },
149
136
  body: {},
150
137
  qs: {
151
- alias: true
152
- }
138
+ alias: true,
139
+ },
153
140
  });
154
141
  },
155
142
 
@@ -161,20 +148,21 @@ const Metrics = WebexPlugin.extend({
161
148
  resource: 'clientmetrics-prelogin',
162
149
  headers: {
163
150
  authorization: token.toString(),
164
- 'x-prelogin-userid': preLoginId
151
+ 'x-prelogin-userid': preLoginId,
165
152
  },
166
- body: payload
167
- }));
153
+ body: payload,
154
+ })
155
+ );
168
156
  },
169
157
 
170
158
  submitCallDiagnosticEvents(payload) {
171
159
  const event = {
172
160
  type: 'diagnostic-event',
173
- eventPayload: payload
161
+ eventPayload: payload,
174
162
  };
175
163
 
176
164
  return this.callDiagnosticEventsBatcher.request(event);
177
- }
165
+ },
178
166
  });
179
167
 
180
168
  export default Metrics;
@@ -27,8 +27,8 @@ describe('plugin-metrics', () => {
27
27
  beforeEach(() => {
28
28
  webex = new MockWebex({
29
29
  children: {
30
- metrics: Metrics
31
- }
30
+ metrics: Metrics,
31
+ },
32
32
  });
33
33
 
34
34
  webex.config.metrics = config.metrics;
@@ -37,7 +37,7 @@ describe('plugin-metrics', () => {
37
37
  return Promise.resolve({
38
38
  statusCode: 204,
39
39
  body: undefined,
40
- options
40
+ options,
41
41
  });
42
42
  };
43
43
  sinon.spy(webex, 'request');
@@ -58,9 +58,10 @@ describe('plugin-metrics', () => {
58
58
  it('clears the queue', () => {
59
59
  clock.uninstall();
60
60
 
61
- return webex.internal.metrics.batcher.request({
62
- key: 'testMetric'
63
- })
61
+ return webex.internal.metrics.batcher
62
+ .request({
63
+ key: 'testMetric',
64
+ })
64
65
  .then(() => {
65
66
  assert.calledOnce(webex.request);
66
67
  assert.lengthOf(webex.internal.metrics.batcher.queue, 0);
@@ -80,26 +81,28 @@ describe('plugin-metrics', () => {
80
81
 
81
82
  sinon.stub(webex, 'request').callsFake((options) => {
82
83
  options.headers = {
83
- trackingid: count
84
+ trackingid: count,
84
85
  };
85
86
 
86
87
  count += 1;
87
88
  if (count < 9) {
88
- return Promise.reject(new WebexHttpError.NetworkOrCORSError({
89
- statusCode: 0,
90
- options
91
- }));
89
+ return Promise.reject(
90
+ new WebexHttpError.NetworkOrCORSError({
91
+ statusCode: 0,
92
+ options,
93
+ })
94
+ );
92
95
  }
93
96
 
94
97
  return Promise.resolve({
95
98
  statusCode: 204,
96
99
  body: undefined,
97
- options
100
+ options,
98
101
  });
99
102
  });
100
103
 
101
104
  const promise = webex.internal.metrics.batcher.request({
102
- key: 'testMetric'
105
+ key: 'testMetric',
103
106
  });
104
107
 
105
108
  return promiseTick(50)
@@ -159,8 +162,16 @@ describe('plugin-metrics', () => {
159
162
  .then(() => assert.lengthOf(webex.internal.metrics.batcher.queue, 0))
160
163
  .then(() => promise)
161
164
  .then(() => {
162
- assert.lengthOf(webex.request.args[1][0].body.metrics, 1, 'Reenqueuing the metric once did not increase the number of metrics to be submitted');
163
- assert.lengthOf(webex.request.args[2][0].body.metrics, 1, 'Reenqueuing the metric twice did not increase the number of metrics to be submitted');
165
+ assert.lengthOf(
166
+ webex.request.args[1][0].body.metrics,
167
+ 1,
168
+ 'Reenqueuing the metric once did not increase the number of metrics to be submitted'
169
+ );
170
+ assert.lengthOf(
171
+ webex.request.args[2][0].body.metrics,
172
+ 1,
173
+ 'Reenqueuing the metric twice did not increase the number of metrics to be submitted'
174
+ );
164
175
  assert.lengthOf(webex.internal.metrics.batcher.queue, 0);
165
176
  });
166
177
  });
@@ -27,8 +27,8 @@ describe('plugin-metrics', () => {
27
27
  beforeEach(() => {
28
28
  webex = new MockWebex({
29
29
  children: {
30
- metrics: Metrics
31
- }
30
+ metrics: Metrics,
31
+ },
32
32
  });
33
33
 
34
34
  webex.config.metrics = config.metrics;
@@ -37,7 +37,7 @@ describe('plugin-metrics', () => {
37
37
  return Promise.resolve({
38
38
  statusCode: 204,
39
39
  body: undefined,
40
- options
40
+ options,
41
41
  });
42
42
  };
43
43
  sinon.spy(webex, 'request');
@@ -58,14 +58,15 @@ describe('plugin-metrics', () => {
58
58
  it('clears the queue', () => {
59
59
  clock.uninstall();
60
60
 
61
- return webex.internal.metrics.callDiagnosticEventsBatcher.request({
62
- type: 'diagnostic-event',
63
- eventPayload: {
64
- originTime: {
65
- triggered: 'mock triggered timestamp'
66
- }
67
- }
68
- })
61
+ return webex.internal.metrics.callDiagnosticEventsBatcher
62
+ .request({
63
+ type: 'diagnostic-event',
64
+ eventPayload: {
65
+ originTime: {
66
+ triggered: 'mock triggered timestamp',
67
+ },
68
+ },
69
+ })
69
70
  .then(() => {
70
71
  assert.calledOnce(webex.request);
71
72
  assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0);
@@ -85,21 +86,23 @@ describe('plugin-metrics', () => {
85
86
 
86
87
  sinon.stub(webex, 'request').callsFake((options) => {
87
88
  options.headers = {
88
- trackingid: count
89
+ trackingid: count,
89
90
  };
90
91
 
91
92
  count += 1;
92
93
  if (count < 9) {
93
- return Promise.reject(new WebexHttpError.NetworkOrCORSError({
94
- statusCode: 0,
95
- options
96
- }));
94
+ return Promise.reject(
95
+ new WebexHttpError.NetworkOrCORSError({
96
+ statusCode: 0,
97
+ options,
98
+ })
99
+ );
97
100
  }
98
101
 
99
102
  return Promise.resolve({
100
103
  statusCode: 204,
101
104
  body: undefined,
102
- options
105
+ options,
103
106
  });
104
107
  });
105
108
 
@@ -107,13 +110,15 @@ describe('plugin-metrics', () => {
107
110
  type: 'diagnostic-event',
108
111
  eventPayload: {
109
112
  originTime: {
110
- triggered: 'mock triggered timestamp'
111
- }
112
- }
113
+ triggered: 'mock triggered timestamp',
114
+ },
115
+ },
113
116
  });
114
117
 
115
118
  return promiseTick(50)
116
- .then(() => assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 1))
119
+ .then(() =>
120
+ assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 1)
121
+ )
117
122
  .then(() => clock.tick(config.metrics.batcherWait))
118
123
  .then(() => assert.calledOnce(webex.request))
119
124
 
@@ -166,11 +171,21 @@ describe('plugin-metrics', () => {
166
171
  .then(() => assert.callCount(webex.request, 9))
167
172
 
168
173
  .then(() => promiseTick(50))
169
- .then(() => assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0))
174
+ .then(() =>
175
+ assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0)
176
+ )
170
177
  .then(() => promise)
171
178
  .then(() => {
172
- assert.lengthOf(webex.request.args[1][0].body.metrics, 1, 'Reenqueuing the metric once did not increase the number of metrics to be submitted');
173
- assert.lengthOf(webex.request.args[2][0].body.metrics, 1, 'Reenqueuing the metric twice did not increase the number of metrics to be submitted');
179
+ assert.lengthOf(
180
+ webex.request.args[1][0].body.metrics,
181
+ 1,
182
+ 'Reenqueuing the metric once did not increase the number of metrics to be submitted'
183
+ );
184
+ assert.lengthOf(
185
+ webex.request.args[2][0].body.metrics,
186
+ 1,
187
+ 'Reenqueuing the metric twice did not increase the number of metrics to be submitted'
188
+ );
174
189
  assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0);
175
190
  });
176
191
  });
@@ -27,8 +27,8 @@ describe('plugin-metrics', () => {
27
27
  beforeEach(() => {
28
28
  webex = new MockWebex({
29
29
  children: {
30
- metrics: Metrics
31
- }
30
+ metrics: Metrics,
31
+ },
32
32
  });
33
33
 
34
34
  webex.config.metrics = config.metrics;
@@ -37,7 +37,7 @@ describe('plugin-metrics', () => {
37
37
  return Promise.resolve({
38
38
  statusCode: 204,
39
39
  body: undefined,
40
- options
40
+ options,
41
41
  });
42
42
  };
43
43
  sinon.spy(webex, 'request');
@@ -58,9 +58,10 @@ describe('plugin-metrics', () => {
58
58
  it('clears the queue', () => {
59
59
  clock.uninstall();
60
60
 
61
- return webex.internal.metrics.clientMetricsBatcher.request({
62
- key: 'testMetric'
63
- })
61
+ return webex.internal.metrics.clientMetricsBatcher
62
+ .request({
63
+ key: 'testMetric',
64
+ })
64
65
  .then(() => {
65
66
  assert.calledOnce(webex.request);
66
67
  assert.lengthOf(webex.internal.metrics.clientMetricsBatcher.queue, 0);
@@ -80,26 +81,28 @@ describe('plugin-metrics', () => {
80
81
 
81
82
  sinon.stub(webex, 'request').callsFake((options) => {
82
83
  options.headers = {
83
- trackingid: count
84
+ trackingid: count,
84
85
  };
85
86
 
86
87
  count += 1;
87
88
  if (count < 9) {
88
- return Promise.reject(new WebexHttpError.NetworkOrCORSError({
89
- statusCode: 0,
90
- options
91
- }));
89
+ return Promise.reject(
90
+ new WebexHttpError.NetworkOrCORSError({
91
+ statusCode: 0,
92
+ options,
93
+ })
94
+ );
92
95
  }
93
96
 
94
97
  return Promise.resolve({
95
98
  statusCode: 204,
96
99
  body: undefined,
97
- options
100
+ options,
98
101
  });
99
102
  });
100
103
 
101
104
  const promise = webex.internal.metrics.clientMetricsBatcher.request({
102
- key: 'testMetric'
105
+ key: 'testMetric',
103
106
  });
104
107
 
105
108
  return promiseTick(50)
@@ -159,8 +162,16 @@ describe('plugin-metrics', () => {
159
162
  .then(() => assert.lengthOf(webex.internal.metrics.clientMetricsBatcher.queue, 0))
160
163
  .then(() => promise)
161
164
  .then(() => {
162
- assert.lengthOf(webex.request.args[1][0].body.metrics, 1, 'Reenqueuing the metric once did not increase the number of metrics to be submitted');
163
- assert.lengthOf(webex.request.args[2][0].body.metrics, 1, 'Reenqueuing the metric twice did not increase the number of metrics to be submitted');
165
+ assert.lengthOf(
166
+ webex.request.args[1][0].body.metrics,
167
+ 1,
168
+ 'Reenqueuing the metric once did not increase the number of metrics to be submitted'
169
+ );
170
+ assert.lengthOf(
171
+ webex.request.args[2][0].body.metrics,
172
+ 1,
173
+ 'Reenqueuing the metric twice did not increase the number of metrics to be submitted'
174
+ );
164
175
  assert.lengthOf(webex.internal.metrics.clientMetricsBatcher.queue, 0);
165
176
  });
166
177
  });