task-187: 临时磁盘容量/占用指标契约(tmpdisk.capacity/used gauge,path 标签,可算占用比对接 09 告警)+ 8 条测试
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.nanri.aiimage.metrics;
|
||||
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* task-187:临时磁盘容量/占用指标契约(spec 11 §1,09 临时磁盘巡检/告警接线面)。
|
||||
* aiimage.tmpdisk.capacity/used gauge,标签 path;与 09 告警阈值联动时可直接读值比判。
|
||||
*/
|
||||
class TempDiskMetricsTest {
|
||||
|
||||
private final SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
private final TaskObservabilityMetrics metrics = new TaskObservabilityMetrics("server-110", registry);
|
||||
|
||||
private static final String TMP = "/data/tmp-files";
|
||||
|
||||
@Test
|
||||
void diskCapacityMeasured() {
|
||||
metrics.disk(TMP, 100_000_000L, 30_000_000L);
|
||||
assertEquals(100_000_000.0,
|
||||
registry.get("aiimage.tmpdisk.capacity").tag("path", TMP).gauge().value(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void diskUsedMeasured() {
|
||||
metrics.disk(TMP, 100_000_000L, 30_000_000L);
|
||||
assertEquals(30_000_000.0,
|
||||
registry.get("aiimage.tmpdisk.used").tag("path", TMP).gauge().value(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void usagePercentDerivableForThreshold() {
|
||||
metrics.disk(TMP, 100_000_000L, 90_000_000L);
|
||||
double capacity = registry.get("aiimage.tmpdisk.capacity").tag("path", TMP).gauge().value();
|
||||
double used = registry.get("aiimage.tmpdisk.used").tag("path", TMP).gauge().value();
|
||||
double percent = used / capacity * 100;
|
||||
assertEquals(90.0, percent, 0.001, "09 告警可按占用百分比比判阈值");
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyDirectoryRecordsZero() {
|
||||
metrics.disk(TMP, 100_000_000L, 0L);
|
||||
assertEquals(0.0, registry.get("aiimage.tmpdisk.used").tag("path", TMP).gauge().value(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathTagAttached() {
|
||||
metrics.disk(TMP, 1L, 1L);
|
||||
assertEquals(TMP, registry.get("aiimage.tmpdisk.capacity").gauge().getId().getTag("path"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void gaugeUpdatesOnLaterWrite() {
|
||||
metrics.disk(TMP, 100L, 10L);
|
||||
metrics.disk(TMP, 100L, 55L);
|
||||
assertEquals(55.0, registry.get("aiimage.tmpdisk.used").tag("path", TMP).gauge().value(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void diskNamesFrozen() {
|
||||
metrics.disk(TMP, 1L, 1L);
|
||||
assertTrue(registry.getMeters().stream()
|
||||
.anyMatch(m -> m.getId().getName().equals("aiimage.tmpdisk.capacity")));
|
||||
assertTrue(registry.getMeters().stream()
|
||||
.anyMatch(m -> m.getId().getName().equals("aiimage.tmpdisk.used")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void measureCallDoesNotBlockOrThrow() {
|
||||
// 多次写 gauge 不抛错(巡检每轮都调用),registry 为空也不影响
|
||||
TaskObservabilityMetrics without = new TaskObservabilityMetrics("server-121", null);
|
||||
without.disk(TMP, 1L, 1L);
|
||||
metrics.disk(TMP, 1L, 1L);
|
||||
metrics.disk(TMP + "/sub", 1L, 0L);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user