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

Introduce SpanKind support #610

Merged
merged 2 commits into from
Mar 20, 2018
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
1 change: 1 addition & 0 deletions trace/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func UnregisterExporter(e Exporter) {
type SpanData struct {
SpanContext
ParentSpanID SpanID
SpanKind int
Name string
StartTime time.Time
// The wall clock time of EndTime will be adjusted to always be offset
Expand Down
14 changes: 12 additions & 2 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func WithSpan(parent context.Context, s *Span) context.Context {
return context.WithValue(parent, contextKey{}, s)
}

// All available span kinds. Span kind must be either one of these values.
const (
SpanKindUnspecified = iota
SpanKindServer
SpanKindClient
)

// StartOptions contains options concerning how a span is started.
type StartOptions struct {
// Sampler to consult for this Span. If provided, it is always consulted.
Expand All @@ -111,9 +118,11 @@ type StartOptions struct {
// when there is a non-remote parent, no new sampling decision will be made:
// we will preserve the sampling of the parent.
Sampler Sampler
}

// TODO(jbd): Remove start options.
// SpanKind represents the kind of a span. If none is set,
// SpanKindUnspecified is used.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would specify that these must be one of the SpanKindUnspecified, SpanKindServer, SpanKindClient

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

SpanKind int
}

// 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 @@ -180,6 +189,7 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
span.data = &SpanData{
SpanContext: span.spanContext,
StartTime: time.Now(),
SpanKind: o.SpanKind,
Name: name,
HasRemoteParent: remoteParent,
}
Expand Down
86 changes: 78 additions & 8 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,15 @@ func TestStartSpanWithRemoteParent(t *testing.T) {
}

// startSpan returns a context with a new Span that is recording events and will be exported.
func startSpan() *Span {
func startSpan(o StartOptions) *Span {
return NewSpanWithRemoteParent("span0",
SpanContext{
TraceID: tid,
SpanID: sid,
TraceOptions: 1,
},
StartOptions{})
o,
)
}

type testExporter struct {
Expand Down Expand Up @@ -295,8 +296,77 @@ func checkTime(x *time.Time) bool {
return true
}

func TestSpanKind(t *testing.T) {
tests := []struct {
name string
startOptions StartOptions
want *SpanData
}{
{
name: "zero StartOptions",
startOptions: StartOptions{},
want: &SpanData{
SpanContext: SpanContext{
TraceID: tid,
SpanID: SpanID{},
TraceOptions: 0x1,
},
ParentSpanID: sid,
Name: "span0",
SpanKind: SpanKindUnspecified,
HasRemoteParent: true,
},
},
{
name: "client span",
startOptions: StartOptions{
SpanKind: SpanKindClient,
},
want: &SpanData{
SpanContext: SpanContext{
TraceID: tid,
SpanID: SpanID{},
TraceOptions: 0x1,
},
ParentSpanID: sid,
Name: "span0",
SpanKind: SpanKindClient,
HasRemoteParent: true,
},
},
{
name: "server span",
startOptions: StartOptions{
SpanKind: SpanKindServer,
},
want: &SpanData{
SpanContext: SpanContext{
TraceID: tid,
SpanID: SpanID{},
TraceOptions: 0x1,
},
ParentSpanID: sid,
Name: "span0",
SpanKind: SpanKindServer,
HasRemoteParent: true,
},
},
}

for _, tt := range tests {
span := startSpan(tt.startOptions)
got, err := endSpan(span)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("exporting span: got %#v want %#v", got, tt.want)
}
}
}

func TestSetSpanAttributes(t *testing.T) {
span := startSpan()
span := startSpan(StartOptions{})
span.AddAttributes(StringAttribute("key1", "value1"))
got, err := endSpan(span)
if err != nil {
Expand All @@ -320,7 +390,7 @@ func TestSetSpanAttributes(t *testing.T) {
}

func TestAnnotations(t *testing.T) {
span := startSpan()
span := startSpan(StartOptions{})
span.Annotatef([]Attribute{StringAttribute("key1", "value1")}, "%f", 1.5)
span.Annotate([]Attribute{StringAttribute("key2", "value2")}, "Annotate")
got, err := endSpan(span)
Expand Down Expand Up @@ -354,7 +424,7 @@ func TestAnnotations(t *testing.T) {
}

func TestMessageEvents(t *testing.T) {
span := startSpan()
span := startSpan(StartOptions{})
span.AddMessageReceiveEvent(3, 400, 300)
span.AddMessageSendEvent(1, 200, 100)
got, err := endSpan(span)
Expand Down Expand Up @@ -388,7 +458,7 @@ func TestMessageEvents(t *testing.T) {
}

func TestSetSpanStatus(t *testing.T) {
span := startSpan()
span := startSpan(StartOptions{})
span.SetStatus(Status{Code: int32(1), Message: "request failed"})
got, err := endSpan(span)
if err != nil {
Expand All @@ -412,7 +482,7 @@ func TestSetSpanStatus(t *testing.T) {
}

func TestAddLink(t *testing.T) {
span := startSpan()
span := startSpan(StartOptions{})
span.AddLink(Link{
TraceID: tid,
SpanID: sid,
Expand Down Expand Up @@ -450,7 +520,7 @@ func TestUnregisterExporter(t *testing.T) {
RegisterExporter(&te)
UnregisterExporter(&te)

ctx := startSpan()
ctx := startSpan(StartOptions{})
endSpan(ctx)
if len(te.spans) != 0 {
t.Error("unregistered Exporter was called")
Expand Down