-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(host-metrics)!: fix process.cpu.* metrics #1785
Merged
legendecas
merged 8 commits into
open-telemetry:main
from
david-luna:1718-fix-process-cpu-metrics
Jan 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
05f866f
fix(host-metrics): fix process.cpu.* metrics
david-luna 682d174
chore(host-metrics): adjust test data
david-luna cd39afb
Merge branch 'main' into 1718-fix-process-cpu-metrics
david-luna 705f2ed
chore(host-metrics): update attribute names
david-luna 6f5c422
chore(host-metrics): fix test
david-luna d56bef9
chore(host-metrics): fix test assertions
david-luna 8afe0ed
chore(host-metrics): lint fix
david-luna eb13e64
Merge branch 'main' into 1718-fix-process-cpu-metrics
david-luna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(host-metrics): fix process.cpu.* metrics
- Loading branch information
commit 05f866fff14b8ca738c6d9758dfe95fcc80e0db2
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import { METRIC_ATTRIBUTES } from '../src/enum'; | |
import { HostMetrics } from '../src'; | ||
|
||
const cpuJson = require('./mocks/cpu.json'); | ||
const processJson = require('./mocks/process.json'); | ||
const networkJson = require('./mocks/network.json'); | ||
|
||
class TestMetricReader extends MetricReader { | ||
|
@@ -75,7 +76,20 @@ const mockedOS = { | |
}, | ||
}; | ||
|
||
const INTERVAL = 3000; | ||
const mockedProcess = { | ||
uptime: function () { | ||
return 0; | ||
}, | ||
procIdx: 0, | ||
cpuUsage: function () { | ||
return processJson[this.procIdx++ % 2]; | ||
}, | ||
memoryUsage: { | ||
rss: function () { | ||
return 123456; | ||
}, | ||
}, | ||
}; | ||
|
||
describe('Host Metrics', () => { | ||
let meterProvider: MeterProvider; | ||
|
@@ -113,24 +127,17 @@ describe('Host Metrics', () => { | |
sandbox = sinon.createSandbox(); | ||
sandbox.useFakeTimers(); | ||
|
||
sandbox.stub(os, 'freemem').callsFake(() => { | ||
return mockedOS.freemem(); | ||
}); | ||
sandbox.stub(os, 'totalmem').returns(mockedOS.totalmem()); | ||
sandbox.stub(os, 'freemem').callsFake(mockedOS.freemem); | ||
sandbox.stub(os, 'totalmem').callsFake(mockedOS.totalmem); | ||
sandbox.stub(os, 'cpus').callsFake(() => mockedOS.cpus()); | ||
sandbox.stub(process, 'uptime').returns(0); | ||
sandbox.stub(SI, 'networkStats').callsFake(() => { | ||
return mockedSI.networkStats(); | ||
}); | ||
sandbox.stub(process, 'cpuUsage').callsFake(() => { | ||
return { | ||
user: 90713560, | ||
system: 63192630, | ||
}; | ||
}); | ||
sandbox.stub(process.memoryUsage, 'rss').callsFake(() => { | ||
return 123456; | ||
}); | ||
sandbox.stub(process, 'uptime').callsFake(mockedProcess.uptime); | ||
sandbox | ||
.stub(process, 'cpuUsage') | ||
.callsFake(() => mockedProcess.cpuUsage()); | ||
sandbox | ||
.stub(process.memoryUsage, 'rss') | ||
.callsFake(mockedProcess.memoryUsage.rss); | ||
sandbox.stub(SI, 'networkStats').callsFake(mockedSI.networkStats); | ||
|
||
reader = new TestMetricReader(); | ||
|
||
|
@@ -143,13 +150,9 @@ describe('Host Metrics', () => { | |
}); | ||
await hostMetrics.start(); | ||
|
||
const dateStub = sandbox | ||
.stub(Date.prototype, 'getTime') | ||
.returns(process.uptime() * 1000 + 1); | ||
// Drop first frame cpu metrics, see | ||
// src/common.ts getCpuUsageData | ||
// src/common.ts getCpuUsageData/getProcessCpuUsageData | ||
await reader.collect(); | ||
dateStub.returns(process.uptime() * 1000 + INTERVAL); | ||
|
||
// advance the clock for the next collection | ||
sandbox.clock.tick(1000); | ||
|
@@ -314,15 +317,15 @@ describe('Host Metrics', () => { | |
it('should export Process CPU time metrics', async () => { | ||
const metric = await getRecords(reader, 'process.cpu.time'); | ||
|
||
ensureValue(metric, { state: 'user' }, 90713.56); | ||
ensureValue(metric, { state: 'system' }, 63192.630000000005); | ||
ensureValue(metric, { state: 'user' }, 90.71356); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewer: values have just changed in the magnitude since according to NodeJS docs the usage is reported in microseconds and not milliseconds |
||
ensureValue(metric, { state: 'system' }, 63.192629999999994); | ||
}); | ||
|
||
it('should export Process CPU utilization metrics', async () => { | ||
const metric = await getRecords(reader, 'process.cpu.utilization'); | ||
|
||
ensureValue(metric, { state: 'user' }, 30247.935978659552); | ||
ensureValue(metric, { state: 'system' }, 21071.23374458153); | ||
ensureValue(metric, { state: 'user' }, 1.5); | ||
ensureValue(metric, { state: 'system' }, 1); | ||
}); | ||
|
||
it('should export Process Memory usage metrics', async () => { | ||
|
10 changes: 10 additions & 0 deletions
10
packages/opentelemetry-host-metrics/test/mocks/process.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"user": 87713560, | ||
"system": 61192630 | ||
}, | ||
{ | ||
"user": 90713560, | ||
"system": 63192630 | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewer: I don't know why there was this non null assertion in place. It does not affect the expression and also it was giving an lint warning so I've decided to remove it.