-
Notifications
You must be signed in to change notification settings - Fork 442
/
send-aggregated-metrics-to-datadog.js
544 lines (517 loc) · 23.9 KB
/
send-aggregated-metrics-to-datadog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
module.exports = {
friendlyName: 'Send aggregated metrics to datadog',
description: 'Sends the aggregated metrics for usage statistics reported by Fleet instances in the past week',
fn: async function () {
sails.log('Running custom shell script... (`sails run send-metrics-to-datadog`)');
let nowAt = Date.now();
let oneWeekAgoAt = nowAt - (1000 * 60 * 60 * 24 * 7);
// get a timestamp in seconds to use for the metrics we'll send to datadog.
let timestampForTheseMetrics = Math.floor(nowAt / 1000);
// Get all the usage snapshots for the past week.
let usageStatisticsReportedInTheLastWeek = await HistoricalUsageSnapshot.find({
createdAt: { '>=': oneWeekAgoAt},// Search for records created in the past week.
})
.sort('createdAt DESC');// Sort the results by the createdAt timestamp
// Filter out development premium licenses and loadtests.
let filteredStatistics = _.filter(usageStatisticsReportedInTheLastWeek, (report)=>{
return !_.contains(['Fleet Sandbox', 'fleet-loadtest', 'development-only', 'Dev license (expired)', ''], report.organization);
});
let statisticsReportedByFleetInstance = _.groupBy(filteredStatistics, 'anonymousIdentifier');
let metricsToReport = [];
let latestStatisticsForEachInstance = [];
for (let id in statisticsReportedByFleetInstance) {
let lastReportIdForThisInstance = _.max(_.pluck(statisticsReportedByFleetInstance[id], 'id'));
let latestReportFromThisInstance = _.find(statisticsReportedByFleetInstance[id], {id: lastReportIdForThisInstance});
latestStatisticsForEachInstance.push(latestReportFromThisInstance);
}
// Get a filtered array of metrics reported by Fleet Premium instances
let latestPremiumUsageStatistics = _.filter(latestStatisticsForEachInstance, {licenseTier: 'premium'});
// Group reports by organization name.
let reportsByOrgName = _.groupBy(latestPremiumUsageStatistics, 'organization');
for(let org in reportsByOrgName) {
// Sort the results for this array by the createdAt value. This makes sure we're always sending the most recent results.
let reportsForThisOrg = _.sortByOrder(reportsByOrgName[org], 'createdAt', 'desc');
let lastReportForThisOrg = reportsForThisOrg[0];
// Get the metrics we'll report for each org.
// Combine the numHostsEnrolled values from the last report for each unique Fleet instance that reports this organization.
let totalNumberOfHostsReportedByThisOrg = _.sum(reportsForThisOrg, (report)=>{
return report.numHostsEnrolled;
});
let lastReportedFleetVersion = lastReportForThisOrg.fleetVersion;
let hostCountMetricForThisOrg = {
metric: 'usage_statistics.num_hosts_enrolled_by_org',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: totalNumberOfHostsReportedByThisOrg
}],
resources: [{
name: reportsByOrgName[org][0].anonymousIdentifier,
type: 'fleet_instance'
}],
tags: [
`organization:${org}`,
`fleet_version:${lastReportedFleetVersion}`,
],
};
metricsToReport.push(hostCountMetricForThisOrg);
}
// Filter the statistics to be only for released versions of Fleet.
// Note: we're doing this after we've reported the metrics for Fleet Premium instances to make sure
// that we are reporting metrics sent by customers who may be using a non-4.x.x version of Fleet.
let latestStatisticsReportedByReleasedFleetVersions = _.filter(latestStatisticsForEachInstance, (statistics)=>{
return _.startsWith(statistics.fleetVersion, '4.');
});
let numberOfInstancesToReport = latestStatisticsReportedByReleasedFleetVersions.length;
// Build aggregated metrics for JSON attrributes
// Create an empty object to store combined host counts.
let combinedHostsEnrolledByOperatingSystem = {};
// Get an array of the last reported hostsEnrolledByOperatingSystem values.
let allHostsEnrolledByOsValues = _.pluck(latestStatisticsReportedByReleasedFleetVersions, 'hostsEnrolledByOperatingSystem');
// Iterate through each reported value, and combine them.
for(let reportedHostCounts of allHostsEnrolledByOsValues) {
_.merge(combinedHostsEnrolledByOperatingSystem, reportedHostCounts, (combinedCountsForThisOperatingSystemType, countsForThisOperatingSystemType) => {
if(Array.isArray(combinedCountsForThisOperatingSystemType) && Array.isArray(countsForThisOperatingSystemType)){
let mergedArrayOfHostCounts = [];
// Iterate through the counts in the array we're combining with the aggregator object.
for (let versionInfo of countsForThisOperatingSystemType) {
let matchingVersionFromCombinedCounts = _.find(combinedCountsForThisOperatingSystemType, (osType) => osType.version === versionInfo.version);
if (matchingVersionFromCombinedCounts) {
mergedArrayOfHostCounts.push({ version: versionInfo.version, numEnrolled: versionInfo.numEnrolled + matchingVersionFromCombinedCounts.numEnrolled });
} else {
mergedArrayOfHostCounts.push(versionInfo);
}
}
// Now add the hostCounts from the combined host counts.
for (let versionInfo of combinedCountsForThisOperatingSystemType) {
let versionOnlyExistsInCombinedCounts = !_.find(countsForThisOperatingSystemType, (osVersion)=>{ return osVersion.version === versionInfo.version;});
if (versionOnlyExistsInCombinedCounts) {
mergedArrayOfHostCounts.push(versionInfo);
}
}
return mergedArrayOfHostCounts;
}
});
}
for(let operatingSystem in combinedHostsEnrolledByOperatingSystem) {
// For every object in the array, we'll send a metric to track host count for each operating system version.
for(let osVersion of combinedHostsEnrolledByOperatingSystem[operatingSystem]) {
// Only continue if the object in the array has a numEnrolled and version value.
if(osVersion.numEnrolled && osVersion.version !== '') {
let metricToAdd = {
metric: 'usage_statistics_v2.host_count_by_os_version',
type: 3,
points: [{timestamp: timestampForTheseMetrics, value:osVersion.numEnrolled}],
resources: [{name: operatingSystem, type: 'os_type'}],
tags: [`os_version_name:${osVersion.version}`],
};
// Add the custom metric to the array of metrics to send to Datadog.
metricsToReport.push(metricToAdd);
}//fi
}//∞
}//∞
let allHostsEnrolledByOsqueryVersion = _.pluck(latestStatisticsReportedByReleasedFleetVersions, 'hostsEnrolledByOsqueryVersion');
let combinedHostsEnrolledByOsqueryVersion = [];
let flattenedHostsEnrolledByOsqueryVersions = _.flatten(allHostsEnrolledByOsqueryVersion);
let groupedHostsEnrolledValuesByOsqueryVersion = _.groupBy(flattenedHostsEnrolledByOsqueryVersions, 'osqueryVersion');
for(let osqueryVersion in groupedHostsEnrolledValuesByOsqueryVersion) {
combinedHostsEnrolledByOsqueryVersion.push({
osqueryVersion: osqueryVersion,
numHosts: _.sum(groupedHostsEnrolledValuesByOsqueryVersion[osqueryVersion], (version)=>{return version.numHosts;})
});
}
for(let version of combinedHostsEnrolledByOsqueryVersion) {
if(version.osqueryVersion !== ''){
let metricToAdd = {
metric: 'usage_statistics_v2.host_count_by_osquery_version',
type: 3,
points: [{timestamp: timestampForTheseMetrics, value:version.numHosts}],
tags: [`osquery_version:${version.osqueryVersion}`],
};
// Add the custom metric to the array of metrics to send to Datadog.
metricsToReport.push(metricToAdd);
}
}//∞
let combinedHostsEnrolledByOrbitVersion = [];
let allHostsEnrolledByOrbitVersion = _.pluck(latestStatisticsReportedByReleasedFleetVersions, 'hostsEnrolledByOrbitVersion');
let flattenedHostsEnrolledByOrbitVersions = _.flatten(allHostsEnrolledByOrbitVersion);
let groupedHostsEnrolledValuesByOrbitVersion = _.groupBy(flattenedHostsEnrolledByOrbitVersions, 'orbitVersion');
for(let orbitVersion in groupedHostsEnrolledValuesByOrbitVersion) {
combinedHostsEnrolledByOrbitVersion.push({
orbitVersion: orbitVersion,
numHosts: _.sum(groupedHostsEnrolledValuesByOrbitVersion[orbitVersion], (version)=>{return version.numHosts;})
});
}
for(let version of combinedHostsEnrolledByOrbitVersion) {
if(version.orbitVersion !== '') {
let metricToAdd = {
metric: 'usage_statistics_v2.host_count_by_orbit_version',
type: 3,
points: [{timestamp: timestampForTheseMetrics, value:version.numHosts}],
tags: [`orbit_version:${version.orbitVersion}`],
};
// Add the custom metric to the array of metrics to send to Datadog.
metricsToReport.push(metricToAdd);
}
}//∞
// Merge the arrays of JSON storedErrors
let allStoredErrors = _.pluck(latestStatisticsReportedByReleasedFleetVersions, 'storedErrors');
let flattenedStoredErrors = _.flatten(allStoredErrors);
let groupedStoredErrorsByLocation = _.groupBy(flattenedStoredErrors, 'loc');
let combinedStoredErrors = [];
for(let location in groupedStoredErrorsByLocation) {
combinedStoredErrors.push({
location: groupedStoredErrorsByLocation[location][0].loc,
count: _.sum(groupedStoredErrorsByLocation[location], (location)=>{return location.count;}),
numberOfInstancesReportingThisError: groupedStoredErrorsByLocation[location].length
});
}
for(let error of combinedStoredErrors) {
// Create a new array of tags for this error
let errorTags = [];
let errorLocation = 1;
// Create a tag for each error location
for(let location of error.location) { // iterate throught the location array of this error
// Add the error's location as a custom tag (SNAKE_CASED)
errorTags.push(`error_location_${errorLocation}:${location.replace(/\s/gi, '_')}`);
errorLocation++;
}
// Add a metric with the combined error count for each unique error location
metricsToReport.push({
metric: 'usage_statistics_v2.stored_errors_counts',
type: 3,
points: [{timestamp: timestampForTheseMetrics, value: error.count}],
tags: errorTags,
});
// Add a metric to report how many different instances reported errors with the same location.
metricsToReport.push({
metric: 'usage_statistics_v2.stored_errors_statistics',
type: 3,
points: [{timestamp: timestampForTheseMetrics, value: error.numberOfInstancesReportingThisError}],
tags: errorTags,
});
}//∞
// Build a metric for each Fleet version reported.
let statisticsByReportedFleetVersion = _.groupBy(latestStatisticsReportedByReleasedFleetVersions, 'fleetVersion');
for(let version in statisticsByReportedFleetVersion){
let numberOfInstancesReportingThisVersion = statisticsByReportedFleetVersion[version].length;
metricsToReport.push({
metric: 'usage_statistics.fleet_version',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesReportingThisVersion
}],
tags: [`fleet_version:${version}`],
});
}
// Build a metric for each license tier reported.
let statisticsByReportedFleetLicenseTier = _.groupBy(latestStatisticsReportedByReleasedFleetVersions, 'licenseTier');
for(let tier in statisticsByReportedFleetLicenseTier){
let numberOfInstancesReportingThisLicenseTier = statisticsByReportedFleetLicenseTier[tier].length;
metricsToReport.push({
metric: 'usage_statistics.fleet_license',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesReportingThisLicenseTier
}],
tags: [`license_tier:${tier}`],
});
}
// Build aggregated metrics for boolean variables:
// Software Inventory
let numberOfInstancesWithSoftwareInventoryEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {softwareInventoryEnabled: true}).length;
let numberOfInstancesWithSoftwareInventoryDisabled = numberOfInstancesToReport - numberOfInstancesWithSoftwareInventoryEnabled;
metricsToReport.push({
metric: 'usage_statistics.software_inventory',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithSoftwareInventoryEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.software_inventory',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithSoftwareInventoryDisabled
}],
tags: [`enabled:false`],
});
// vulnDetectionEnabled
let numberOfInstancesWithVulnDetectionEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {vulnDetectionEnabled: true}).length;
let numberOfInstancesWithVulnDetectionDisabled = numberOfInstancesToReport - numberOfInstancesWithVulnDetectionEnabled;
metricsToReport.push({
metric: 'usage_statistics.vuln_detection',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithVulnDetectionEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.vuln_detection',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithVulnDetectionDisabled
}],
tags: [`enabled:false`],
});
// SystemUsersEnabled
let numberOfInstancesWithSystemUsersEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {systemUsersEnabled: true}).length;
let numberOfInstancesWithSystemUsersDisabled = numberOfInstancesToReport - numberOfInstancesWithSystemUsersEnabled;
metricsToReport.push({
metric: 'usage_statistics.system_users',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithSystemUsersEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.system_users',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithSystemUsersDisabled
}],
tags: [`enabled:false`],
});
// hostsStatusWebHookEnabled
let numberOfInstancesWithHostsStatusWebHookEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {hostsStatusWebHookEnabled: true}).length;
let numberOfInstancesWithHostsStatusWebHookDisabled = numberOfInstancesToReport - numberOfInstancesWithHostsStatusWebHookEnabled;
metricsToReport.push({
metric: 'usage_statistics.host_status_webhook',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithHostsStatusWebHookEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.host_status_webhook',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithHostsStatusWebHookDisabled
}],
tags: [`enabled:false`],
});
// mdmMacOsEnabled
let numberOfInstancesWithMdmMacOsEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {mdmMacOsEnabled: true}).length;
let numberOfInstancesWithMdmMacOsDisabled = numberOfInstancesToReport - numberOfInstancesWithMdmMacOsEnabled;
metricsToReport.push({
metric: 'usage_statistics.macos_mdm',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMdmMacOsEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.macos_mdm',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMdmMacOsDisabled
}],
tags: [`enabled:false`],
});
// mdmWindowsEnabled
let numberOfInstancesWithMdmWindowsEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {mdmWindowsEnabled: true}).length;
let numberOfInstancesWithMdmWindowsDisabled = numberOfInstancesToReport - numberOfInstancesWithMdmWindowsEnabled;
metricsToReport.push({
metric: 'usage_statistics.windows_mdm',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMdmWindowsEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.windows_mdm',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMdmWindowsDisabled
}],
tags: [`enabled:false`],
});
// liveQueryDisabled
let numberOfInstancesWithLiveQueryDisabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {liveQueryDisabled: true}).length;
let numberOfInstancesWithLiveQueryEnabled = numberOfInstancesToReport - numberOfInstancesWithLiveQueryDisabled;
metricsToReport.push({
metric: 'usage_statistics.live_query',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithLiveQueryDisabled
}],
tags: [`enabled:false`],
});
metricsToReport.push({
metric: 'usage_statistics.live_query',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithLiveQueryEnabled
}],
tags: [`enabled:true`],
});
// hostExpiryEnabled
let numberOfInstancesWithHostExpiryEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {hostExpiryEnabled: true}).length;
let numberOfInstancesWithHostExpiryDisabled = numberOfInstancesToReport - numberOfInstancesWithHostExpiryEnabled;
metricsToReport.push({
metric: 'usage_statistics.host_expiry',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithHostExpiryEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.host_expiry',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithHostExpiryDisabled
}],
tags: [`enabled:false`],
});
// aiFeaturesDisabled
let numberOfInstancesWithAiFeaturesDisabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {aiFeaturesDisabled: true}).length;
let numberOfInstancesWithAiFeaturesEnabled = numberOfInstancesToReport - numberOfInstancesWithAiFeaturesDisabled;
metricsToReport.push({
metric: 'usage_statistics.ai_features',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithAiFeaturesEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.ai_features',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithAiFeaturesDisabled
}],
tags: [`enabled:false`],
});
// maintenanceWindowsEnabled
let numberOfInstancesWithMaintenanceWindowsEnabled = _.where(latestStatisticsReportedByReleasedFleetVersions, {maintenanceWindowsEnabled: true}).length;
let numberOfInstancesWithMaintenanceWindowsDisabled = numberOfInstancesToReport - numberOfInstancesWithMaintenanceWindowsEnabled;
metricsToReport.push({
metric: 'usage_statistics.maintenance_windows',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMaintenanceWindowsEnabled
}],
tags: [`enabled:true`],
});
metricsToReport.push({
metric: 'usage_statistics.maintenance_windows',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMaintenanceWindowsDisabled
}],
tags: [`enabled:false`],
});
// maintenanceWindowsConfigured
let numberOfInstancesWithMaintenanceWindowsConfigured = _.where(latestStatisticsReportedByReleasedFleetVersions, {maintenanceWindowsEnabled: true}).length;
let numberOfInstancesWithoutMaintenanceWindowsConfigured = numberOfInstancesToReport - numberOfInstancesWithMaintenanceWindowsConfigured;
metricsToReport.push({
metric: 'usage_statistics.maintenance_windows_configured',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithMaintenanceWindowsConfigured
}],
tags: [`configured:true`],
});
metricsToReport.push({
metric: 'usage_statistics.maintenance_windows_configured',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: numberOfInstancesWithoutMaintenanceWindowsConfigured
}],
tags: [`configured:false`],
});
// Create two metrics to track total number of hosts reported in the last week.
let totalNumberOfHostsReportedByPremiumInstancesInTheLastWeek = _.sum(_.pluck(_.filter(latestStatisticsReportedByReleasedFleetVersions, {licenseTier: 'premium'}), 'numHostsEnrolled'));
metricsToReport.push({
metric: 'usage_statistics.total_num_hosts_enrolled',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: totalNumberOfHostsReportedByPremiumInstancesInTheLastWeek
}],
tags: [`license_tier:premium`],
});
let totalNumberOfHostsReportedByFreeInstancesInTheLastWeek = _.sum(_.pluck(_.filter(latestStatisticsReportedByReleasedFleetVersions, {licenseTier: 'free'}), 'numHostsEnrolled'));
metricsToReport.push({
metric: 'usage_statistics.total_num_hosts_enrolled',
type: 3,
points: [{
timestamp: timestampForTheseMetrics,
value: totalNumberOfHostsReportedByFreeInstancesInTheLastWeek
}],
tags: [`license_tier:free`],
});
// FUTURE: Uncomment the section below to send metrics about reported number of queries to Datadog.
// let fleetInstancesThatReportedNumQueries = _.filter(latestStatisticsReportedByReleasedFleetVersions, (statistics)=>{
// return statistics.numQueries > 0;
// });
// let averageNumberOfQueries = Math.foor(_.sum(_.pluck(fleetInstancesThatReportedNumQueries, 'numQueries')) / fleetInstancesThatReportedNumQueries.length);
// metricsToReport.push({
// metric: 'usage_statistics.avg_num_queries',
// type: 3,
// points: [{
// timestamp: timestampForTheseMetrics,
// value: averageNumberOfQueries
// }],
// });
// let highestNumberOfQueries = _.max(_.pluck(fleetInstancesThatReportedNumQueries, 'numQueries'));
// metricsToReport.push({
// metric: 'usage_statistics.max_num_queries',
// type: 3,
// points: [{
// timestamp: timestampForTheseMetrics,
// value: highestNumberOfQueries
// }],
// });
// Break the metrics into smaller arrays to ensure we don't exceed Datadog's 512 kb request body limit.
let chunkedMetrics = _.chunk(metricsToReport, 500);// Note: 500 stringified JSON metrics is ~410 kb.
for(let chunkOfMetrics of chunkedMetrics) {
await sails.helpers.http.post.with({
url: 'https://api.us5.datadoghq.com/api/v2/series',
data: {
series: chunkOfMetrics,
},
headers: {
'DD-API-KEY': sails.config.custom.datadogApiKey,
'Content-Type': 'application/json',
}
}).intercept((err)=>{
// If there was an error sending metrics to Datadog, we'll log the error in a warning, but we won't throw an error.
// This way, we'll still return a 200 status to the Fleet instance that sent usage analytics.
return new Error(`When the send-metrics-to-datadog script sent a request to send metrics to Datadog, an error occured. Raw error: ${require('util').inspect(err)}`);
});
}//∞
sails.log(`Aggregated metrics for ${numberOfInstancesToReport} Fleet instances from the past week sent to Datadog.`);
}
};