From 2c6d8cfbddac9ec906877a179ae6a3f62d78b831 Mon Sep 17 00:00:00 2001 From: Maikel Date: Sat, 30 Mar 2024 00:59:26 +0100 Subject: [PATCH 01/16] fix: semcov alignment & reduce import bundle --- packages/opentelemetry-host-metrics/src/enum.ts | 4 +--- packages/opentelemetry-host-metrics/src/stats/si.ts | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-host-metrics/src/enum.ts b/packages/opentelemetry-host-metrics/src/enum.ts index acfe4adc05..40ed0a0a39 100644 --- a/packages/opentelemetry-host-metrics/src/enum.ts +++ b/packages/opentelemetry-host-metrics/src/enum.ts @@ -34,9 +34,7 @@ export enum ATTRIBUTE_NAMES { SYSTEM_DEVICE = 'system.device', SYSTEM_NETWORK_DIRECTION = 'system.network.direction', SYSTEM_NETWORK_STATE = 'system.network.state', - // TODO: change value if semconv changes - // https://github.com/open-telemetry/opentelemetry-specification/issues/3776 - PROCESS_CPU_STATE = 'state', + PROCESS_CPU_STATE = 'process.cpu.state', } export enum CPU_LABELS { diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index f31543e694..d67bf5cd36 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -14,12 +14,12 @@ * limitations under the License. */ -import * as SI from 'systeminformation'; +import { networkStats } from 'systeminformation/lib/network'; import { NetworkData } from '../types'; export function getNetworkData() { return new Promise(resolve => { - SI.networkStats() + networkStats() .then(resolve) .catch(() => { resolve([]); From 60e093310a8015c37936747544f3432a78893a98 Mon Sep 17 00:00:00 2001 From: Maikel Date: Tue, 2 Apr 2024 16:08:38 +0200 Subject: [PATCH 02/16] fix: types? --- packages/opentelemetry-host-metrics/global.d.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/opentelemetry-host-metrics/global.d.ts diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts new file mode 100644 index 0000000000..cbeb743549 --- /dev/null +++ b/packages/opentelemetry-host-metrics/global.d.ts @@ -0,0 +1,5 @@ +/// + +declare module 'systeminformation/lib/network' { + export function networkStats(ifaces?: string, cb?: (data: Systeminformation.NetworkStatsData[]) => any): Promise; +} \ No newline at end of file From ae578fc2af24813f12c00a1c6d43800a04b30b64 Mon Sep 17 00:00:00 2001 From: Maikel Date: Tue, 2 Apr 2024 16:40:28 +0200 Subject: [PATCH 03/16] fix: ci/cd --- package-lock.json | 16 +-- .../opentelemetry-host-metrics/global.d.ts | 8 +- .../opentelemetry-host-metrics/package.json | 4 +- .../src/BaseMetrics.ts | 16 +-- .../opentelemetry-host-metrics/src/metric.ts | 120 +++++++++--------- .../src/stats/common.ts | 14 +- .../src/stats/si.ts | 4 +- .../opentelemetry-host-metrics/src/types.ts | 13 -- .../opentelemetry-host-metrics/src/util.ts | 2 +- .../opentelemetry-host-metrics/tsconfig.json | 5 +- 10 files changed, 94 insertions(+), 108 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0149d7bb23..05494be1ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32571,9 +32571,9 @@ } }, "node_modules/systeminformation": { - "version": "5.22.0", - "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.0.tgz", - "integrity": "sha512-oAP80ymt8ssrAzjX8k3frbL7ys6AotqC35oikG6/SG15wBw+tG9nCk4oPaXIhEaAOAZ8XngxUv3ORq2IuR3r4Q==", + "version": "5.22.7", + "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.7.tgz", + "integrity": "sha512-AWxlP05KeHbpGdgvZkcudJpsmChc2Y5Eo/GvxG/iUA/Aws5LZKHAMSeAo+V+nD+nxWZaxrwpWcnx4SH3oxNL3A==", "os": [ "darwin", "linux", @@ -36552,7 +36552,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/sdk-metrics": "^1.8.0", - "systeminformation": "^5.21.20" + "systeminformation": "^5.22.7" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", @@ -45621,7 +45621,7 @@ "nyc": "15.1.0", "rimraf": "5.0.5", "sinon": "15.2.0", - "systeminformation": "^5.21.20", + "systeminformation": "^5.22.7", "ts-mocha": "10.0.0", "typescript": "4.4.4" }, @@ -66740,9 +66740,9 @@ "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==" }, "systeminformation": { - "version": "5.22.0", - "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.0.tgz", - "integrity": "sha512-oAP80ymt8ssrAzjX8k3frbL7ys6AotqC35oikG6/SG15wBw+tG9nCk4oPaXIhEaAOAZ8XngxUv3ORq2IuR3r4Q==" + "version": "5.22.7", + "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.7.tgz", + "integrity": "sha512-AWxlP05KeHbpGdgvZkcudJpsmChc2Y5Eo/GvxG/iUA/Aws5LZKHAMSeAo+V+nD+nxWZaxrwpWcnx4SH3oxNL3A==" }, "table-layout": { "version": "3.0.2", diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts index cbeb743549..9943ad2931 100644 --- a/packages/opentelemetry-host-metrics/global.d.ts +++ b/packages/opentelemetry-host-metrics/global.d.ts @@ -1,5 +1,5 @@ -/// - declare module 'systeminformation/lib/network' { - export function networkStats(ifaces?: string, cb?: (data: Systeminformation.NetworkStatsData[]) => any): Promise; -} \ No newline at end of file + import type { Systeminformation } from 'systeminformation'; + + export function networkStats(ifaces?: string, cb?: (data: Systeminformation.NetworkStatsData[]) => any): Promise; +} diff --git a/packages/opentelemetry-host-metrics/package.json b/packages/opentelemetry-host-metrics/package.json index 0337703640..1939a63029 100644 --- a/packages/opentelemetry-host-metrics/package.json +++ b/packages/opentelemetry-host-metrics/package.json @@ -56,7 +56,7 @@ }, "dependencies": { "@opentelemetry/sdk-metrics": "^1.8.0", - "systeminformation": "^5.21.20" + "systeminformation": "^5.22.7" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/opentelemetry-host-metrics#readme" -} +} \ No newline at end of file diff --git a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts index fa9453ff22..46de1b65f1 100644 --- a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts +++ b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts @@ -14,17 +14,15 @@ * limitations under the License. */ -import * as api from '@opentelemetry/api'; -import * as metrics from '@opentelemetry/sdk-metrics'; - -import { VERSION } from './version'; +import { Meter, diag, metrics } from '@opentelemetry/api'; +import { MeterProvider } from '@opentelemetry/sdk-metrics'; /** * Metrics Collector Configuration */ export interface MetricsCollectorConfig { // Meter Provider - meterProvider?: metrics.MeterProvider; + meterProvider?: MeterProvider; // Character to be used to join metrics - default is "." metricNameSeparator?: string; // Name of component @@ -39,18 +37,18 @@ const DEFAULT_NAME = '@opentelemetry/host-metrics'; * Base Class for metrics */ export abstract class BaseMetrics { - protected _logger = api.diag; - protected _meter: api.Meter; + protected _logger = diag; + protected _meter: Meter; private _name: string; constructor(config: MetricsCollectorConfig) { this._name = config.name || DEFAULT_NAME; const meterProvider = - config.meterProvider || api.metrics.getMeterProvider(); + config.meterProvider || metrics.getMeterProvider(); if (!config.meterProvider) { this._logger.warn('No meter provider, using default'); } - this._meter = meterProvider.getMeter(this._name, VERSION); + this._meter = meterProvider.getMeter(this._name); } /** diff --git a/packages/opentelemetry-host-metrics/src/metric.ts b/packages/opentelemetry-host-metrics/src/metric.ts index 2604e95d00..e21646f0db 100644 --- a/packages/opentelemetry-host-metrics/src/metric.ts +++ b/packages/opentelemetry-host-metrics/src/metric.ts @@ -15,8 +15,8 @@ */ import { BaseMetrics } from './BaseMetrics'; -import * as api from '@opentelemetry/api'; -import * as enums from './enum'; +import { BatchObservableResult, ObservableCounter, ObservableGauge } from '@opentelemetry/api'; +import { ATTRIBUTE_NAMES, CPU_LABELS, MEMORY_LABELS, METRIC_NAMES, NETWORK_LABELS } from './enum'; import { getCpuUsageData, @@ -25,161 +25,161 @@ import { getProcessMemoryData, } from './stats/common'; import { getNetworkData } from './stats/si'; -import { +import type { CpuUsageData, MemoryData, - NetworkData, ProcessCpuUsageData, } from './types'; +import type { Systeminformation } from 'systeminformation'; /** * Metrics Collector - collects metrics for CPU, Memory, Network */ export class HostMetrics extends BaseMetrics { private _batchUpdateCpuUsages( - observableResult: api.BatchObservableResult, + observableResult: BatchObservableResult, cpuUsages: CpuUsageData[] ): void { - const stateAttr = enums.ATTRIBUTE_NAMES.SYSTEM_CPU_STATE; - const cpuAttr = enums.ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER; + const stateAttr = ATTRIBUTE_NAMES.SYSTEM_CPU_STATE; + const cpuAttr = ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER; for (let i = 0, j = cpuUsages.length; i < j; i++) { const cpuUsage = cpuUsages[i]; observableResult.observe(this._cpuTime, cpuUsage.user, { - [stateAttr]: enums.CPU_LABELS.USER, + [stateAttr]: CPU_LABELS.USER, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuTime, cpuUsage.system, { - [stateAttr]: enums.CPU_LABELS.SYSTEM, + [stateAttr]: CPU_LABELS.SYSTEM, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuTime, cpuUsage.idle, { - [stateAttr]: enums.CPU_LABELS.IDLE, + [stateAttr]: CPU_LABELS.IDLE, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuTime, cpuUsage.interrupt, { - [stateAttr]: enums.CPU_LABELS.INTERRUPT, + [stateAttr]: CPU_LABELS.INTERRUPT, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuTime, cpuUsage.nice, { - [stateAttr]: enums.CPU_LABELS.NICE, + [stateAttr]: CPU_LABELS.NICE, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuUtilization, cpuUsage.userP, { - [stateAttr]: enums.CPU_LABELS.USER, + [stateAttr]: CPU_LABELS.USER, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuUtilization, cpuUsage.systemP, { - [stateAttr]: enums.CPU_LABELS.SYSTEM, + [stateAttr]: CPU_LABELS.SYSTEM, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuUtilization, cpuUsage.idleP, { - [stateAttr]: enums.CPU_LABELS.IDLE, + [stateAttr]: CPU_LABELS.IDLE, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuUtilization, cpuUsage.interruptP, { - [stateAttr]: enums.CPU_LABELS.INTERRUPT, + [stateAttr]: CPU_LABELS.INTERRUPT, [cpuAttr]: cpuUsage.cpuNumber, }); observableResult.observe(this._cpuUtilization, cpuUsage.niceP, { - [stateAttr]: enums.CPU_LABELS.NICE, + [stateAttr]: CPU_LABELS.NICE, [cpuAttr]: cpuUsage.cpuNumber, }); } } private _batchUpdateProcessCpuUsages( - observableResult: api.BatchObservableResult, + observableResult: BatchObservableResult, processCpuUsage: ProcessCpuUsageData ): void { - const stateAttr = enums.ATTRIBUTE_NAMES.PROCESS_CPU_STATE; + const stateAttr = ATTRIBUTE_NAMES.PROCESS_CPU_STATE; observableResult.observe(this._processCpuTime, processCpuUsage.user, { - [stateAttr]: enums.CPU_LABELS.USER, + [stateAttr]: CPU_LABELS.USER, }); observableResult.observe(this._processCpuTime, processCpuUsage.system, { - [stateAttr]: enums.CPU_LABELS.SYSTEM, + [stateAttr]: CPU_LABELS.SYSTEM, }); observableResult.observe( this._processCpuUtilization, processCpuUsage.userP, { - [stateAttr]: enums.CPU_LABELS.USER, + [stateAttr]: CPU_LABELS.USER, } ); observableResult.observe( this._processCpuUtilization, processCpuUsage.systemP, { - [stateAttr]: enums.CPU_LABELS.SYSTEM, + [stateAttr]: CPU_LABELS.SYSTEM, } ); } private _batchUpdateMemUsages( - observableResult: api.BatchObservableResult, + observableResult: BatchObservableResult, memUsage: MemoryData ): void { - const stateAttr = enums.ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE; + const stateAttr = ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE; observableResult.observe(this._memoryUsage, memUsage.used, { - [stateAttr]: enums.MEMORY_LABELS.USED, + [stateAttr]: MEMORY_LABELS.USED, }); observableResult.observe(this._memoryUsage, memUsage.free, { - [stateAttr]: enums.MEMORY_LABELS.FREE, + [stateAttr]: MEMORY_LABELS.FREE, }); observableResult.observe(this._memoryUtilization, memUsage.usedP, { - [stateAttr]: enums.MEMORY_LABELS.USED, + [stateAttr]: MEMORY_LABELS.USED, }); observableResult.observe(this._memoryUtilization, memUsage.freeP, { - [stateAttr]: enums.MEMORY_LABELS.FREE, + [stateAttr]: MEMORY_LABELS.FREE, }); } private _batchUpdateProcessMemUsage( - observableResult: api.BatchObservableResult, + observableResult: BatchObservableResult, memoryUsage: number ): void { observableResult.observe(this._processMemoryUsage, memoryUsage); } private _batchUpdateNetworkData( - observableResult: api.BatchObservableResult, - networkUsages: NetworkData[] + observableResult: BatchObservableResult, + networkUsages: Systeminformation.NetworkStatsData[] ): void { - const deviceAttr = enums.ATTRIBUTE_NAMES.SYSTEM_DEVICE; - const directionAttr = enums.ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION; + const deviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE; + const directionAttr = ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION; for (let i = 0, j = networkUsages.length; i < j; i++) { const networkUsage = networkUsages[i]; observableResult.observe(this._networkDropped, networkUsage.rx_dropped, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.RECEIVE, + [directionAttr]: NETWORK_LABELS.RECEIVE, }); observableResult.observe(this._networkDropped, networkUsage.tx_dropped, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.TRANSMIT, + [directionAttr]: NETWORK_LABELS.TRANSMIT, }); observableResult.observe(this._networkErrors, networkUsage.rx_errors, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.RECEIVE, + [directionAttr]: NETWORK_LABELS.RECEIVE, }); observableResult.observe(this._networkErrors, networkUsage.tx_errors, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.TRANSMIT, + [directionAttr]: NETWORK_LABELS.TRANSMIT, }); observableResult.observe(this._networkIo, networkUsage.rx_bytes, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.RECEIVE, + [directionAttr]: NETWORK_LABELS.RECEIVE, }); observableResult.observe(this._networkIo, networkUsage.tx_bytes, { [deviceAttr]: networkUsage.iface, - [directionAttr]: enums.NETWORK_LABELS.TRANSMIT, + [directionAttr]: NETWORK_LABELS.TRANSMIT, }); } } @@ -189,66 +189,66 @@ export class HostMetrics extends BaseMetrics { */ protected _createMetrics(): void { this._cpuTime = this._meter.createObservableCounter( - enums.METRIC_NAMES.CPU_TIME, + METRIC_NAMES.CPU_TIME, { description: 'Cpu time in seconds', unit: 's', } ); this._cpuUtilization = this._meter.createObservableGauge( - enums.METRIC_NAMES.CPU_UTILIZATION, + METRIC_NAMES.CPU_UTILIZATION, { description: 'Cpu usage time 0-1', } ); this._memoryUsage = this._meter.createObservableGauge( - enums.METRIC_NAMES.MEMORY_USAGE, + METRIC_NAMES.MEMORY_USAGE, { description: 'Memory usage in bytes', } ); this._memoryUtilization = this._meter.createObservableGauge( - enums.METRIC_NAMES.MEMORY_UTILIZATION, + METRIC_NAMES.MEMORY_UTILIZATION, { description: 'Memory usage 0-1', } ); this._networkDropped = this._meter.createObservableCounter( - enums.METRIC_NAMES.NETWORK_DROPPED, + METRIC_NAMES.NETWORK_DROPPED, { description: 'Network dropped packets', } ); this._networkErrors = this._meter.createObservableCounter( - enums.METRIC_NAMES.NETWORK_ERRORS, + METRIC_NAMES.NETWORK_ERRORS, { description: 'Network errors counter', } ); this._networkIo = this._meter.createObservableCounter( - enums.METRIC_NAMES.NETWORK_IO, + METRIC_NAMES.NETWORK_IO, { description: 'Network transmit and received bytes', } ); this._processCpuTime = this._meter.createObservableCounter( - enums.METRIC_NAMES.PROCESS_CPU_TIME, + METRIC_NAMES.PROCESS_CPU_TIME, { description: 'Process Cpu time in seconds', unit: 's', } ); this._processCpuUtilization = this._meter.createObservableGauge( - enums.METRIC_NAMES.PROCESS_CPU_UTILIZATION, + METRIC_NAMES.PROCESS_CPU_UTILIZATION, { description: 'Process Cpu usage time 0-1', } ); this._processMemoryUsage = this._meter.createObservableGauge( - enums.METRIC_NAMES.PROCESS_MEMORY_USAGE, + METRIC_NAMES.PROCESS_MEMORY_USAGE, { description: 'Process Memory usage in bytes', } @@ -291,14 +291,14 @@ export class HostMetrics extends BaseMetrics { } // The metrics are created in `_createMetrics`. - private _cpuTime!: api.ObservableCounter; - private _cpuUtilization!: api.ObservableGauge; - private _memoryUsage!: api.ObservableGauge; - private _memoryUtilization!: api.ObservableGauge; - private _processCpuTime!: api.ObservableCounter; - private _processCpuUtilization!: api.ObservableGauge; - private _processMemoryUsage!: api.ObservableGauge; - private _networkDropped!: api.ObservableCounter; - private _networkErrors!: api.ObservableCounter; - private _networkIo!: api.ObservableCounter; + private _cpuTime!: ObservableCounter; + private _cpuUtilization!: ObservableGauge; + private _memoryUsage!: ObservableGauge; + private _memoryUtilization!: ObservableGauge; + private _processCpuTime!: ObservableCounter; + private _processCpuUtilization!: ObservableGauge; + private _processMemoryUsage!: ObservableGauge; + private _networkDropped!: ObservableCounter; + private _networkErrors!: ObservableCounter; + private _networkIo!: ObservableCounter; } diff --git a/packages/opentelemetry-host-metrics/src/stats/common.ts b/packages/opentelemetry-host-metrics/src/stats/common.ts index 6803905fb7..220dd619b2 100644 --- a/packages/opentelemetry-host-metrics/src/stats/common.ts +++ b/packages/opentelemetry-host-metrics/src/stats/common.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import * as os from 'os'; +import { type CpuInfo, cpus, totalmem, freemem } from 'os'; -import { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types'; +import type { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types'; const MILLISECOND = 1 / 1e3; const MICROSECOND = 1 / 1e6; @@ -25,9 +25,9 @@ const MICROSECOND = 1 / 1e6; * We get data as soon as we load the module so the 1st collect * of the metric already has valuable data to be sent. */ -let prevOsData: { time: number; cpus: os.CpuInfo[] } = { +let prevOsData: { time: number; cpus: CpuInfo[] } = { time: Date.now(), - cpus: os.cpus(), + cpus: cpus(), }; /** @@ -38,7 +38,7 @@ let prevOsData: { time: number; cpus: os.CpuInfo[] } = { export function getCpuUsageData(): CpuUsageData[] { const currentTime = Date.now(); const timeElapsed = currentTime - prevOsData.time; - const currentOsData = { time: currentTime, cpus: os.cpus() }; + const currentOsData = { time: currentTime, cpus: cpus() }; const usageData = currentOsData.cpus.map((cpu, cpuNumber) => { const prevTimes = prevOsData.cpus[cpuNumber].times; @@ -121,8 +121,8 @@ export function getProcessCpuUsageData(): ProcessCpuUsageData { * Returns memory data as absolute values */ export function getMemoryData(): MemoryData { - const total = os.totalmem(); - const free = os.freemem(); + const total = totalmem(); + const free = freemem(); const used = total - free; diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index d67bf5cd36..a25e1a65d0 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -15,10 +15,10 @@ */ import { networkStats } from 'systeminformation/lib/network'; -import { NetworkData } from '../types'; +import type { Systeminformation } from 'systeminformation'; export function getNetworkData() { - return new Promise(resolve => { + return new Promise(resolve => { networkStats() .then(resolve) .catch(() => { diff --git a/packages/opentelemetry-host-metrics/src/types.ts b/packages/opentelemetry-host-metrics/src/types.ts index 91d945012e..f0a7d983e4 100644 --- a/packages/opentelemetry-host-metrics/src/types.ts +++ b/packages/opentelemetry-host-metrics/src/types.ts @@ -14,19 +14,6 @@ * limitations under the License. */ -/** - * Network data - */ -export interface NetworkData { - iface: string; - rx_bytes: number; - rx_dropped: number; - rx_errors: number; - tx_bytes: number; - tx_dropped: number; - tx_errors: number; -} - /** * CPU usage data */ diff --git a/packages/opentelemetry-host-metrics/src/util.ts b/packages/opentelemetry-host-metrics/src/util.ts index e26b0f7610..341c6fff61 100644 --- a/packages/opentelemetry-host-metrics/src/util.ts +++ b/packages/opentelemetry-host-metrics/src/util.ts @@ -14,6 +14,6 @@ * limitations under the License. */ -export function ObjectKeys(t: T) { +export function ObjectKeys(t: T) { return Object.keys(t) as (keyof T)[]; } diff --git a/packages/opentelemetry-host-metrics/tsconfig.json b/packages/opentelemetry-host-metrics/tsconfig.json index 4078877ce6..73128b74c5 100644 --- a/packages/opentelemetry-host-metrics/tsconfig.json +++ b/packages/opentelemetry-host-metrics/tsconfig.json @@ -6,6 +6,7 @@ }, "include": [ "src/**/*.ts", - "test/**/*.ts" + "test/**/*.ts", + "global.d.ts" ] -} +} \ No newline at end of file From 1106e97c09c2dbf5403490927f35bac41712e29f Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 4 Apr 2024 22:28:59 +0200 Subject: [PATCH 04/16] docs: specify semantic conventions --- packages/opentelemetry-host-metrics/README.md | 51 +++++++++++++++---- .../opentelemetry-host-metrics/src/enum.ts | 2 +- .../opentelemetry-host-metrics/src/metric.ts | 2 +- .../test/metric.test.ts | 6 +-- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/packages/opentelemetry-host-metrics/README.md b/packages/opentelemetry-host-metrics/README.md index d9635f1acf..da0987da5f 100644 --- a/packages/opentelemetry-host-metrics/README.md +++ b/packages/opentelemetry-host-metrics/README.md @@ -20,30 +20,63 @@ npm install --save @opentelemetry/host-metrics ## Usage ```javascript -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); -const { HostMetrics } = require('@opentelemetry/host-metrics'); -const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); +const { MeterProvider } | require('@opentelemetry/sdk-metrics'); +const { HostMetrics } | require('@opentelemetry/host-metrics'); +const { PrometheusExporter } | require('@opentelemetry/exporter-prometheus'); -const exporter = new PrometheusExporter( +const exporter | new PrometheusExporter( { startServer: true - }, () => { + }| () => { console.log('prometheus scrape endpoint: http://localhost:9464/metrics') } ); -const meterProvider = new MeterProvider(); +const meterProvider | new MeterProvider(); meterProvider.addMetricReader(exporter); -const hostMetrics = new HostMetrics({ meterProvider, name: 'example-host-metrics' }); +const hostMetrics | new HostMetrics({ meterProvider| name: 'example-host-metrics' }); hostMetrics.start(); ``` +## Semantic Conventions + +This package uses Semantic Conventions [Version 1.25.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.25.0/docs/system). +As for now the Semantic Conventions are bundled in this package but eventually will be imported from `@opentelemetry/semantic-conventions` package when it is updated to latest version. +Ref: [opentelemetry-js/issues/4235](https://github.com/open-telemetry/opentelemetry-js/issues/4235) + +Metrics collected: + +| Metric | Short Description | Notes | +| --------------------------- | --------------------------------------------------------- | ----- | +| `system.cpu.time` | Seconds each logical CPU spent on each mode | | +| `system.cpu.utilization` | Difference in system.cpu.time since the last measurement | | +| `system.memory.usage` | Reports memory in use by state | | +| `system.memory.utilization` | | | +| `system.network.dropped` | Count of packets that are dropped | | +| `system.network.errors` | Count of network errors detected | | +| `system.network.io` | | | +| `process.cpu.time` | Total CPU seconds | | +| `process.cpu.utilization` | Difference in process.cpu.time since the last measurement | | +| `process.memory.usage` | The amount of physical memory in use | | + +Attributes collected: + +| Metric | Short Description | Notes | +| --------------------------- | ---------------------------------- | ----- | +| `system.cpu.logical_number` | The logical CPU number | | +| `system.cpu.state` | The state of the CPU | | +| `system.memory.state` | The memory state | | +| `system.device` | The device identifier | | +| `network.io.direction` | The network IO operation direction | | +| `system.network.state` | The network state | | +| `process.cpu.state` | The CPU state | | + ## Useful links -* For more information on OpenTelemetry, visit: +* For more information on OpenTelemetry| visit: * For more about OpenTelemetry JavaScript: -* For help or feedback on this project, join us in [GitHub Discussions][discussions-url] +* For help or feedback on this project| join us in [GitHub Discussions][discussions-url] ## License diff --git a/packages/opentelemetry-host-metrics/src/enum.ts b/packages/opentelemetry-host-metrics/src/enum.ts index 40ed0a0a39..581085d5bf 100644 --- a/packages/opentelemetry-host-metrics/src/enum.ts +++ b/packages/opentelemetry-host-metrics/src/enum.ts @@ -32,7 +32,7 @@ export enum ATTRIBUTE_NAMES { SYSTEM_CPU_STATE = 'system.cpu.state', SYSTEM_MEMORY_STATE = 'system.memory.state', SYSTEM_DEVICE = 'system.device', - SYSTEM_NETWORK_DIRECTION = 'system.network.direction', + NETWORK_NETWORK_DIRECTION = 'network.io.direction', SYSTEM_NETWORK_STATE = 'system.network.state', PROCESS_CPU_STATE = 'process.cpu.state', } diff --git a/packages/opentelemetry-host-metrics/src/metric.ts b/packages/opentelemetry-host-metrics/src/metric.ts index e21646f0db..e67f5f3b0d 100644 --- a/packages/opentelemetry-host-metrics/src/metric.ts +++ b/packages/opentelemetry-host-metrics/src/metric.ts @@ -151,7 +151,7 @@ export class HostMetrics extends BaseMetrics { networkUsages: Systeminformation.NetworkStatsData[] ): void { const deviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE; - const directionAttr = ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION; + const directionAttr = ATTRIBUTE_NAMES.NETWORK_NETWORK_DIRECTION; for (let i = 0, j = networkUsages.length; i < j; i++) { const networkUsage = networkUsages[i]; diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 196e74e702..7d54739005 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -38,8 +38,8 @@ class TestMetricReader extends MetricReader { public override selectAggregationTemporality(): AggregationTemporality { return AggregationTemporality.CUMULATIVE; } - protected async onForceFlush(): Promise {} - protected async onShutdown(): Promise {} + protected async onForceFlush(): Promise { } + protected async onShutdown(): Promise { } } let countSI = 0; @@ -169,7 +169,7 @@ describe('Host Metrics', () => { const sysCpuNumAttr = ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER; const sysMemStateAttr = ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE; const sysDeviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE; - const sysNetDirAttr = ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION; + const sysNetDirAttr = ATTRIBUTE_NAMES.NETWORK_NETWORK_DIRECTION; const procCpuStateAttr = ATTRIBUTE_NAMES.PROCESS_CPU_STATE; it('should export CPU time metrics', async () => { From 8f655203be3972bfe572c0868c42e3f31803a1d3 Mon Sep 17 00:00:00 2001 From: Maikel Date: Fri, 5 Apr 2024 13:58:22 +0200 Subject: [PATCH 05/16] revert some changes --- packages/opentelemetry-host-metrics/README.md | 18 +++++++++--------- .../src/BaseMetrics.ts | 4 +++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/opentelemetry-host-metrics/README.md b/packages/opentelemetry-host-metrics/README.md index da0987da5f..411fc17c98 100644 --- a/packages/opentelemetry-host-metrics/README.md +++ b/packages/opentelemetry-host-metrics/README.md @@ -20,22 +20,22 @@ npm install --save @opentelemetry/host-metrics ## Usage ```javascript -const { MeterProvider } | require('@opentelemetry/sdk-metrics'); -const { HostMetrics } | require('@opentelemetry/host-metrics'); -const { PrometheusExporter } | require('@opentelemetry/exporter-prometheus'); +const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { HostMetrics } = require('@opentelemetry/host-metrics'); +const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); -const exporter | new PrometheusExporter( +const exporter = new PrometheusExporter( { startServer: true - }| () => { + }, () => { console.log('prometheus scrape endpoint: http://localhost:9464/metrics') } ); -const meterProvider | new MeterProvider(); +const meterProvider = new MeterProvider(); meterProvider.addMetricReader(exporter); -const hostMetrics | new HostMetrics({ meterProvider| name: 'example-host-metrics' }); +const hostMetrics = new HostMetrics({ meterProvider, name: 'example-host-metrics' }); hostMetrics.start(); ``` @@ -74,9 +74,9 @@ Attributes collected: ## Useful links -* For more information on OpenTelemetry| visit: +* For more information on OpenTelemetry, visit: * For more about OpenTelemetry JavaScript: -* For help or feedback on this project| join us in [GitHub Discussions][discussions-url] +* For help or feedback on this project, join us in [GitHub Discussions][discussions-url] ## License diff --git a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts index 46de1b65f1..e92963a4a3 100644 --- a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts +++ b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts @@ -17,6 +17,8 @@ import { Meter, diag, metrics } from '@opentelemetry/api'; import { MeterProvider } from '@opentelemetry/sdk-metrics'; +import { VERSION } from './version'; + /** * Metrics Collector Configuration */ @@ -48,7 +50,7 @@ export abstract class BaseMetrics { if (!config.meterProvider) { this._logger.warn('No meter provider, using default'); } - this._meter = meterProvider.getMeter(this._name); + this._meter = meterProvider.getMeter(this._name, VERSION); } /** From 63cc39d95682cdc9e9cfa8f915ea2fd3158112f5 Mon Sep 17 00:00:00 2001 From: Maikel Date: Tue, 9 Apr 2024 11:32:45 +0200 Subject: [PATCH 06/16] fix: small type improvement --- packages/opentelemetry-host-metrics/global.d.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts index 9943ad2931..f7a9a601f0 100644 --- a/packages/opentelemetry-host-metrics/global.d.ts +++ b/packages/opentelemetry-host-metrics/global.d.ts @@ -1,5 +1,3 @@ declare module 'systeminformation/lib/network' { - import type { Systeminformation } from 'systeminformation'; - - export function networkStats(ifaces?: string, cb?: (data: Systeminformation.NetworkStatsData[]) => any): Promise; + export { networkStats } from 'systeminformation'; } From 03e22378ef172c349b02987e79e72f50e186f731 Mon Sep 17 00:00:00 2001 From: Maikel Date: Tue, 9 Apr 2024 12:24:17 +0200 Subject: [PATCH 07/16] docs: add small comment --- packages/opentelemetry-host-metrics/src/stats/si.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index a25e1a65d0..021ae5e727 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -14,6 +14,9 @@ * limitations under the License. */ +// Import from network file directly as importing from the root imports the /lib/cpu file, +// resulting in also having to add osx-temperature-sensor as a dependency for macOS, +// while /lib/cpu isn't even used by this package (deep-importing not working as expected) import { networkStats } from 'systeminformation/lib/network'; import type { Systeminformation } from 'systeminformation'; From 744ea1d0b1d1d7c4fbe6de0a930fe41936b124fd Mon Sep 17 00:00:00 2001 From: Maikel Date: Tue, 16 Apr 2024 22:02:59 +0200 Subject: [PATCH 08/16] fix: feedback --- packages/opentelemetry-host-metrics/README.md | 24 +++++++++---------- .../opentelemetry-host-metrics/src/enum.ts | 2 +- .../opentelemetry-host-metrics/src/metric.ts | 2 +- .../test/metric.test.ts | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/opentelemetry-host-metrics/README.md b/packages/opentelemetry-host-metrics/README.md index 411fc17c98..e40099b4d8 100644 --- a/packages/opentelemetry-host-metrics/README.md +++ b/packages/opentelemetry-host-metrics/README.md @@ -47,18 +47,18 @@ Ref: [opentelemetry-js/issues/4235](https://github.com/open-telemetry/openteleme Metrics collected: -| Metric | Short Description | Notes | -| --------------------------- | --------------------------------------------------------- | ----- | -| `system.cpu.time` | Seconds each logical CPU spent on each mode | | -| `system.cpu.utilization` | Difference in system.cpu.time since the last measurement | | -| `system.memory.usage` | Reports memory in use by state | | -| `system.memory.utilization` | | | -| `system.network.dropped` | Count of packets that are dropped | | -| `system.network.errors` | Count of network errors detected | | -| `system.network.io` | | | -| `process.cpu.time` | Total CPU seconds | | -| `process.cpu.utilization` | Difference in process.cpu.time since the last measurement | | -| `process.memory.usage` | The amount of physical memory in use | | +| Metric | Short Description | +| --------------------------- | --------------------------------------------------------- | +| `system.cpu.time` | Seconds each logical CPU spent on each mode | +| `system.cpu.utilization` | Difference in system.cpu.time since the last measurement | +| `system.memory.usage` | Reports memory in use by state | +| `system.memory.utilization` | | +| `system.network.dropped` | Count of packets that are dropped | +| `system.network.errors` | Count of network errors detected | +| `system.network.io` | | +| `process.cpu.time` | Total CPU seconds | +| `process.cpu.utilization` | Difference in process.cpu.time since the last measurement | +| `process.memory.usage` | The amount of physical memory in use | Attributes collected: diff --git a/packages/opentelemetry-host-metrics/src/enum.ts b/packages/opentelemetry-host-metrics/src/enum.ts index 581085d5bf..19e4efbda4 100644 --- a/packages/opentelemetry-host-metrics/src/enum.ts +++ b/packages/opentelemetry-host-metrics/src/enum.ts @@ -32,7 +32,7 @@ export enum ATTRIBUTE_NAMES { SYSTEM_CPU_STATE = 'system.cpu.state', SYSTEM_MEMORY_STATE = 'system.memory.state', SYSTEM_DEVICE = 'system.device', - NETWORK_NETWORK_DIRECTION = 'network.io.direction', + NETWORK_IO_DIRECTION = 'network.io.direction', SYSTEM_NETWORK_STATE = 'system.network.state', PROCESS_CPU_STATE = 'process.cpu.state', } diff --git a/packages/opentelemetry-host-metrics/src/metric.ts b/packages/opentelemetry-host-metrics/src/metric.ts index e67f5f3b0d..7c3d8abd97 100644 --- a/packages/opentelemetry-host-metrics/src/metric.ts +++ b/packages/opentelemetry-host-metrics/src/metric.ts @@ -151,7 +151,7 @@ export class HostMetrics extends BaseMetrics { networkUsages: Systeminformation.NetworkStatsData[] ): void { const deviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE; - const directionAttr = ATTRIBUTE_NAMES.NETWORK_NETWORK_DIRECTION; + const directionAttr = ATTRIBUTE_NAMES.NETWORK_IO_DIRECTION; for (let i = 0, j = networkUsages.length; i < j; i++) { const networkUsage = networkUsages[i]; diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 7d54739005..118cf5bb50 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -169,7 +169,7 @@ describe('Host Metrics', () => { const sysCpuNumAttr = ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER; const sysMemStateAttr = ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE; const sysDeviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE; - const sysNetDirAttr = ATTRIBUTE_NAMES.NETWORK_NETWORK_DIRECTION; + const sysNetDirAttr = ATTRIBUTE_NAMES.NETWORK_IO_DIRECTION; const procCpuStateAttr = ATTRIBUTE_NAMES.PROCESS_CPU_STATE; it('should export CPU time metrics', async () => { From a8196e48d941461854b0ff6b5ed7f171fdf13872 Mon Sep 17 00:00:00 2001 From: Maikel Date: Sat, 20 Apr 2024 16:17:01 +0200 Subject: [PATCH 09/16] fix: feedback --- .../opentelemetry-host-metrics/global.d.ts | 15 +++++++++ .../src/BaseMetrics.ts | 3 +- .../opentelemetry-host-metrics/src/metric.ts | 31 ++++++++++--------- .../src/stats/common.ts | 4 +-- .../test/metric.test.ts | 7 +++-- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts index f7a9a601f0..19cc54f99a 100644 --- a/packages/opentelemetry-host-metrics/global.d.ts +++ b/packages/opentelemetry-host-metrics/global.d.ts @@ -1,3 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ declare module 'systeminformation/lib/network' { export { networkStats } from 'systeminformation'; } diff --git a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts index e92963a4a3..a6bf68df8e 100644 --- a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts +++ b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts @@ -45,8 +45,7 @@ export abstract class BaseMetrics { constructor(config: MetricsCollectorConfig) { this._name = config.name || DEFAULT_NAME; - const meterProvider = - config.meterProvider || metrics.getMeterProvider(); + const meterProvider = config.meterProvider || metrics.getMeterProvider(); if (!config.meterProvider) { this._logger.warn('No meter provider, using default'); } diff --git a/packages/opentelemetry-host-metrics/src/metric.ts b/packages/opentelemetry-host-metrics/src/metric.ts index 7c3d8abd97..09a51cf9d2 100644 --- a/packages/opentelemetry-host-metrics/src/metric.ts +++ b/packages/opentelemetry-host-metrics/src/metric.ts @@ -15,8 +15,18 @@ */ import { BaseMetrics } from './BaseMetrics'; -import { BatchObservableResult, ObservableCounter, ObservableGauge } from '@opentelemetry/api'; -import { ATTRIBUTE_NAMES, CPU_LABELS, MEMORY_LABELS, METRIC_NAMES, NETWORK_LABELS } from './enum'; +import { + BatchObservableResult, + ObservableCounter, + ObservableGauge, +} from '@opentelemetry/api'; +import { + ATTRIBUTE_NAMES, + CPU_LABELS, + MEMORY_LABELS, + METRIC_NAMES, + NETWORK_LABELS, +} from './enum'; import { getCpuUsageData, @@ -25,11 +35,7 @@ import { getProcessMemoryData, } from './stats/common'; import { getNetworkData } from './stats/si'; -import type { - CpuUsageData, - MemoryData, - ProcessCpuUsageData, -} from './types'; +import type { CpuUsageData, MemoryData, ProcessCpuUsageData } from './types'; import type { Systeminformation } from 'systeminformation'; /** @@ -188,13 +194,10 @@ export class HostMetrics extends BaseMetrics { * Creates metrics */ protected _createMetrics(): void { - this._cpuTime = this._meter.createObservableCounter( - METRIC_NAMES.CPU_TIME, - { - description: 'Cpu time in seconds', - unit: 's', - } - ); + this._cpuTime = this._meter.createObservableCounter(METRIC_NAMES.CPU_TIME, { + description: 'Cpu time in seconds', + unit: 's', + }); this._cpuUtilization = this._meter.createObservableGauge( METRIC_NAMES.CPU_UTILIZATION, { diff --git a/packages/opentelemetry-host-metrics/src/stats/common.ts b/packages/opentelemetry-host-metrics/src/stats/common.ts index 220dd619b2..7b709f00e3 100644 --- a/packages/opentelemetry-host-metrics/src/stats/common.ts +++ b/packages/opentelemetry-host-metrics/src/stats/common.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { type CpuInfo, cpus, totalmem, freemem } from 'os'; - +import { cpus, totalmem, freemem } from 'os'; +import type { CpuInfo } from 'os'; import type { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types'; const MILLISECOND = 1 / 1e3; diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 118cf5bb50..bb7b4c2eab 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -const SI = require('systeminformation'); +import * as Network from 'systeminformation/lib/network'; +import type { Systeminformation } from 'systeminformation'; import { Attributes } from '@opentelemetry/api'; import { AggregationTemporality, @@ -45,7 +46,7 @@ class TestMetricReader extends MetricReader { let countSI = 0; const mockedSI = { networkStats: function () { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { countSI++; const stats: any[] = networkJson .slice() @@ -137,7 +138,7 @@ describe('Host Metrics', () => { sandbox .stub(process.memoryUsage, 'rss') .callsFake(mockedProcess.memoryUsage.rss); - sandbox.stub(SI, 'networkStats').callsFake(mockedSI.networkStats); + sandbox.stub(Network, 'networkStats').callsFake(mockedSI.networkStats); reader = new TestMetricReader(); From 59223342aab84c4a12f975ad3fe35adaf1397a33 Mon Sep 17 00:00:00 2001 From: Maikel Date: Wed, 22 May 2024 21:40:07 +0200 Subject: [PATCH 10/16] fix: feedback --- packages/opentelemetry-host-metrics/README.md | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/opentelemetry-host-metrics/README.md b/packages/opentelemetry-host-metrics/README.md index 658732ad17..f6fe4012b5 100644 --- a/packages/opentelemetry-host-metrics/README.md +++ b/packages/opentelemetry-host-metrics/README.md @@ -50,27 +50,27 @@ Metrics collected: | Metric | Short Description | | --------------------------- | --------------------------------------------------------- | | `system.cpu.time` | Seconds each logical CPU spent on each mode | -| `system.cpu.utilization` | Difference in system.cpu.time since the last measurement | +| `system.cpu.utilization` | CPU usage time (0-1) | | `system.memory.usage` | Reports memory in use by state | -| `system.memory.utilization` | | +| `system.memory.utilization` | Memory usage (0-1) | | `system.network.dropped` | Count of packets that are dropped | | `system.network.errors` | Count of network errors detected | -| `system.network.io` | | +| `system.network.io` | Network flow direction | | `process.cpu.time` | Total CPU seconds | | `process.cpu.utilization` | Difference in process.cpu.time since the last measurement | | `process.memory.usage` | The amount of physical memory in use | Attributes collected: -| Metric | Short Description | Notes | -| --------------------------- | ---------------------------------- | ----- | -| `system.cpu.logical_number` | The logical CPU number | | -| `system.cpu.state` | The state of the CPU | | -| `system.memory.state` | The memory state | | -| `system.device` | The device identifier | | -| `network.io.direction` | The network IO operation direction | | -| `system.network.state` | The network state | | -| `process.cpu.state` | The CPU state | | +| Metric | Short Description | +| --------------------------- | ---------------------------------- | +| `system.cpu.logical_number` | The logical CPU number | +| `system.cpu.state` | The state of the CPU | +| `system.memory.state` | The memory state | +| `system.device` | The device identifier | +| `network.io.direction` | The network IO operation direction | +| `system.network.state` | The network state | +| `process.cpu.state` | The CPU state | ## Useful links From f67581166f3b7e5cb8ee7576d6be38dffe33c411 Mon Sep 17 00:00:00 2001 From: Maikel Date: Fri, 24 May 2024 15:20:21 +0200 Subject: [PATCH 11/16] fix: remove subpath --- .../opentelemetry-host-metrics/global.d.ts | 18 --------- .../src/stats/si.ts | 5 +-- .../opentelemetry-host-metrics/src/util.ts | 2 +- .../test/metric.test.ts | 37 ++++++++++--------- .../opentelemetry-host-metrics/tsconfig.json | 3 +- 5 files changed, 22 insertions(+), 43 deletions(-) delete mode 100644 packages/opentelemetry-host-metrics/global.d.ts diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts deleted file mode 100644 index 19cc54f99a..0000000000 --- a/packages/opentelemetry-host-metrics/global.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -declare module 'systeminformation/lib/network' { - export { networkStats } from 'systeminformation'; -} diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index 021ae5e727..71c6c8c285 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -14,10 +14,7 @@ * limitations under the License. */ -// Import from network file directly as importing from the root imports the /lib/cpu file, -// resulting in also having to add osx-temperature-sensor as a dependency for macOS, -// while /lib/cpu isn't even used by this package (deep-importing not working as expected) -import { networkStats } from 'systeminformation/lib/network'; +import { networkStats } from 'systeminformation'; import type { Systeminformation } from 'systeminformation'; export function getNetworkData() { diff --git a/packages/opentelemetry-host-metrics/src/util.ts b/packages/opentelemetry-host-metrics/src/util.ts index 341c6fff61..05a6622c30 100644 --- a/packages/opentelemetry-host-metrics/src/util.ts +++ b/packages/opentelemetry-host-metrics/src/util.ts @@ -14,6 +14,6 @@ * limitations under the License. */ -export function ObjectKeys(t: T) { +export function ObjectKeys>(t: T) { return Object.keys(t) as (keyof T)[]; } diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index bb7b4c2eab..1ed10bec03 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import * as Network from 'systeminformation/lib/network'; +import * as SI from 'systeminformation'; import type { Systeminformation } from 'systeminformation'; import { Attributes } from '@opentelemetry/api'; import { @@ -46,21 +46,23 @@ class TestMetricReader extends MetricReader { let countSI = 0; const mockedSI = { networkStats: function () { - return new Promise((resolve, reject) => { - countSI++; - const stats: any[] = networkJson - .slice() - .map((obj: any) => Object.assign({}, obj)); - - for (let i = 0, j = networkJson.length; i < j; i++) { - Object.keys(stats[i]).forEach(key => { - if (typeof stats[i][key] === 'number' && stats[i][key] > 0) { - stats[i][key] = stats[i][key] * countSI; - } - }); + return new Promise( + (resolve, reject) => { + countSI++; + const stats: any[] = networkJson + .slice() + .map((obj: any) => Object.assign({}, obj)); + + for (let i = 0, j = networkJson.length; i < j; i++) { + Object.keys(stats[i]).forEach(key => { + if (typeof stats[i][key] === 'number' && stats[i][key] > 0) { + stats[i][key] = stats[i][key] * countSI; + } + }); + } + resolve(stats); } - resolve(stats); - }); + ); }, }; @@ -138,12 +140,11 @@ describe('Host Metrics', () => { sandbox .stub(process.memoryUsage, 'rss') .callsFake(mockedProcess.memoryUsage.rss); - sandbox.stub(Network, 'networkStats').callsFake(mockedSI.networkStats); + sandbox.stub(SI, 'networkStats').callsFake(mockedSI.networkStats); reader = new TestMetricReader(); - meterProvider = new MeterProvider(); - meterProvider.addMetricReader(reader); + meterProvider = new MeterProvider({ readers: [reader] }); hostMetrics = new HostMetrics({ meterProvider, diff --git a/packages/opentelemetry-host-metrics/tsconfig.json b/packages/opentelemetry-host-metrics/tsconfig.json index 73128b74c5..e5f9ff1579 100644 --- a/packages/opentelemetry-host-metrics/tsconfig.json +++ b/packages/opentelemetry-host-metrics/tsconfig.json @@ -6,7 +6,6 @@ }, "include": [ "src/**/*.ts", - "test/**/*.ts", - "global.d.ts" + "test/**/*.ts" ] } \ No newline at end of file From 72ba6f95b356a0c92c9468db646d5f8d3bfc7e68 Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 30 May 2024 18:28:00 +0200 Subject: [PATCH 12/16] feedback --- package-lock.json | 16 ++++++++-------- .../opentelemetry-host-metrics/global.d.ts | 18 ++++++++++++++++++ .../opentelemetry-host-metrics/package.json | 2 +- .../src/stats/common.ts | 4 ++-- .../opentelemetry-host-metrics/src/stats/si.ts | 6 +++++- .../test/metric.test.ts | 6 +++--- .../opentelemetry-host-metrics/tsconfig.json | 3 ++- 7 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 packages/opentelemetry-host-metrics/global.d.ts diff --git a/package-lock.json b/package-lock.json index a14147b85a..97f0a27af3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32709,9 +32709,9 @@ } }, "node_modules/systeminformation": { - "version": "5.22.7", - "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.7.tgz", - "integrity": "sha512-AWxlP05KeHbpGdgvZkcudJpsmChc2Y5Eo/GvxG/iUA/Aws5LZKHAMSeAo+V+nD+nxWZaxrwpWcnx4SH3oxNL3A==", + "version": "5.22.9", + "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.9.tgz", + "integrity": "sha512-qUWJhQ9JSBhdjzNUQywpvc0icxUAjMY3sZqUoS0GOtaJV9Ijq8s9zEP8Gaqmymn1dOefcICyPXK1L3kgKxlUpg==", "os": [ "darwin", "linux", @@ -36628,7 +36628,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/sdk-metrics": "^1.8.0", - "systeminformation": "^5.22.7" + "systeminformation": "5.22.9" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", @@ -46594,7 +46594,7 @@ "nyc": "15.1.0", "rimraf": "5.0.5", "sinon": "15.2.0", - "systeminformation": "^5.22.7", + "systeminformation": "5.22.9", "ts-mocha": "10.0.0", "typescript": "4.4.4" }, @@ -67864,9 +67864,9 @@ "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==" }, "systeminformation": { - "version": "5.22.7", - "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.7.tgz", - "integrity": "sha512-AWxlP05KeHbpGdgvZkcudJpsmChc2Y5Eo/GvxG/iUA/Aws5LZKHAMSeAo+V+nD+nxWZaxrwpWcnx4SH3oxNL3A==" + "version": "5.22.9", + "resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.22.9.tgz", + "integrity": "sha512-qUWJhQ9JSBhdjzNUQywpvc0icxUAjMY3sZqUoS0GOtaJV9Ijq8s9zEP8Gaqmymn1dOefcICyPXK1L3kgKxlUpg==" }, "table-layout": { "version": "3.0.2", diff --git a/packages/opentelemetry-host-metrics/global.d.ts b/packages/opentelemetry-host-metrics/global.d.ts new file mode 100644 index 0000000000..19cc54f99a --- /dev/null +++ b/packages/opentelemetry-host-metrics/global.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +declare module 'systeminformation/lib/network' { + export { networkStats } from 'systeminformation'; +} diff --git a/packages/opentelemetry-host-metrics/package.json b/packages/opentelemetry-host-metrics/package.json index 2f9a0b169a..1e5401c557 100644 --- a/packages/opentelemetry-host-metrics/package.json +++ b/packages/opentelemetry-host-metrics/package.json @@ -56,7 +56,7 @@ }, "dependencies": { "@opentelemetry/sdk-metrics": "^1.8.0", - "systeminformation": "^5.22.7" + "systeminformation": "5.22.9" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/opentelemetry-host-metrics#readme" } \ No newline at end of file diff --git a/packages/opentelemetry-host-metrics/src/stats/common.ts b/packages/opentelemetry-host-metrics/src/stats/common.ts index 7b709f00e3..341d4ee2a7 100644 --- a/packages/opentelemetry-host-metrics/src/stats/common.ts +++ b/packages/opentelemetry-host-metrics/src/stats/common.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { cpus, totalmem, freemem } from 'os'; -import type { CpuInfo } from 'os'; +import { cpus, totalmem, freemem } from 'node:os'; +import type { CpuInfo } from 'node:os'; import type { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types'; const MILLISECOND = 1 / 1e3; diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index 71c6c8c285..3d1aaabef3 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { networkStats } from 'systeminformation'; + +// Import from the network file directly as bundlers trigger the 'osx-temperature-sensor' import in the systeminformation/lib/cpu.js, +// resulting in the following warning: "Can't resolve 'osx-temperature-sensor'" +// See https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2071 +import { networkStats } from 'systeminformation/lib/network'; import type { Systeminformation } from 'systeminformation'; export function getNetworkData() { diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 6ff9a24b4c..3ff5600cac 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import * as SI from 'systeminformation'; +import * as Network from 'systeminformation/lib/network'; import type { Systeminformation } from 'systeminformation'; import { Attributes } from '@opentelemetry/api'; import { @@ -26,7 +26,7 @@ import { MetricReader, } from '@opentelemetry/sdk-metrics'; import * as assert from 'assert'; -import * as os from 'os'; +import * as os from 'node:os'; import * as sinon from 'sinon'; import { ATTRIBUTE_NAMES } from '../src/enum'; import { HostMetrics } from '../src'; @@ -140,7 +140,7 @@ describe('Host Metrics', () => { sandbox .stub(process.memoryUsage, 'rss') .callsFake(mockedProcess.memoryUsage.rss); - sandbox.stub(SI, 'networkStats').callsFake(mockedSI.networkStats); + sandbox.stub(Network, 'networkStats').callsFake(mockedSI.networkStats); reader = new TestMetricReader(); diff --git a/packages/opentelemetry-host-metrics/tsconfig.json b/packages/opentelemetry-host-metrics/tsconfig.json index e5f9ff1579..73128b74c5 100644 --- a/packages/opentelemetry-host-metrics/tsconfig.json +++ b/packages/opentelemetry-host-metrics/tsconfig.json @@ -6,6 +6,7 @@ }, "include": [ "src/**/*.ts", - "test/**/*.ts" + "test/**/*.ts", + "global.d.ts" ] } \ No newline at end of file From 4c0c3c905a186815cd30bdbc1e454ab2270eeaa1 Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 30 May 2024 18:29:24 +0200 Subject: [PATCH 13/16] lint --- packages/opentelemetry-host-metrics/src/stats/si.ts | 1 - packages/opentelemetry-host-metrics/test/metric.test.ts | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-host-metrics/src/stats/si.ts b/packages/opentelemetry-host-metrics/src/stats/si.ts index 3d1aaabef3..e259b0f1ea 100644 --- a/packages/opentelemetry-host-metrics/src/stats/si.ts +++ b/packages/opentelemetry-host-metrics/src/stats/si.ts @@ -14,7 +14,6 @@ * limitations under the License. */ - // Import from the network file directly as bundlers trigger the 'osx-temperature-sensor' import in the systeminformation/lib/cpu.js, // resulting in the following warning: "Can't resolve 'osx-temperature-sensor'" // See https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2071 diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 3ff5600cac..765d350ca3 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -39,8 +39,8 @@ class TestMetricReader extends MetricReader { public override selectAggregationTemporality(): AggregationTemporality { return AggregationTemporality.CUMULATIVE; } - protected async onForceFlush(): Promise { } - protected async onShutdown(): Promise { } + protected async onForceFlush(): Promise {} + protected async onShutdown(): Promise {} } let countSI = 0; From c0fbc8bd281f1e03f2fadeda11e84eb6f754f299 Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 30 May 2024 22:45:17 +0200 Subject: [PATCH 14/16] remove node import prefix --- packages/opentelemetry-host-metrics/src/stats/common.ts | 4 ++-- packages/opentelemetry-host-metrics/test/metric.test.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-host-metrics/src/stats/common.ts b/packages/opentelemetry-host-metrics/src/stats/common.ts index 341d4ee2a7..7b709f00e3 100644 --- a/packages/opentelemetry-host-metrics/src/stats/common.ts +++ b/packages/opentelemetry-host-metrics/src/stats/common.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { cpus, totalmem, freemem } from 'node:os'; -import type { CpuInfo } from 'node:os'; +import { cpus, totalmem, freemem } from 'os'; +import type { CpuInfo } from 'os'; import type { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types'; const MILLISECOND = 1 / 1e3; diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index 765d350ca3..b57325bff2 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -26,7 +26,7 @@ import { MetricReader, } from '@opentelemetry/sdk-metrics'; import * as assert from 'assert'; -import * as os from 'node:os'; +import * as os from 'os'; import * as sinon from 'sinon'; import { ATTRIBUTE_NAMES } from '../src/enum'; import { HostMetrics } from '../src'; @@ -39,8 +39,8 @@ class TestMetricReader extends MetricReader { public override selectAggregationTemporality(): AggregationTemporality { return AggregationTemporality.CUMULATIVE; } - protected async onForceFlush(): Promise {} - protected async onShutdown(): Promise {} + protected async onForceFlush(): Promise { } + protected async onShutdown(): Promise { } } let countSI = 0; From 5338456373c0b64bbe55074894df7710631b3d89 Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 30 May 2024 22:56:31 +0200 Subject: [PATCH 15/16] lint --- packages/opentelemetry-host-metrics/test/metric.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index b57325bff2..c98ad92636 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -39,8 +39,8 @@ class TestMetricReader extends MetricReader { public override selectAggregationTemporality(): AggregationTemporality { return AggregationTemporality.CUMULATIVE; } - protected async onForceFlush(): Promise { } - protected async onShutdown(): Promise { } + protected async onForceFlush(): Promise {} + protected async onShutdown(): Promise {} } let countSI = 0; From 3df9163eeeae002560e4a1a268ef2bf3d3a424e3 Mon Sep 17 00:00:00 2001 From: Maikel Date: Thu, 30 May 2024 23:15:37 +0200 Subject: [PATCH 16/16] eof --- packages/opentelemetry-host-metrics/package.json | 2 +- packages/opentelemetry-host-metrics/tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-host-metrics/package.json b/packages/opentelemetry-host-metrics/package.json index 1e5401c557..095e79e70e 100644 --- a/packages/opentelemetry-host-metrics/package.json +++ b/packages/opentelemetry-host-metrics/package.json @@ -59,4 +59,4 @@ "systeminformation": "5.22.9" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/opentelemetry-host-metrics#readme" -} \ No newline at end of file +} diff --git a/packages/opentelemetry-host-metrics/tsconfig.json b/packages/opentelemetry-host-metrics/tsconfig.json index 73128b74c5..cbf86a96b1 100644 --- a/packages/opentelemetry-host-metrics/tsconfig.json +++ b/packages/opentelemetry-host-metrics/tsconfig.json @@ -9,4 +9,4 @@ "test/**/*.ts", "global.d.ts" ] -} \ No newline at end of file +}