Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 9d52fcf

Browse files
committed
trace: add starttime start option
Adds a StartTime option when creating a new span. In some cases, you are able to trace only after the operation was made. for example in some post-operation hook/observer. func myHook(ctx context.Context, info queryInfo) { _, span := StartSpan("mydatabase", WithStartTime(time.Now().Sub(info.Duration())) span.End() }
1 parent df0549d commit 9d52fcf

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

trace/trace.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ type StartOptions struct {
141141
// SpanKind represents the kind of a span. If none is set,
142142
// SpanKindUnspecified is used.
143143
SpanKind int
144+
145+
// StartTime is used as the start time of the span if provided. Else it will
146+
// use time.Now().
147+
StartTime time.Time
144148
}
145149

146150
// StartOption apply changes to StartOptions.
@@ -161,6 +165,13 @@ func WithSampler(sampler Sampler) StartOption {
161165
}
162166
}
163167

168+
// WithStartTime sets the span start time.
169+
func WithStartTime(t time.Time) StartOption {
170+
return func(o *StartOptions) {
171+
o.StartTime = t
172+
}
173+
}
174+
164175
// StartSpan starts a new child span of the current span in the context. If
165176
// there is no span in the context, creates a new trace and span.
166177
//
@@ -236,7 +247,7 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
236247

237248
span.data = &SpanData{
238249
SpanContext: span.spanContext,
239-
StartTime: time.Now(),
250+
StartTime: o.StartTime,
240251
SpanKind: o.SpanKind,
241252
Name: name,
242253
HasRemoteParent: remoteParent,
@@ -246,6 +257,9 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
246257
span.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
247258
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
248259

260+
if span.data.StartTime.IsZero() {
261+
span.data.StartTime = time.Now()
262+
}
249263
if hasParent {
250264
span.data.ParentSpanID = parent.SpanID
251265
}

trace/trace_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ func TestStartSpanWithRemoteParent(t *testing.T) {
230230
}
231231
}
232232

233+
func TestStartSpanWithStartTime(t *testing.T) {
234+
start := time.Now()
235+
236+
ctx, span := StartSpan(context.Background(), "parent", WithSampler(AlwaysSample()), WithStartTime(start))
237+
if !span.data.StartTime.Equal(start) {
238+
t.Errorf("expected start time=%s was=%s", start, span.data.StartTime)
239+
}
240+
241+
_, span = StartSpan(ctx, "child")
242+
if !span.data.StartTime.After(start) {
243+
t.Error("expected child's start time to be after parent's")
244+
}
245+
}
246+
233247
// startSpan returns a context with a new Span that is recording events and will be exported.
234248
func startSpan(o StartOptions) *Span {
235249
_, span := StartSpanWithRemoteParent(context.Background(), "span0",

0 commit comments

Comments
 (0)