diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java index 51718fee7..7cb83ed4b 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java @@ -340,7 +340,15 @@ public class Span implements SpanContext { * Add an {@link Log#event event} to the timeline associated with this span. */ public void logEvent(String event) { - this.logs.add(new Log(System.currentTimeMillis(), event)); + logEvent(System.currentTimeMillis(), event); + } + + /** + * Add a {@link Log#event event} to a specific point (a timestamp in milliseconds) in the timeline + * associated with this span. + */ + public void logEvent(long timestampMilliseconds, String event) { + this.logs.add(new Log(timestampMilliseconds, event)); } /** diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTests.java index 2f51e189f..ce988953d 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTests.java @@ -166,6 +166,19 @@ public class SpanTests { .isEqualTo(span.logs()); } + @Test public void can_log_with_generated_timestamp() throws IOException { + span.logEvent("event1"); + + long beforeLog = System.currentTimeMillis(); + assertThat(span.logs().get(0).getTimestamp()).isGreaterThanOrEqualTo(beforeLog); + } + + @Test public void can_log_with_specified_timestamp() throws IOException { + span.logEvent(1L, "event1"); + + then(span.logs().get(0).getTimestamp()).isEqualTo(1L); + } + @Test public void should_properly_serialize_tags() throws IOException { span.tag("calculatedTax", "100");