forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvester.js
84 lines (76 loc) · 1.9 KB
/
harvester.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
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
/**
* @class
* @classdesc Used to keep track of all registered aggregators.
*/
module.exports = class Harvester {
constructor() {
this.aggregators = []
}
/**
* Calls start on every registered aggregator that is enabled.
*/
start() {
this.aggregators.forEach((aggregator) => {
if (aggregator.enabled) {
aggregator.start()
}
})
}
/**
* Calls send on every registered aggregator that is enabled.
* It then will wait for the collector to acknowledge that the data has been
* sent and resolve a promise.
*
* @param {Function} callback function to call once all aggregators have sent data
*/
clear(callback) {
Promise.all(
this.aggregators.map((aggregator) => {
return new Promise((resolve) => {
if (aggregator.enabled) {
aggregator.once(`finished_data_send-${aggregator.method}`, function finish() {
resolve()
})
aggregator.send()
} else {
// No data to flush because aggregator is not enabled
resolve()
}
})
})
).then(() => {
// Get out of the promise so callback errors aren't treated as
// promise rejections.
setImmediate(callback)
})
}
/**
* Calls stop on every registered aggregator.
*/
stop() {
this.aggregators.forEach((aggregator) => aggregator.stop())
}
/**
* Calls reconfigure on every registered aggregator.
*
* @param {object} config updated agent configuration
*/
update(config) {
this.aggregators.forEach((aggregator) => {
aggregator.reconfigure(config)
})
}
/**
* Registers an aggregator with the Harvester instance.
*
* @param aggregator
*/
add(aggregator) {
this.aggregators.push(aggregator)
}
}