Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit b6602af

Browse files
committed
Ensure that span start <= end
Adds code to detect when a span's end time is less than its start time. This would be caught by a database constraint and prevent the insertion of the span. The new code will swap the start and end times to allow the span's insertion.
1 parent d152878 commit b6602af

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ We use the following categories for changes:
1414

1515
## [Unreleased]
1616

17+
### Fixed
18+
- Fix spans with end < start. Start and end are swapped in this case. [#1096]
19+
1720
## [0.9.0] - 2022-02-02
1821

1922
### Added

pkg/pgmodel/ingestor/trace/writer.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,23 @@ func (t *traceWriterImpl) InsertTraces(ctx context.Context, traces pdata.Traces)
265265

266266
eventTimeRange := getEventTimeRange(span.Events())
267267

268+
// postgresql timestamptz only has microsecond precision while time.Time has nanosecond precision
269+
start := span.StartTimestamp().AsTime().Truncate(time.Microsecond)
270+
end := span.EndTimestamp().AsTime().Truncate(time.Microsecond)
271+
// make sure start <= end
272+
if end.Before(start) {
273+
start, end = end, start
274+
}
275+
268276
spanBatch.Queue(
269277
insertSpanSQL,
270278
traceID,
271279
spanID,
272280
getTraceStateValue(span.TraceState()),
273281
parentSpanID,
274282
operationID,
275-
span.StartTimestamp().AsTime(),
276-
span.EndTimestamp().AsTime(),
283+
start,
284+
end,
277285
string(jsonTags),
278286
span.DroppedAttributesCount(),
279287
eventTimeRange,

pkg/tests/end_to_end_tests/dataset_traces_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ func fillSpanOne(span pdata.Span) {
136136
span.SetTraceID(pdata.NewTraceID(traceID1))
137137
span.SetSpanID(pdata.NewSpanID(generateRandSpanID()))
138138
span.SetName("operationA")
139-
span.SetStartTimestamp(testSpanStartTimestamp)
140-
span.SetEndTimestamp(testSpanEndTimestamp)
139+
// test the logic that detects and fixes end < start
140+
span.SetStartTimestamp(testSpanEndTimestamp)
141+
span.SetEndTimestamp(testSpanStartTimestamp)
141142
span.SetDroppedAttributesCount(1)
142143
span.SetTraceState("span-trace-state1")
143144
initSpanAttributes(span.Attributes())

0 commit comments

Comments
 (0)