Skip to content

Commit cadbcbf

Browse files
author
Ignacio Bonafonte
authored
Fix startTime of span. (#282)
* Fix startTime of span. SpanBuilderSDK startTime was being initialised with current system time, this was forcing this value to be used as the startTime when the RecordEventsReadableSpan was initialised. This was causing two big issues: - The start time was set when the spanBuilder was created, not when the span was really started - If the user decided to use an external clock, it would only be used for the end time, not for the start time. Which would make the duration to be always wrong
1 parent d7eb282 commit cadbcbf

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public class RecordEventsReadableSpan: ReadableSpan {
4444
public private(set) var clock: Clock
4545
/// The resource associated with this span.
4646
public private(set) var resource: Resource
47-
/// The start time of the span.
4847
/// instrumentation library of the named tracer which created this span
4948
public private(set) var instrumentationLibraryInfo: InstrumentationLibraryInfo
50-
/// The resource associated with this span.
49+
/// The start time of the span.
5150
public private(set) var startTime: Date
5251
/// Set of recorded attributes. DO NOT CALL any other method that changes the ordering of events.
5352
private var attributes: AttributesDictionary
@@ -132,7 +131,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
132131
/// - attributes: the attributes set during span creation.
133132
/// - links: the links set during span creation, may be truncated.
134133
/// - totalRecordedLinks: the total number of links set (including dropped links).
135-
/// - startTime: the time for the new span.
134+
/// - startTime: the time for the new span, if not set it will use assigned Clock time
136135
public static func startSpan(context: SpanContext,
137136
name: String,
138137
instrumentationLibraryInfo: InstrumentationLibraryInfo,
@@ -146,7 +145,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
146145
attributes: AttributesDictionary,
147146
links: [SpanData.Link],
148147
totalRecordedLinks: Int,
149-
startTime: Date) -> RecordEventsReadableSpan
148+
startTime: Date?) -> RecordEventsReadableSpan
150149
{
151150
let span = RecordEventsReadableSpan(context: context,
152151
name: name,

Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SpanBuilderSdk: SpanBuilder {
3333

3434
private var startAsActive: Bool = false
3535

36-
private var startTime = Date()
36+
private var startTime: Date?
3737

3838
init(spanName: String,
3939
instrumentationLibraryInfo: InstrumentationLibraryInfo,

0 commit comments

Comments
 (0)