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

Commit

Permalink
trace: add starttime start option
Browse files Browse the repository at this point in the history
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().Add(-1 * info.Duration()))
       span.End()
    }
  • Loading branch information
gwik committed Sep 25, 2019
1 parent df0549d commit 35e0833
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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.
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

0 comments on commit 35e0833

Please sign in to comment.