diff --git a/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondAccessItem.java b/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondAccessItem.java index e0bde4ad306..9d570312000 100644 --- a/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondAccessItem.java +++ b/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondAccessItem.java @@ -26,12 +26,19 @@ public class DurationMillisecondAccessItem implements AccessLogItem { @Override public void appendServerFormattedItem(ServerAccessLogEvent accessLogEvent, StringBuilder builder) { - builder.append(accessLogEvent.getMilliEndTime() - accessLogEvent.getMilliStartTime()); + builder.append(calc(accessLogEvent.getMilliEndTime(), accessLogEvent.getMilliStartTime())); } @Override public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) { - builder.append((finishEvent.getInvocation().getInvocationStageTrace().getFinish() - + builder.append(calc(finishEvent.getInvocation().getInvocationStageTrace().getFinish(), finishEvent.getInvocation().getInvocationStageTrace().getStartSend()) / 1000_000); } + + private long calc(long end, long begin) { + if (begin == 0 || end == 0) { + return 0; + } + return end - begin; + } } diff --git a/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationSecondAccessItem.java b/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationSecondAccessItem.java index 2da34026d5e..9dcd015fc92 100644 --- a/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationSecondAccessItem.java +++ b/common/common-access-log/src/main/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationSecondAccessItem.java @@ -28,12 +28,19 @@ public class DurationSecondAccessItem implements AccessLogItem { @Override public void appendServerFormattedItem(ServerAccessLogEvent accessLogEvent, StringBuilder builder) { - builder.append((accessLogEvent.getMilliEndTime() - accessLogEvent.getMilliStartTime()) / 1000); + builder.append(calc(accessLogEvent.getMilliEndTime(), accessLogEvent.getMilliStartTime()) / 1000); } @Override public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) { - builder.append((finishEvent.getInvocation().getInvocationStageTrace().getFinish() - + builder.append(calc(finishEvent.getInvocation().getInvocationStageTrace().getFinish(), finishEvent.getInvocation().getInvocationStageTrace().getStartSend()) / 1000_000_000); } + + private long calc(long end, long begin) { + if (begin == 0 || end == 0) { + return 0; + } + return end - begin; + } } diff --git a/common/common-access-log/src/test/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondItemTest.java b/common/common-access-log/src/test/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondItemTest.java index 7ad32b23f87..c97afc41d3c 100644 --- a/common/common-access-log/src/test/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondItemTest.java +++ b/common/common-access-log/src/test/java/org/apache/servicecomb/common/accessLog/core/element/impl/DurationMillisecondItemTest.java @@ -64,6 +64,13 @@ public void testAppendFormattedElement() { ELEMENT.appendServerFormattedItem(accessLogEvent, strBuilder); Assertions.assertEquals("1", strBuilder.toString()); + strBuilder = new StringBuilder(); + ELEMENT.appendClientFormattedItem(finishEvent, strBuilder); + Assertions.assertEquals("0", strBuilder.toString()); + + when(invocationStageTrace.getStartSend()).thenReturn(1000L); + when(invocationStageTrace.getFinish()).thenReturn(1001_000L); + strBuilder = new StringBuilder(); ELEMENT.appendClientFormattedItem(finishEvent, strBuilder); Assertions.assertEquals("1", strBuilder.toString());