Skip to content

Commit 48954e3

Browse files
author
Serkan ÖZAL
authored
Add memory stats (#3)
1 parent fb85294 commit 48954e3

File tree

7 files changed

+179
-11
lines changed

7 files changed

+179
-11
lines changed

dist/post/index.js

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sc/index.js

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sc/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interfaces/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export interface CPUStats {
1010
readonly systemLoad: number
1111
}
1212

13+
export interface MemoryStats {
14+
readonly time: number
15+
readonly totalMemoryMb: number
16+
readonly activeMemoryMb: number
17+
readonly availableMemoryMb: number
18+
}
19+
1320
export interface NetworkStats {
1421
readonly time: number
1522
readonly rxMb: number
@@ -32,6 +39,11 @@ export interface ProcessedCPUStats {
3239
readonly systemLoadX: ProcessedStats[],
3340
}
3441

42+
export interface ProcessedMemoryStats {
43+
readonly activeMemoryX: ProcessedStats[],
44+
readonly availableMemoryX: ProcessedStats[],
45+
}
46+
3547
export interface ProcessedNetworkStats {
3648
readonly networkReadX: ProcessedStats[],
3749
readonly networkWriteX: ProcessedStats[],

src/post.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import {
1010
StackedAreaGraphOptions,
1111
GraphResponse,
1212
ProcessedStats,
13+
CPUStats,
14+
MemoryStats,
15+
NetworkStats,
1316
DiskStats,
17+
ProcessedCPUStats,
18+
ProcessedMemoryStats,
19+
ProcessedNetworkStats,
1420
ProcessedDiskStats,
15-
NetworkStats,
16-
ProcessedNetworkStats, ProcessedCPUStats, CPUStats
1721
} from './interfaces'
1822

1923
const STAT_SERVER_PORT: number = 7777
@@ -30,6 +34,7 @@ async function run(): Promise<void> {
3034
await triggerStatCollect()
3135

3236
const { userLoadX, systemLoadX } = await getCPUStats()
37+
const { activeMemoryX, availableMemoryX } = await getMemoryStats()
3338
const { networkReadX, networkWriteX } = await getNetworkStats()
3439
const { diskReadX, diskWriteX } = await getDiskStats()
3540

@@ -38,17 +43,33 @@ async function run(): Promise<void> {
3843
areas: [
3944
{
4045
label: 'User Load',
41-
color: '#e41a1c',
46+
color: '#e41a1c99',
4247
points: userLoadX
4348
},
4449
{
4550
label: 'System Load',
46-
color: '#ff7f00',
51+
color: '#ff7f0099',
4752
points: systemLoadX
4853
}
4954
]
5055
})
5156

57+
const memoryUsage = await getStackedAreaGraph({
58+
label: 'Memory Usage (MB)',
59+
areas: [
60+
{
61+
label: 'Used',
62+
color: '#377eb899',
63+
points: activeMemoryX
64+
},
65+
{
66+
label: 'Free',
67+
color: '#4daf4a99',
68+
points: availableMemoryX
69+
}
70+
]
71+
})
72+
5273
const networkIORead = await getLineGraph({
5374
label: 'Network I/O Read (MB)',
5475
line: {
@@ -123,6 +144,9 @@ async function run(): Promise<void> {
123144
'### CPU Metrics',
124145
`![${cpuLoad.id}](${cpuLoad.url})`,
125146
'',
147+
'### Memory Metrics',
148+
`![${memoryUsage.id}](${memoryUsage.url})`,
149+
'',
126150
'### IO Metrics',
127151
'| | Read | Write |',
128152
'|--- |--- |--- |',
@@ -173,6 +197,31 @@ async function getCPUStats(): Promise<ProcessedCPUStats> {
173197
return { userLoadX, systemLoadX }
174198
}
175199

200+
async function getMemoryStats(): Promise<ProcessedMemoryStats> {
201+
let activeMemoryX: ProcessedStats[] = []
202+
let availableMemoryX: ProcessedStats[] = []
203+
204+
logger.debug('Getting memory stats ...')
205+
const response = await axios.get(
206+
`http://localhost:${STAT_SERVER_PORT}/memory`
207+
)
208+
logger.debug(`Got memory stats: ${JSON.stringify(response.data)}`)
209+
210+
response.data.forEach((element: MemoryStats) => {
211+
activeMemoryX.push({
212+
x: element.time,
213+
y: element.activeMemoryMb
214+
})
215+
216+
availableMemoryX.push({
217+
x: element.time,
218+
y: element.availableMemoryMb
219+
})
220+
})
221+
222+
return { activeMemoryX, availableMemoryX }
223+
}
224+
176225
async function getNetworkStats(): Promise<ProcessedNetworkStats> {
177226
let networkReadX: ProcessedStats[] = []
178227
let networkWriteX: ProcessedStats[] = []

src/statCollector.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import si from 'systeminformation'
22
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
33
import * as logger from './logger'
44

5-
import { CPUStats, DiskStats, NetworkStats } from './interfaces'
5+
import { CPUStats, MemoryStats, DiskStats, NetworkStats } from './interfaces'
66

77
const STATS_FREQ: number =
88
parseInt(process.env.WORKFLOW_TELEMETRY_STAT_FREQ || '') || 5000
@@ -18,7 +18,7 @@ let statCollectTime: number = 0
1818

1919
///////////////////////////
2020

21-
// CPU Stats //
21+
// CPU Stats //
2222
///////////////////////////
2323

2424
const cpuStatsHistogram: CPUStats[] = []
@@ -45,6 +45,33 @@ function collectCPUStats(
4545

4646
///////////////////////////
4747

48+
// Memory Stats //
49+
///////////////////////////
50+
51+
const memoryStatsHistogram: MemoryStats[] = []
52+
53+
function collectMemoryStats(
54+
statTime: number,
55+
timeInterval: number
56+
): Promise<any> {
57+
return si
58+
.mem()
59+
.then((data: si.Systeminformation.MemData) => {
60+
const memoryStats: MemoryStats = {
61+
time: statTime,
62+
totalMemoryMb: data.total / 1024 / 1024,
63+
activeMemoryMb: data.active / 1024 / 1024,
64+
availableMemoryMb: data.available / 1024 / 1024
65+
}
66+
memoryStatsHistogram.push(memoryStats)
67+
})
68+
.catch((error: any) => {
69+
logger.error(error)
70+
})
71+
}
72+
73+
///////////////////////////
74+
4875
// Network Stats //
4976
///////////////////////////
5077

@@ -117,6 +144,7 @@ async function collectStats(triggeredFromScheduler: boolean = true) {
117144
const promises: Promise<any>[] = []
118145

119146
promises.push(collectCPUStats(statCollectTime, timeInterval))
147+
promises.push(collectMemoryStats(statCollectTime, timeInterval))
120148
promises.push(collectNetworkStats(statCollectTime, timeInterval))
121149
promises.push(collectDiskStats(statCollectTime, timeInterval))
122150

@@ -143,6 +171,15 @@ function startHttpServer() {
143171
}
144172
break
145173
}
174+
case '/memory': {
175+
if (request.method === 'GET') {
176+
response.end(JSON.stringify(memoryStatsHistogram))
177+
} else {
178+
response.statusCode = 405
179+
response.end()
180+
}
181+
break
182+
}
146183
case '/network': {
147184
if (request.method === 'GET') {
148185
response.end(JSON.stringify(networkStatsHistogram))

0 commit comments

Comments
 (0)