Skip to content

Commit

Permalink
add APIs for traceoption and tracestate. (open-telemetry#60)
Browse files Browse the repository at this point in the history
* add APIs for traceoption and tracestate.

* remove tracestate and refactor traceoptions.
  • Loading branch information
rghetia authored Aug 2, 2019
1 parent dad5622 commit 0edf31e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
18 changes: 16 additions & 2 deletions api/core/span_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ type TraceID struct {
Low uint64
}

const (
traceOptionBitMaskSampled = byte(0x01)
traceOptionBitMaskUnused = byte(0xFE)

// TraceOptionSampled is a byte with sampled bit set. It is a convenient value initialize
// SpanContext when a trace is sampled.
TraceOptionSampled = traceOptionBitMaskSampled
)

type SpanContext struct {
TraceID TraceID
SpanID uint64
TraceID TraceID
SpanID uint64
TraceOptions byte
}

var (
Expand All @@ -52,3 +62,7 @@ func (sc SpanContext) TraceIDString() string {
p2 := fmt.Sprintf("%.16x", sc.TraceID.Low)
return p1[0:3] + ".." + p2[13:16]
}

func (sc SpanContext) IsSampled() bool {
return sc.TraceOptions&traceOptionBitMaskSampled == traceOptionBitMaskSampled
}
42 changes: 42 additions & 0 deletions api/core/span_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,45 @@ func TestTraceIDString(t *testing.T) {
})
}
}

func TestSpanContextIsSampled(t *testing.T) {
for _, testcase := range []struct {
name string
sc SpanContext
want bool
}{
{
name: "sampled",
sc: SpanContext{
TraceID: TraceID{
High: uint64(42),
Low: uint64(42),
},
TraceOptions: TraceOptionSampled,
},
want: true,
}, {
name: "sampled plus unused",
sc: SpanContext{
TraceID: TraceID{
High: uint64(42),
Low: uint64(42),
},
TraceOptions: TraceOptionSampled | traceOptionBitMaskUnused,
},
want: true,
}, {
name: "not sampled/default",
sc: SpanContext{TraceID: TraceID{}},
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (sc SpanContext) TraceIDString() string {}
have := testcase.sc.IsSampled()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/golang/mock v1.2.0 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/golangci/golangci-lint v1.17.1
github.com/google/go-cmp v0.3.0 // indirect
github.com/google/go-cmp v0.3.0
github.com/lightstep/tracecontext.go v0.0.0-20181129014701-1757c391b1ac
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
Expand All @@ -20,6 +20,7 @@ require (
golang.org/x/sys v0.0.0-20190614160838-b47fdc937951 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190521203540-521d6ed310dd
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873 // indirect
google.golang.org/grpc v1.21.1
)

0 comments on commit 0edf31e

Please sign in to comment.