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

Add support for child span count. #1023

Merged
merged 1 commit into from
Feb 3, 2019
Merged
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
3 changes: 3 additions & 0 deletions trace/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ type SpanData struct {
DroppedAnnotationCount int
DroppedMessageEventCount int
DroppedLinkCount int

// ChildSpanCount holds the number of child span created for this span.
ChildSpanCount int
}
10 changes: 10 additions & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont
var opts StartOptions
var parent SpanContext
if p := FromContext(ctx); p != nil {
p.addChild()
parent = p.spanContext
}
for _, op := range o {
Expand Down Expand Up @@ -385,6 +386,15 @@ func (s *Span) copyToCappedAttributes(attributes []Attribute) {
}
}

func (s *Span) addChild() {
if !s.IsRecordingEvents() {
return
}
s.mu.Lock()
s.data.ChildSpanCount++
s.mu.Unlock()
}

// AddAttributes sets attributes in the span.
//
// Existing attributes whose keys appear in the attributes parameter are overwritten.
Expand Down
31 changes: 31 additions & 0 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,37 @@ func TestStartSpanAfterEnd(t *testing.T) {
}
}

func TestChildSpanCount(t *testing.T) {
spans := make(exporter)
RegisterExporter(&spans)
defer UnregisterExporter(&spans)
ctx, span0 := StartSpan(context.Background(), "parent", WithSampler(AlwaysSample()))
ctx1, span1 := StartSpan(ctx, "span-1", WithSampler(AlwaysSample()))
_, span2 := StartSpan(ctx1, "span-2", WithSampler(AlwaysSample()))
span2.End()
span1.End()

_, span3 := StartSpan(ctx, "span-3", WithSampler(AlwaysSample()))
span3.End()
span0.End()
UnregisterExporter(&spans)
if got, want := len(spans), 4; got != want {
t.Fatalf("len(%#v) = %d; want %d", spans, got, want)
}
if got, want := spans["span-3"].ChildSpanCount, 0; got != want {
t.Errorf("span-3.ChildSpanCount=%q; want %q", got, want)
}
if got, want := spans["span-2"].ChildSpanCount, 0; got != want {
t.Errorf("span-2.ChildSpanCount=%q; want %q", got, want)
}
if got, want := spans["span-1"].ChildSpanCount, 1; got != want {
t.Errorf("span-1.ChildSpanCount=%q; want %q", got, want)
}
if got, want := spans["parent"].ChildSpanCount, 2; got != want {
t.Errorf("parent.ChildSpanCount=%q; want %q", got, want)
}
}

func TestNilSpanEnd(t *testing.T) {
var span *Span
span.End()
Expand Down