diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/InMemoryTraceRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/InMemoryTraceRepository.java index d05fc0e64092..f2f797e06ca3 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/InMemoryTraceRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/InMemoryTraceRepository.java @@ -16,9 +16,9 @@ package org.springframework.boot.actuate.trace; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -31,7 +31,18 @@ public class InMemoryTraceRepository implements TraceRepository { private int capacity = 100; - private final List traces = new ArrayList(); + private boolean reverse = true; + + private final List traces = new LinkedList(); + + /** + * Flag to say that the repository lists traces in reverse order. + * + * @param reverse flag value (default true) + */ + public void setReverse(boolean reverse) { + this.reverse = reverse; + } /** * @param capacity the capacity to set @@ -52,9 +63,14 @@ public void add(Map map) { Trace trace = new Trace(new Date(), map); synchronized (this.traces) { while (this.traces.size() >= this.capacity) { - this.traces.remove(0); + this.traces.remove(this.capacity - 1); + } + if (this.reverse) { + this.traces.add(0, trace); + } + else { + this.traces.add(trace); } - this.traces.add(trace); } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/InMemoryTraceRepositoryTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/InMemoryTraceRepositoryTests.java index 5de63340fbf1..cb67cc091903 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/InMemoryTraceRepositoryTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/InMemoryTraceRepositoryTests.java @@ -40,7 +40,7 @@ public void capacityLimited() { this.repository.add(Collections. singletonMap("bar", "bar")); List traces = this.repository.findAll(); assertEquals(2, traces.size()); - assertEquals("bar", traces.get(1).getInfo().get("bar")); + assertEquals("bar", traces.get(0).getInfo().get("bar")); } }