Skip to content

Commit

Permalink
Add guard against invalid end timestamps (elastic#3363)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKunz authored Oct 17, 2023
1 parent 546da88 commit 45657a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
=== Unreleased
[float]
===== Features
* Added protection against invalid timestamps provided by manual instrumentation - {pull}3363[#3363]
[float]
===== Bug fixes
* Fixed too many spans being created for `HTTPUrlConnection` requests with method `HEAD` - {pull}3353[#3353]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ public void end() {

public final void end(long epochMicros) {
if (!finished) {

long startTime = timestamp.get();
if(epochMicros < startTime) {
logger.warn("End called on {} with a timestamp before start! Using start timestamp as end instead.", this);
epochMicros = startTime;
}

this.endTimestamp.set(epochMicros);
childDurations.onSpanEnd(epochMicros);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,20 @@ void testTimestamps() {
assertThat(span.getDuration()).isEqualTo(10);
}

@Test
void testTimestampSanitization() {
final Transaction transaction = tracerImpl.startChildTransaction(new HashMap<>(), TextHeaderMapAccessor.INSTANCE, ConstantSampler.of(true), 10, null);
final Span span = transaction.createSpan(20);
span.end(10);

transaction.end(5);

assertThat(transaction.getTimestamp()).isEqualTo(10);
assertThat(transaction.getDuration()).isEqualTo(0);
assertThat(span.getTimestamp()).isEqualTo(20);
assertThat(span.getDuration()).isEqualTo(0);
}

@Test
void testStartSpanAfterTransactionHasEnded() {
final Transaction transaction = startTestRootTransaction();
Expand Down

0 comments on commit 45657a0

Please sign in to comment.