Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: fixing tests
  • Loading branch information
obecny committed Dec 9, 2020
commit 1739685861b015eaa68b4f348e74ad2c179d471e
4 changes: 2 additions & 2 deletions packages/opentelemetry-host-metrics/src/stats/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import * as os from 'os';
import { CpuUsageData, MemoryData } from '../types';

const MICROSECOND = 1 / 1e6;
let cpuUsageTime = 0;
let cpuUsageTime: number | undefined = undefined;

/**
* It returns cpu load delta from last time - to be used with SumObservers.
* When called first time it will return 0 and then delta will be calculated
*/
export function getCpuUsageData(): CpuUsageData {
if (!cpuUsageTime) {
if (typeof cpuUsageTime !== 'number') {
cpuUsageTime = new Date().getTime() - process.uptime() * 1000;
}

Expand Down
25 changes: 14 additions & 11 deletions packages/opentelemetry-host-metrics/test/metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('Host Metrics', () => {
});
sandbox.stub(os, 'totalmem').returns(mockedOS.totalmem());
sandbox.stub(process, 'cpuUsage').returns(cpuJson);
sandbox.stub(process, 'uptime').returns(0);
sandbox.stub(SI, 'networkStats').callsFake(() => {
return mockedSI.networkStats();
});
Expand All @@ -109,14 +110,16 @@ describe('Host Metrics', () => {
});
hostMetrics.start();

countSI = 0;

// sinon fake doesn't work fine with setImmediate
originalSetTimeout(() => {
// move the clock with the same value as interval
sandbox.clock.tick(INTERVAL * 2);
// move to "real" next tick so that async batcher observer will start
// processing metrics
originalSetTimeout(() => {
// allow all calbacks to finish correctly as they are finishing in
// allow all callbacks to finish correctly as they are finishing in
// next tick due to async
sandbox.clock.tick(1);
originalSetTimeout(() => {
Expand Down Expand Up @@ -152,15 +155,15 @@ describe('Host Metrics', () => {
assert.strictEqual(records.length, 3);
ensureValue(records[0], { state: 'user' }, 1.899243);
ensureValue(records[1], { state: 'system' }, 0.258553);
ensureValue(records[2], { state: 'idle' }, 0.842204);
ensureValue(records[2], { state: 'idle' }, 3.8422039999999997);
});

it('should export CPU utilization metrics', () => {
const records = getRecords(exportSpy.args[0][0], 'system.cpu.utilization');
assert.strictEqual(records.length, 3);
ensureValue(records[0], { state: 'user' }, 0.633081);
ensureValue(records[1], { state: 'system' }, 0.08618433333333332);
ensureValue(records[2], { state: 'idle' }, 0.28073466666666663);
ensureValue(records[0], { state: 'user' }, 0.3165405);
ensureValue(records[1], { state: 'system' }, 0.04309216666666666);
ensureValue(records[2], { state: 'idle' }, 0.6403673333333333);
});

it('should export Memory usage metrics', done => {
Expand All @@ -185,24 +188,24 @@ describe('Host Metrics', () => {
it('should export Network io dropped', done => {
const records = getRecords(exportSpy.args[0][0], 'system.network.dropped');
assert.strictEqual(records.length, 2);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 1200);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 12);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 2400);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 24);
done();
});

it('should export Network io errors', done => {
const records = getRecords(exportSpy.args[0][0], 'system.network.errors');
assert.strictEqual(records.length, 2);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 3);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 15);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 6);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 30);
done();
});

it('should export Network io bytes', done => {
const records = getRecords(exportSpy.args[0][0], 'system.network.io');
assert.strictEqual(records.length, 2);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 123123);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 321321);
ensureValue(records[0], { direction: 'receive', device: 'eth0' }, 246246);
ensureValue(records[1], { direction: 'transmit', device: 'eth0' }, 642642);
done();
});
});
Expand Down