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

trace: add starttime start option #1176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type StartOptions struct {
// SpanKind represents the kind of a span. If none is set,
// SpanKindUnspecified is used.
SpanKind int

// StartTime is used as the start time of the span if provided. Else it will
// use time.Now().
StartTime time.Time
}

// StartOption apply changes to StartOptions.
Expand All @@ -161,6 +165,13 @@ func WithSampler(sampler Sampler) StartOption {
}
}

// WithStartTime sets the span start time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a bit more description would be better.

WithStartTime sets the start time of the span to provided time t, when it is started.
In absence of this option, wall clock time is used as the start time.
This option is typically used when starting of a span is delayed.

func WithStartTime(t time.Time) StartOption {
return func(o *StartOptions) {
o.StartTime = t
}
}

// StartSpan starts a new child span of the current span in the context. If
// there is no span in the context, creates a new trace and span.
//
Expand Down Expand Up @@ -236,7 +247,7 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa

span.data = &SpanData{
SpanContext: span.spanContext,
StartTime: time.Now(),
StartTime: o.StartTime,
SpanKind: o.SpanKind,
Name: name,
HasRemoteParent: remoteParent,
Expand All @@ -246,6 +257,9 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
span.messageEvents = newEvictedQueue(cfg.MaxMessageEventsPerSpan)
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)

if span.data.StartTime.IsZero() {
span.data.StartTime = time.Now()
}
if hasParent {
span.data.ParentSpanID = parent.SpanID
}
Expand Down
14 changes: 14 additions & 0 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ func TestStartSpanWithRemoteParent(t *testing.T) {
}
}

func TestStartSpanWithStartTime(t *testing.T) {
start := time.Now().Add(-10 * time.Second)

ctx, span := StartSpan(context.Background(), "parent", WithSampler(AlwaysSample()), WithStartTime(start))
if !span.data.StartTime.Equal(start) {
t.Errorf("expected start time=%s was=%s", start, span.data.StartTime)
}

_, span = StartSpan(ctx, "child")
if !span.data.StartTime.After(start) {
t.Error("expected child's start time to be after parent's")
}
}

// startSpan returns a context with a new Span that is recording events and will be exported.
func startSpan(o StartOptions) *Span {
_, span := StartSpanWithRemoteParent(context.Background(), "span0",
Expand Down