From 1c7d861ffcb5ac56b5be4948537f8de2a7e5dc0c Mon Sep 17 00:00:00 2001 From: Brian Devins Date: Wed, 25 Jan 2017 11:25:44 -0500 Subject: [PATCH] Add a new log method to span that allows for setting the time (#501) * Add a new log method that allows for setting the time * Clarify with time unit * Simple test and spelling correction * Consolidate "logic" and add a test for the other log method --- .../java/org/springframework/cloud/sleuth/Span.java | 10 +++++++++- .../org/springframework/cloud/sleuth/SpanTests.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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");