update SystemMetrics according to spec update
MODIFY
instrumentation/oshi/library/src/main/java/io/opentelemetry/instrumentation/oshi/SystemMetrics.java
instrumentation/oshi/library/src/main/java/io/opentelemetry/instrumentation/oshi/SystemMetrics.java
@@ -19,7 +19,10 @@ import oshi.hardware.NetworkIF;
/** System Metrics Utility. */
public class SystemMetrics {
- private static final String TYPE_LABEL_KEY = "type";
+ private static final String DEVICE_LABEL_KEY = "device";
+ private static final String DIRECTION_LABEL_KEY = "direction";
+ private static final Labels LABEL_STATE_USED = Labels.of("state", "used");
+ private static final Labels LABEL_STATE_FREE = Labels.of("state", "free");
private SystemMetrics() {}
@@ -32,15 +35,15 @@ public class SystemMetrics {
meter
.longUpDownSumObserverBuilder("system.memory.usage")
.setDescription("System memory usage")
- .setUnit("bytes")
+ .setUnit("By")
.build()
.setCallback(
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
GlobalMemory mem = hal.getMemory();
- r.observe(mem.getTotal() - mem.getAvailable(), Labels.of(TYPE_LABEL_KEY, "used"));
- r.observe(mem.getAvailable(), Labels.of(TYPE_LABEL_KEY, "free"));
+ r.observe(mem.getTotal() - mem.getAvailable(), LABEL_STATE_USED);
+ r.observe(mem.getAvailable(), LABEL_STATE_FREE);
}
});
@@ -56,33 +59,31 @@ public class SystemMetrics {
GlobalMemory mem = hal.getMemory();
r.observe(
((double) (mem.getTotal() - mem.getAvailable())) / mem.getTotal(),
- Labels.of(TYPE_LABEL_KEY, "used"));
- r.observe(
- ((double) mem.getAvailable()) / mem.getTotal(),
- Labels.of(TYPE_LABEL_KEY, "free"));
+ LABEL_STATE_USED);
+ r.observe(((double) mem.getAvailable()) / mem.getTotal(), LABEL_STATE_FREE);
}
});
meter
.longSumObserverBuilder("system.network.io")
.setDescription("System network IO")
- .setUnit("bytes")
+ .setUnit("By")
.build()
.setCallback(
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
- long recv = 0;
- long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes();
- recv += networkIf.getBytesRecv();
- sent += networkIf.getBytesSent();
+ long recv = networkIf.getBytesRecv();
+ long sent = networkIf.getBytesSent();
+ String device = networkIf.getName();
+ r.observe(
+ recv, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "receive"));
+ r.observe(
+ sent, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "transmit"));
}
-
- r.observe(recv, Labels.of(TYPE_LABEL_KEY, "receive"));
- r.observe(sent, Labels.of(TYPE_LABEL_KEY, "transmit"));
}
});
@@ -95,17 +96,17 @@ public class SystemMetrics {
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
- long recv = 0;
- long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes();
- recv += networkIf.getPacketsRecv();
- sent += networkIf.getPacketsSent();
+ long recv = networkIf.getPacketsRecv();
+ long sent = networkIf.getPacketsSent();
+ String device = networkIf.getName();
+ r.observe(
+ recv, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "receive"));
+ r.observe(
+ sent, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "transmit"));
}
-
- r.observe(recv, Labels.of(TYPE_LABEL_KEY, "receive"));
- r.observe(sent, Labels.of(TYPE_LABEL_KEY, "transmit"));
}
});
@@ -118,39 +119,37 @@ public class SystemMetrics {
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
- long recv = 0;
- long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes();
- recv += networkIf.getInErrors();
- sent += networkIf.getOutErrors();
+ long recv = networkIf.getInErrors();
+ long sent = networkIf.getOutErrors();
+ String device = networkIf.getName();
+ r.observe(
+ recv, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "receive"));
+ r.observe(
+ sent, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "transmit"));
}
-
- r.observe(recv, Labels.of(TYPE_LABEL_KEY, "receive"));
- r.observe(sent, Labels.of(TYPE_LABEL_KEY, "transmit"));
}
});
meter
.longSumObserverBuilder("system.disk.io")
.setDescription("System disk IO")
- .setUnit("bytes")
+ .setUnit("By")
.build()
.setCallback(
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
- long read = 0;
- long write = 0;
-
for (HWDiskStore diskStore : hal.getDiskStores()) {
- read += diskStore.getReadBytes();
- write += diskStore.getWriteBytes();
+ long read = diskStore.getReadBytes();
+ long write = diskStore.getWriteBytes();
+ String device = diskStore.getName();
+ r.observe(read, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "read"));
+ r.observe(
+ write, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "write"));
}
-
- r.observe(read, Labels.of(TYPE_LABEL_KEY, "read"));
- r.observe(write, Labels.of(TYPE_LABEL_KEY, "write"));
}
});
@@ -163,16 +162,15 @@ public class SystemMetrics {
new Callback<LongResult>() {
@Override
public void update(LongResult r) {
- long read = 0;
- long write = 0;
for (HWDiskStore diskStore : hal.getDiskStores()) {
- read += diskStore.getReads();
- write += diskStore.getWrites();
+ long read = diskStore.getReads();
+ long write = diskStore.getWrites();
+ String device = diskStore.getName();
+ r.observe(read, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "read"));
+ r.observe(
+ write, Labels.of(DEVICE_LABEL_KEY, device, DIRECTION_LABEL_KEY, "write"));
}
-
- r.observe(read, Labels.of(TYPE_LABEL_KEY, "read"));
- r.observe(write, Labels.of(TYPE_LABEL_KEY, "write"));
}
});
}

MODIFY
instrumentation/oshi/library/src/test/java/io/opentelemetry/instrumentation/oshi/SystemMetricsTest.java
instrumentation/oshi/library/src/test/java/io/opentelemetry/instrumentation/oshi/SystemMetricsTest.java
@@ -19,14 +19,14 @@ public class SystemMetricsTest extends AbstractMetricsTest {
testMetricExporter.waitForData();
intervalMetricReader.shutdown();
- verify("system.memory.usage", "bytes", Type.NON_MONOTONIC_LONG, true);
+ verify("system.memory.usage", "By", Type.NON_MONOTONIC_LONG, true);
verify("system.memory.utilization", "1", Type.GAUGE_DOUBLE, true);
- verify("system.network.io", "bytes", Type.MONOTONIC_LONG, false);
+ verify("system.network.io", "By", Type.MONOTONIC_LONG, false);
verify("system.network.packets", "packets", Type.MONOTONIC_LONG, false);
verify("system.network.errors", "errors", Type.MONOTONIC_LONG, false);
- verify("system.disk.io", "bytes", Type.MONOTONIC_LONG, false);
+ verify("system.disk.io", "By", Type.MONOTONIC_LONG, false);
verify("system.disk.operations", "operations", Type.MONOTONIC_LONG, false);
}
}

update SystemMetrics according to spec update (#1705)