Skip to content

Commit cdbd895

Browse files
authored
Do not pass walltime=0 to span event (#487)
When `Event#getWallTime` returns `0`(default implementation), do not pass it to the underlying span event. Rather, let the span implementation determine the timestamp. Signed-off-by: Tadaya Tsuyukubo <[email protected]>
1 parent 04147d8 commit cdbd895

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

micrometer-tracing/src/main/java/io/micrometer/tracing/handler/TracingObservationHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ default void onScopeReset(T context) {
121121
@Override
122122
default void onEvent(Event event, T context) {
123123
long timestamp = event.getWallTime();
124-
getRequiredSpan(context).event(event.getContextualName(), timestamp, TimeUnit.MILLISECONDS);
124+
if (timestamp == 0) {
125+
getRequiredSpan(context).event(event.getContextualName());
126+
}
127+
else {
128+
getRequiredSpan(context).event(event.getContextualName(), timestamp, TimeUnit.MILLISECONDS);
129+
}
125130
}
126131

127132
@Override

micrometer-tracing/src/test/java/io/micrometer/tracing/handler/TracingObservationHandlerTests.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package io.micrometer.tracing.handler;
1717

1818
import io.micrometer.observation.Observation;
19+
import io.micrometer.observation.Observation.Event;
1920
import io.micrometer.tracing.CurrentTraceContext;
2021
import io.micrometer.tracing.Span;
2122
import io.micrometer.tracing.Tracer;
@@ -25,6 +26,7 @@
2526
import org.mockito.InOrder;
2627

2728
import java.util.Collections;
29+
import java.util.concurrent.TimeUnit;
2830

2931
import static org.assertj.core.api.Assertions.assertThat;
3032
import static org.assertj.core.api.BDDAssertions.thenNoException;
@@ -95,4 +97,34 @@ void spanShouldNotBeOverriddenWhenResettingScope() {
9597
assertThat(tracingContext.getSpan()).isSameAs(span);
9698
}
9799

100+
@Test
101+
void eventTimestamp() {
102+
Span span = mock(Span.class);
103+
Observation.Context context = new Observation.Context();
104+
TracingObservationHandler.TracingContext tracingContext = new TracingObservationHandler.TracingContext();
105+
tracingContext.setSpan(span);
106+
context.put(TracingObservationHandler.TracingContext.class, tracingContext);
107+
TracingObservationHandler<Observation.Context> handler = () -> tracer;
108+
109+
Event defaultEvent = () -> "default-event";
110+
handler.onEvent(defaultEvent, context);
111+
BDDMockito.then(span).should().event("default-event");
112+
113+
BDDMockito.reset(span);
114+
115+
Event eventWithWallTime = new Event() {
116+
@Override
117+
public String getName() {
118+
return "walltime-event";
119+
}
120+
121+
@Override
122+
public long getWallTime() {
123+
return 100;
124+
}
125+
};
126+
handler.onEvent(eventWithWallTime, context);
127+
BDDMockito.then(span).should().event("walltime-event", 100, TimeUnit.MILLISECONDS);
128+
}
129+
98130
}

0 commit comments

Comments
 (0)