Skip to content

Commit 398a0e8

Browse files
committed
feat: add process info diagnostic for cpu, memory, and system metrics
1 parent 4ae3312 commit 398a0e8

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

scopes/harmony/diagnostic/diagnostic.graphql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export class DiagnosticGraphql implements Schema {
1515
resolvers = {
1616
Query: {
1717
_diagnostic: () => {
18-
return this.diagnosticMain.getDiagnosticData();
18+
// return this.diagnosticMain.getDiagnosticData();
19+
return {};
1920
},
2021
},
2122
};

scopes/harmony/diagnostic/diagnostic.main.runtime.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getBitVersion } from '@teambit/bit.get-bit-version';
2+
import os from 'os';
23

34
import type { SlotRegistry } from '@teambit/harmony';
45
import { Slot } from '@teambit/harmony';
@@ -45,13 +46,46 @@ export class DiagnosticMain {
4546
return { version };
4647
}
4748

49+
static getProcessInfo() {
50+
const memUsage = process.memoryUsage();
51+
const cpuUsage = process.cpuUsage();
52+
return {
53+
uptime: process.uptime(),
54+
pid: process.pid,
55+
nodeVersion: process.version,
56+
platform: process.platform,
57+
arch: process.arch,
58+
memory: {
59+
rss: memUsage.rss,
60+
heapTotal: memUsage.heapTotal,
61+
heapUsed: memUsage.heapUsed,
62+
external: memUsage.external,
63+
arrayBuffers: memUsage.arrayBuffers,
64+
},
65+
cpu: {
66+
user: cpuUsage.user,
67+
system: cpuUsage.system,
68+
},
69+
system: {
70+
totalMemory: os.totalmem(),
71+
freeMemory: os.freemem(),
72+
cpuCount: os.cpus().length,
73+
loadAverage: os.loadavg(),
74+
hostname: os.hostname(),
75+
},
76+
};
77+
}
78+
4879
static async provider(
4980
[express, graphql]: [ExpressMain, GraphqlMain],
5081
config: any,
5182
[diagnosticSlot]: [DiagnosticSlot]
5283
) {
5384
const diagnosticMain = new DiagnosticMain(diagnosticSlot);
54-
diagnosticMain.register({ diagnosticFn: DiagnosticMain.getBitVersion });
85+
diagnosticMain.register(
86+
{ diagnosticFn: DiagnosticMain.getBitVersion },
87+
{ diagnosticFn: DiagnosticMain.getProcessInfo }
88+
);
5589
express.register([new DiagnosticRoute(diagnosticMain)]);
5690
graphql.register(() => new DiagnosticGraphql(diagnosticMain));
5791
return diagnosticMain;

scopes/harmony/diagnostic/diagnostic.route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export class DiagnosticRoute implements Route {
77

88
method = 'GET';
99
route = '/_diagnostic';
10-
verb = Verb.READ;
10+
verb = Verb.WRITE;
1111

1212
middlewares = [
1313
async (req: Request, res: Response) => {
1414
const diagnosticData = this.diagnosticMain.getDiagnosticData();
15-
res.json(diagnosticData);
15+
return res.json(diagnosticData);
1616
},
1717
];
1818
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os from 'os';
2+
import { expect } from 'chai';
3+
import { DiagnosticMain } from './diagnostic.main.runtime';
4+
5+
describe('DiagnosticMain', () => {
6+
describe('getBitVersion', () => {
7+
it('should return an object with a version string', () => {
8+
const result = DiagnosticMain.getBitVersion();
9+
expect(result).to.have.property('version');
10+
expect(result.version).to.be.a('string');
11+
});
12+
});
13+
14+
describe('getProcessInfo', () => {
15+
it('should return process uptime as a positive number', () => {
16+
const result = DiagnosticMain.getProcessInfo();
17+
expect(result.uptime).to.be.a('number');
18+
expect(result.uptime).to.be.greaterThan(0);
19+
});
20+
21+
it('should return the current process pid', () => {
22+
const result = DiagnosticMain.getProcessInfo();
23+
expect(result.pid).to.equal(process.pid);
24+
});
25+
26+
it('should return node version, platform, and arch', () => {
27+
const result = DiagnosticMain.getProcessInfo();
28+
expect(result.nodeVersion).to.equal(process.version);
29+
expect(result.platform).to.equal(process.platform);
30+
expect(result.arch).to.equal(process.arch);
31+
});
32+
33+
it('should return memory usage with all expected fields', () => {
34+
const result = DiagnosticMain.getProcessInfo();
35+
expect(result.memory).to.have.property('rss').that.is.a('number');
36+
expect(result.memory).to.have.property('heapTotal').that.is.a('number');
37+
expect(result.memory).to.have.property('heapUsed').that.is.a('number');
38+
expect(result.memory).to.have.property('external').that.is.a('number');
39+
expect(result.memory).to.have.property('arrayBuffers').that.is.a('number');
40+
});
41+
42+
it('should return memory values that are positive', () => {
43+
const result = DiagnosticMain.getProcessInfo();
44+
expect(result.memory.rss).to.be.greaterThan(0);
45+
expect(result.memory.heapTotal).to.be.greaterThan(0);
46+
expect(result.memory.heapUsed).to.be.greaterThan(0);
47+
});
48+
49+
it('should return cpu usage with user and system fields', () => {
50+
const result = DiagnosticMain.getProcessInfo();
51+
expect(result.cpu).to.have.property('user').that.is.a('number');
52+
expect(result.cpu).to.have.property('system').that.is.a('number');
53+
});
54+
55+
it('should return system info matching os module values', () => {
56+
const result = DiagnosticMain.getProcessInfo();
57+
expect(result.system.totalMemory).to.equal(os.totalmem());
58+
expect(result.system.cpuCount).to.equal(os.cpus().length);
59+
expect(result.system.hostname).to.equal(os.hostname());
60+
});
61+
62+
it('should return load average as an array of 3 numbers', () => {
63+
const result = DiagnosticMain.getProcessInfo();
64+
expect(result.system.loadAverage).to.be.an('array').with.lengthOf(3);
65+
result.system.loadAverage.forEach((val) => {
66+
expect(val).to.be.a('number');
67+
});
68+
});
69+
70+
it('should return free memory less than or equal to total memory', () => {
71+
const result = DiagnosticMain.getProcessInfo();
72+
expect(result.system.freeMemory).to.be.at.most(result.system.totalMemory);
73+
expect(result.system.freeMemory).to.be.greaterThan(0);
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)