1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-01 22:09:57 +02:00

add IsValid() for SpanContext (#46)

* add IsValid() for SpanContext

* add unit test
This commit is contained in:
thinkerou 2019-08-06 00:58:51 +08:00 committed by rghetia
parent 0f32efcdaa
commit 0a37b8f43e
2 changed files with 41 additions and 0 deletions

View File

@ -44,6 +44,10 @@ var (
INVALID_SPAN_CONTEXT = SpanContext{}
)
func (sc SpanContext) IsValid() bool {
return sc.HasTraceID() && sc.HasSpanID()
}
func (sc SpanContext) HasTraceID() bool {
return sc.TraceID.High != 0 || sc.TraceID.Low != 0
}

View File

@ -18,6 +18,43 @@ import (
"testing"
)
func TestIsValid(t *testing.T) {
for _, testcase := range []struct {
name string
tid TraceID
sid uint64
want bool
}{
{
name: "bothTrue",
tid: TraceID{High: uint64(42)},
sid: uint64(42),
want: true,
}, {
name: "bothFalse",
tid: TraceID{High: uint64(0)},
sid: uint64(0),
want: false,
}, {
name: "oneTrue",
tid: TraceID{High: uint64(0)},
sid: uint64(42),
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
sc := SpanContext{
TraceID: testcase.tid,
SpanID: testcase.sid,
}
have := sc.IsValid()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestHasTraceID(t *testing.T) {
for _, testcase := range []struct {
name string