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

Commit a404386

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 ad20435 commit a404386

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ We use the following categories for changes:
1717
### Added
1818
- Add Prometheus metrics support for Tracing [#1102]
1919

20+
### Fixed
21+
- Fix spans with end < start. Start and end are swapped in this case. [#1096]
22+
2023
## [0.9.0] - 2022-02-02
2124

2225
### Added

pkg/pgmodel/ingestor/trace/writer.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,16 @@ func (t *traceWriterImpl) InsertTraces(ctx context.Context, traces pdata.Traces)
276276

277277
eventTimeRange := getEventTimeRange(span.Events())
278278

279-
if maxEndTimestamp < uint64(span.EndTimestamp()) {
280-
maxEndTimestamp = uint64(span.EndTimestamp())
279+
// postgresql timestamptz only has microsecond precision while time.Time has nanosecond precision
280+
start := span.StartTimestamp().AsTime().Truncate(time.Microsecond)
281+
end := span.EndTimestamp().AsTime().Truncate(time.Microsecond)
282+
// make sure start <= end
283+
if end.Before(start) {
284+
start, end = end, start
285+
}
286+
287+
if maxEndTimestamp < uint64(end.Unix()) {
288+
maxEndTimestamp = uint64(end.Unix())
281289
}
282290

283291
spanBatch.Queue(
@@ -287,8 +295,8 @@ func (t *traceWriterImpl) InsertTraces(ctx context.Context, traces pdata.Traces)
287295
getTraceStateValue(span.TraceState()),
288296
parentSpanID,
289297
operationID,
290-
span.StartTimestamp().AsTime(),
291-
span.EndTimestamp().AsTime(),
298+
start,
299+
end,
292300
string(jsonTags),
293301
span.DroppedAttributesCount(),
294302
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)