Commit 8ef21b31 authored by Dave Syer's avatar Dave Syer

Add flag to TraceRepository to reverse order of listing

Default to true (so existing apps change their behaviour, but that
seems to be the majority vote).

Fixes gh-2794
parent b45bb6d0
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
package org.springframework.boot.actuate.trace; package org.springframework.boot.actuate.trace;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -31,7 +31,18 @@ public class InMemoryTraceRepository implements TraceRepository { ...@@ -31,7 +31,18 @@ public class InMemoryTraceRepository implements TraceRepository {
private int capacity = 100; private int capacity = 100;
private final List<Trace> traces = new ArrayList<Trace>(); private boolean reverse = true;
private final List<Trace> traces = new LinkedList<Trace>();
/**
* 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 * @param capacity the capacity to set
...@@ -52,10 +63,15 @@ public class InMemoryTraceRepository implements TraceRepository { ...@@ -52,10 +63,15 @@ public class InMemoryTraceRepository implements TraceRepository {
Trace trace = new Trace(new Date(), map); Trace trace = new Trace(new Date(), map);
synchronized (this.traces) { synchronized (this.traces) {
while (this.traces.size() >= this.capacity) { 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);
} }
} }
}
} }
...@@ -40,7 +40,7 @@ public class InMemoryTraceRepositoryTests { ...@@ -40,7 +40,7 @@ public class InMemoryTraceRepositoryTests {
this.repository.add(Collections.<String, Object> singletonMap("bar", "bar")); this.repository.add(Collections.<String, Object> singletonMap("bar", "bar"));
List<Trace> traces = this.repository.findAll(); List<Trace> traces = this.repository.findAll();
assertEquals(2, traces.size()); assertEquals(2, traces.size());
assertEquals("bar", traces.get(1).getInfo().get("bar")); assertEquals("bar", traces.get(0).getInfo().get("bar"));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment