Skip to content
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
Prev Previous commit
Next Next commit
chore(host-metrics): update attribute names
  • Loading branch information
david-luna committed Nov 21, 2023
commit 705f2ed662f359282d771b50e2d232b44e2f3bf7
10 changes: 8 additions & 2 deletions packages/opentelemetry-host-metrics/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ export enum METRIC_NAMES {
PROCESS_MEMORY_USAGE = 'process.memory.usage',
}

export enum METRIC_ATTRIBUTES {
export enum ATTRIBUTE_NAMES {
SYSTEM_CPU_LOGICAL_NUMBER = 'system.cpu.logical_number',
SYSTEM_CPU_STATE = 'system.cpu.state',
SYSTEM_MEMORY_STATE = 'system.memory.state',
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',
}

export enum CPU_LABELS {
Expand All @@ -41,7 +48,6 @@ export enum CPU_LABELS {
}

export enum NETWORK_LABELS {
DEVICE = 'device',
RECEIVE = 'receive',
TRANSMIT = 'transmit',
}
Expand Down
51 changes: 29 additions & 22 deletions packages/opentelemetry-host-metrics/src/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class HostMetrics extends BaseMetrics {
observableResult: api.BatchObservableResult,
cpuUsages: CpuUsageData[]
): void {
const stateAttr = enums.METRIC_ATTRIBUTES.SYSTEM_CPU_STATE;
const cpuAttr = enums.METRIC_ATTRIBUTES.SYSTEM_CPU_LOGICAL_NUMBER;
const stateAttr = enums.ATTRIBUTE_NAMES.SYSTEM_CPU_STATE;
const cpuAttr = enums.ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER;

for (let i = 0, j = cpuUsages.length; i < j; i++) {
const cpuUsage = cpuUsages[i];
Expand Down Expand Up @@ -93,25 +93,27 @@ export class HostMetrics extends BaseMetrics {
observableResult: api.BatchObservableResult,
processCpuUsage: ProcessCpuUsageData
): void {
const stateAttr = enums.ATTRIBUTE_NAMES.PROCESS_CPU_STATE;

observableResult.observe(this._processCpuTime, processCpuUsage.user, {
state: enums.CPU_LABELS.USER,
[stateAttr]: enums.CPU_LABELS.USER,
});
observableResult.observe(this._processCpuTime, processCpuUsage.system, {
state: enums.CPU_LABELS.SYSTEM,
[stateAttr]: enums.CPU_LABELS.SYSTEM,
});

observableResult.observe(
this._processCpuUtilization,
processCpuUsage.userP,
{
state: enums.CPU_LABELS.USER,
[stateAttr]: enums.CPU_LABELS.USER,
}
);
observableResult.observe(
this._processCpuUtilization,
processCpuUsage.systemP,
{
state: enums.CPU_LABELS.SYSTEM,
[stateAttr]: enums.CPU_LABELS.SYSTEM,
}
);
}
Expand All @@ -120,18 +122,20 @@ export class HostMetrics extends BaseMetrics {
observableResult: api.BatchObservableResult,
memUsage: MemoryData
): void {
const stateAttr = enums.ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE;

observableResult.observe(this._memoryUsage, memUsage.used, {
state: enums.MEMORY_LABELS.USED,
[stateAttr]: enums.MEMORY_LABELS.USED,
});
observableResult.observe(this._memoryUsage, memUsage.free, {
state: enums.MEMORY_LABELS.FREE,
[stateAttr]: enums.MEMORY_LABELS.FREE,
});

observableResult.observe(this._memoryUtilization, memUsage.usedP, {
state: enums.MEMORY_LABELS.USED,
[stateAttr]: enums.MEMORY_LABELS.USED,
});
observableResult.observe(this._memoryUtilization, memUsage.freeP, {
state: enums.MEMORY_LABELS.FREE,
[stateAttr]: enums.MEMORY_LABELS.FREE,
});
}

Expand All @@ -146,33 +150,36 @@ export class HostMetrics extends BaseMetrics {
observableResult: api.BatchObservableResult,
networkUsages: NetworkData[]
): void {
const deviceAttr = enums.ATTRIBUTE_NAMES.SYSTEM_DEVICE;
const directionAttr = enums.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, {
[enums.NETWORK_LABELS.DEVICE]: networkUsage.iface,
direction: enums.NETWORK_LABELS.RECEIVE,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.RECEIVE,
});
observableResult.observe(this._networkDropped, networkUsage.tx_dropped, {
device: networkUsage.iface,
direction: enums.NETWORK_LABELS.TRANSMIT,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.TRANSMIT,
});

observableResult.observe(this._networkErrors, networkUsage.rx_errors, {
device: networkUsage.iface,
direction: enums.NETWORK_LABELS.RECEIVE,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.RECEIVE,
});
observableResult.observe(this._networkErrors, networkUsage.tx_errors, {
device: networkUsage.iface,
direction: enums.NETWORK_LABELS.TRANSMIT,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.TRANSMIT,
});

observableResult.observe(this._networkIo, networkUsage.rx_bytes, {
device: networkUsage.iface,
direction: enums.NETWORK_LABELS.RECEIVE,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.RECEIVE,
});
observableResult.observe(this._networkIo, networkUsage.tx_bytes, {
device: networkUsage.iface,
direction: enums.NETWORK_LABELS.TRANSMIT,
[deviceAttr]: networkUsage.iface,
[directionAttr]: enums.NETWORK_LABELS.TRANSMIT,
});
}
}
Expand Down
Loading