feat(wcs): add correlated logs and product metrics

This commit is contained in:
zgw
2026-08-11 11:33:55 +08:00
parent 434cbe57b9
commit f9ebbcf51c
6 changed files with 369 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package com.ruoyi.wcs.observability;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class WcsObservabilityTest {
@AfterEach
void clearMdc() {
MDC.clear();
}
@Test
void writesAllCorrelationFieldsAndRestoresMdcAfterTheOperation() {
MDC.put("requestScope", "existing");
CorrelationContext.Fields fields = new CorrelationContext.Fields(
"TRACE-1", "REQ-1", "ORDER-1", "WF-1", "STEP-1",
"VEH-1", "CMD-1", "NODE:A", "EVENT-1", "RC-1");
try (CorrelationContext ignored = CorrelationContext.open(fields)) {
assertEquals("TRACE-1", MDC.get("traceId"));
assertEquals("ORDER-1", MDC.get("orderId"));
assertEquals("CMD-1", MDC.get("commandKey"));
assertEquals("RC-1", MDC.get("recoveryCaseId"));
assertEquals("existing", MDC.get("requestScope"));
}
assertNull(MDC.get("orderId"));
assertEquals("existing", MDC.get("requestScope"));
}
@Test
void recordsRequiredMetricsUsingOnlyBoundedOperationalLabels() {
SimpleMeterRegistry registry = new SimpleMeterRegistry();
WcsTelemetry telemetry = new MicrometerWcsTelemetry(registry, "FT546");
telemetry.recordDispatch(Duration.ofMillis(80), "INBOUND", "GRANTED");
telemetry.incrementOutboxRetry("DEVICE_COMMAND", "NOT_SENT");
telemetry.setTaskBacklog(12L, "INBOUND");
telemetry.recordCommandAcknowledgement(Duration.ofMillis(30),
"MOVE", "ACKNOWLEDGED");
telemetry.recordRecovery(Duration.ofSeconds(2), "DEVICE", "CONFIRMED");
assertEquals(1L, registry.counter(WcsMetricNames.OUTBOX_RETRY_COUNT,
"warehouse", "FT546", "outboxType", "DEVICE_COMMAND",
"outcome", "NOT_SENT").count(), 0.0D);
assertEquals(1L, registry.timer(WcsMetricNames.DISPATCH_DURATION,
"warehouse", "FT546", "orderType", "INBOUND",
"outcome", "GRANTED").count());
assertEquals(12.0D, registry.get(WcsMetricNames.TASK_BACKLOG)
.tag("warehouse", "FT546").tag("orderType", "INBOUND")
.gauge().value(), 0.0D);
assertEquals(1L, registry.timer(WcsMetricNames.COMMAND_ACK_DURATION,
"warehouse", "FT546", "commandType", "MOVE",
"outcome", "ACKNOWLEDGED").count());
assertEquals(1L, registry.timer(WcsMetricNames.RECOVERY_DURATION,
"warehouse", "FT546", "recoveryLevel", "DEVICE",
"outcome", "CONFIRMED").count());
}
}