update SystemMetrics according to spec update (#1705)

This commit is contained in:
Sergei Malafeev 2020-11-21 01:54:31 +08:00 committed by GitHub
parent 2ba278489e
commit f0feefc4b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 49 deletions

View File

@ -19,7 +19,10 @@ import oshi.hardware.NetworkIF;
/** System Metrics Utility. */ /** System Metrics Utility. */
public class SystemMetrics { 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() {} private SystemMetrics() {}
@ -32,15 +35,15 @@ public class SystemMetrics {
meter meter
.longUpDownSumObserverBuilder("system.memory.usage") .longUpDownSumObserverBuilder("system.memory.usage")
.setDescription("System memory usage") .setDescription("System memory usage")
.setUnit("bytes") .setUnit("By")
.build() .build()
.setCallback( .setCallback(
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
GlobalMemory mem = hal.getMemory(); GlobalMemory mem = hal.getMemory();
r.observe(mem.getTotal() - mem.getAvailable(), Labels.of(TYPE_LABEL_KEY, "used")); r.observe(mem.getTotal() - mem.getAvailable(), LABEL_STATE_USED);
r.observe(mem.getAvailable(), Labels.of(TYPE_LABEL_KEY, "free")); r.observe(mem.getAvailable(), LABEL_STATE_FREE);
} }
}); });
@ -56,33 +59,31 @@ public class SystemMetrics {
GlobalMemory mem = hal.getMemory(); GlobalMemory mem = hal.getMemory();
r.observe( r.observe(
((double) (mem.getTotal() - mem.getAvailable())) / mem.getTotal(), ((double) (mem.getTotal() - mem.getAvailable())) / mem.getTotal(),
Labels.of(TYPE_LABEL_KEY, "used")); LABEL_STATE_USED);
r.observe( r.observe(((double) mem.getAvailable()) / mem.getTotal(), LABEL_STATE_FREE);
((double) mem.getAvailable()) / mem.getTotal(),
Labels.of(TYPE_LABEL_KEY, "free"));
} }
}); });
meter meter
.longSumObserverBuilder("system.network.io") .longSumObserverBuilder("system.network.io")
.setDescription("System network IO") .setDescription("System network IO")
.setUnit("bytes") .setUnit("By")
.build() .build()
.setCallback( .setCallback(
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
long recv = 0;
long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) { for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes(); networkIf.updateAttributes();
recv += networkIf.getBytesRecv(); long recv = networkIf.getBytesRecv();
sent += networkIf.getBytesSent(); 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>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
long recv = 0;
long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) { for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes(); networkIf.updateAttributes();
recv += networkIf.getPacketsRecv(); long recv = networkIf.getPacketsRecv();
sent += networkIf.getPacketsSent(); 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>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
long recv = 0;
long sent = 0;
for (NetworkIF networkIf : hal.getNetworkIFs()) { for (NetworkIF networkIf : hal.getNetworkIFs()) {
networkIf.updateAttributes(); networkIf.updateAttributes();
recv += networkIf.getInErrors(); long recv = networkIf.getInErrors();
sent += networkIf.getOutErrors(); 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 meter
.longSumObserverBuilder("system.disk.io") .longSumObserverBuilder("system.disk.io")
.setDescription("System disk IO") .setDescription("System disk IO")
.setUnit("bytes") .setUnit("By")
.build() .build()
.setCallback( .setCallback(
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
long read = 0;
long write = 0;
for (HWDiskStore diskStore : hal.getDiskStores()) { for (HWDiskStore diskStore : hal.getDiskStores()) {
read += diskStore.getReadBytes(); long read = diskStore.getReadBytes();
write += diskStore.getWriteBytes(); 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>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult r) { public void update(LongResult r) {
long read = 0;
long write = 0;
for (HWDiskStore diskStore : hal.getDiskStores()) { for (HWDiskStore diskStore : hal.getDiskStores()) {
read += diskStore.getReads(); long read = diskStore.getReads();
write += diskStore.getWrites(); 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"));
} }
}); });
} }

View File

@ -19,14 +19,14 @@ public class SystemMetricsTest extends AbstractMetricsTest {
testMetricExporter.waitForData(); testMetricExporter.waitForData();
intervalMetricReader.shutdown(); 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.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.packets", "packets", Type.MONOTONIC_LONG, false);
verify("system.network.errors", "errors", 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); verify("system.disk.operations", "operations", Type.MONOTONIC_LONG, false);
} }
} }