You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-15 01:04:25 +02:00
add IsValid() for SpanContext (#46)
* add IsValid() for SpanContext * add unit test
This commit is contained in:
@ -44,6 +44,10 @@ var (
|
|||||||
INVALID_SPAN_CONTEXT = SpanContext{}
|
INVALID_SPAN_CONTEXT = SpanContext{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (sc SpanContext) IsValid() bool {
|
||||||
|
return sc.HasTraceID() && sc.HasSpanID()
|
||||||
|
}
|
||||||
|
|
||||||
func (sc SpanContext) HasTraceID() bool {
|
func (sc SpanContext) HasTraceID() bool {
|
||||||
return sc.TraceID.High != 0 || sc.TraceID.Low != 0
|
return sc.TraceID.High != 0 || sc.TraceID.Low != 0
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,43 @@ import (
|
|||||||
"testing"
|
"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) {
|
func TestHasTraceID(t *testing.T) {
|
||||||
for _, testcase := range []struct {
|
for _, testcase := range []struct {
|
||||||
name string
|
name string
|
||||||
|
Reference in New Issue
Block a user