Skip to content

Commit 992fb1c

Browse files
committed
merge
Merge branch 'dev' of https://github.com/oslabs-beta/Chronos into ys/updateEventContext
2 parents 4f29dc8 + a3793fe commit 992fb1c

File tree

5 files changed

+218
-336
lines changed

5 files changed

+218
-336
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
const si = require('systeminformation');
2+
3+
const healthHelpers = {};
4+
5+
/*
6+
This method returns an promise that resolves to an array of
7+
si data points.
8+
*/
9+
10+
healthHelpers.collectHealthData = () => {
11+
// Create an array to hold the promises returned by si
12+
const promises = [];
13+
// for use with every object
14+
const time = Date.now();
15+
// Fill the array of promises
16+
promises.push(
17+
si
18+
.cpuCurrentspeed()
19+
.then(data => ({
20+
metric: 'cpuspeed',
21+
value: data.avg,
22+
category: 'CPU',
23+
time,
24+
}))
25+
.catch(err => {
26+
if (err) {
27+
throw err;
28+
}
29+
})
30+
);
31+
32+
promises.push(
33+
si
34+
.cpuTemperature()
35+
.then(data => ({
36+
metric: 'cputemp',
37+
value: data.main,
38+
category: 'CPU',
39+
time,
40+
}))
41+
.catch(err => {
42+
if (err) {
43+
throw err;
44+
}
45+
})
46+
);
47+
48+
promises.push(
49+
si
50+
.currentLoad()
51+
.then(data => ({
52+
metric: 'cpuloadpercent',
53+
value: data.currentload,
54+
category: 'CPU',
55+
time,
56+
}))
57+
.catch(err => {
58+
throw err;
59+
})
60+
);
61+
62+
promises.push(
63+
si
64+
.mem()
65+
.then(data => ({
66+
totalmemory: data.total,
67+
freememory: data.free,
68+
usedmemory: data.used,
69+
activememory: data.active,
70+
}))
71+
.then(data => {
72+
const memMetrics = [];
73+
for (const metric in data) {
74+
memMetrics.push({
75+
metric,
76+
value: data[metric],
77+
category: 'Memory',
78+
time,
79+
});
80+
}
81+
return memMetrics;
82+
})
83+
.catch(err => {
84+
if (err) {
85+
throw err;
86+
}
87+
})
88+
);
89+
90+
promises.push(
91+
si
92+
.processes()
93+
.then(data => ({
94+
totalprocesses: data.all,
95+
blockedprocesses: data.blocked,
96+
runningprocesses: data.running,
97+
sleepingprocesses: data.sleeping,
98+
}))
99+
.then(data => {
100+
const processMetrics = [];
101+
for (const metric in data) {
102+
processMetrics.push({
103+
metric,
104+
value: data[metric],
105+
category: 'Processes',
106+
time,
107+
});
108+
}
109+
return processMetrics;
110+
})
111+
.catch(err => {
112+
if (err) {
113+
throw err;
114+
}
115+
})
116+
);
117+
118+
promises.push(
119+
si
120+
.inetLatency()
121+
.then(data => ({
122+
metric: 'latency',
123+
value: data,
124+
category: 'Latency',
125+
time,
126+
}))
127+
.catch(err => {
128+
if (err) {
129+
throw err;
130+
}
131+
})
132+
);
133+
// Return an promise that resolves to an array of all of the data points unnested
134+
return Promise.all(promises).then(array => array.flat());
135+
};
136+
137+
module.exports = healthHelpers;

chronos_npm_package/controllers/kafkaHelpers.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@ kafkaHelpers.extractWord = function (str) {
2727
// executes first to ensure that the provided jmxuri provides legitimate data.
2828
// if it doesnt then an error is thrown
2929
kafkaHelpers.initialFetch = function (jmxuri) {
30-
fetch(jmxuri)
31-
.then(response => {
32-
if (response.status !== 200) {
33-
throw new Error(`Error: The provided URI for the JMX exporter is invalid`);
34-
} else {
35-
console.log('Initial fetch to JMX Exporter was successful.');
36-
}
37-
return response.text();
38-
})
39-
.then(text => {
40-
console.log('\nInitial Fetch Response Text:\n', text);
41-
});
30+
fetch(jmxuri).then(response => {
31+
if (response.status !== 200) {
32+
throw new Error(`Error: The provided URI for the JMX exporter is invalid`);
33+
} else {
34+
console.log('Initial fetch to JMX Exporter was successful.');
35+
}
36+
return response.text();
37+
});
4238
};
4339

4440
// fetches kafka metrics from the user-specified location of JMX prometheus and returns the processed result

chronos_npm_package/controllers/mongo.js

Lines changed: 9 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const HealthModelFunc = require('../models/HealthModel');
77
const ContainerInfoFunc = require('../models/ContainerInfo');
88
const KafkaModel = require('../models/KafkaModel');
99
const { kafkaFetch } = require('./kafkaHelpers.js');
10+
const { collectHealthData } = require('./healthHelpers.js');
1011
require('../models/ContainerInfo');
1112

1213
// Handle deprecation warnings
@@ -106,112 +107,17 @@ chronos.communications = ({ microservice, slack, email }) => {
106107
* @param {number} interval Interval for continuous data collection
107108
*/
108109
chronos.health = ({ microservice, interval }) => {
109-
let cpuspeed;
110-
let cputemp;
111-
let cpuloadpercent;
112-
let totalmemory;
113-
let freememory;
114-
let usedmemory;
115-
let activememory;
116-
let latency;
117-
let totalprocesses;
118-
let blockedprocesses;
119-
let runningprocesses;
120-
let sleepingprocesses;
121-
122-
// Collect data at every interval
123110
setInterval(() => {
124-
si.cpuCurrentspeed()
125-
.then(data => {
126-
cpuspeed = data.avg;
127-
})
128-
.catch(err => {
129-
if (err) {
130-
throw err;
131-
}
132-
});
133-
134-
si.cpuTemperature()
135-
.then(data => {
136-
cputemp = data.main;
111+
collectHealthData()
112+
.then(healthMetrics => {
113+
const HealthModel = HealthModelFunc(`${microservice}`);
114+
return HealthModel.insertMany(healthMetrics);
137115
})
138-
.catch(err => {
139-
if (err) {
140-
throw err;
141-
}
142-
});
143-
144-
si.currentLoad()
145-
.then(data => {
146-
cpuloadpercent = data.currentload;
147-
})
148-
.catch(err => {
149-
throw err;
150-
});
151-
152-
si.mem()
153-
.then(data => {
154-
totalmemory = data.total;
155-
freememory = data.free;
156-
usedmemory = data.used;
157-
activememory = data.active;
158-
})
159-
.catch(err => {
160-
if (err) {
161-
throw err;
162-
}
163-
});
164-
165-
si.processes()
166-
.then(data => {
167-
totalprocesses = data.all;
168-
blockedprocesses = data.blocked;
169-
runningprocesses = data.running;
170-
sleepingprocesses = data.sleeping;
171-
})
172-
.catch(err => {
173-
if (err) {
174-
throw err;
175-
}
176-
});
177-
178-
si.inetLatency()
179-
.then(data => {
180-
latency = data;
181-
})
182-
.catch(err => {
183-
if (err) {
184-
throw err;
185-
}
186-
});
187-
188-
// Create collection using name of microservice
189-
const HealthModel = HealthModelFunc(`${microservice}`);
190-
191-
// Save new health document
192-
const health = new HealthModel({
193-
cpuspeed,
194-
cputemp,
195-
cpuloadpercent,
196-
totalmemory,
197-
freememory,
198-
usedmemory,
199-
activememory,
200-
totalprocesses,
201-
runningprocesses,
202-
blockedprocesses,
203-
sleepingprocesses,
204-
latency,
205-
});
206-
207-
health
208-
.save()
209116
.then(() => {
210-
console.log('Health data recorded');
117+
console.log('Health data recorded in MongoDB');
211118
})
212-
.catch(err => console.log('Error saving health data: ', err.message));
119+
.catch(err => console.log('Error inserting health documents: ', err));
213120
}, interval);
214-
console.log('Interval set, recording health data');
215121
};
216122

217123
/**
@@ -314,7 +220,6 @@ chronos.kafka = function (userConfig) {
314220
// fetch the data from Kafka with kafkaFetch()
315221
// then take turn each result in the returned array into a kafkaModel doc
316222
// insertMany into the the KafkaModel
317-
console.log('Starting Kafka Collection');
318223
setInterval(() => {
319224
kafkaFetch(userConfig)
320225
.then(parsedArray => {
@@ -324,17 +229,9 @@ chronos.kafka = function (userConfig) {
324229
}
325230
return KafkaModel.insertMany(documents);
326231
})
327-
.catch(err => console.log('Error inserting kafka documents: ', err));
232+
.then(() => console.log('Kafka metrics recorded in MongoDB'))
233+
.catch(err => console.log('Error inserting kafka documents in MongoDB: ', err));
328234
}, userConfig.interval);
329235
};
330236

331-
// // grabs container data for multiple containers info - TBD
332-
// chronos.dockerInfo = ({ microservice, interval }) => {
333-
// si.dockerInfo()
334-
// .then(function (data) {
335-
// console.log('data from container info', data);
336-
// })
337-
// .catch(err => console.log('Error saving health data: ', err.message));
338-
// };
339-
340237
module.exports = chronos;

0 commit comments

Comments
 (0)