package trace
import (
)
const (
FlagsSampled = TraceFlags(0x01)
FlagsRandom = TraceFlags(0x02)
errInvalidHexID errorConst = "trace-id and span-id can only contain [0-9a-f] characters, all lowercase"
errInvalidTraceIDLength errorConst = "hex encoded trace-id must have length equals to 32"
errNilTraceID errorConst = "trace-id can't be all zero"
errInvalidSpanIDLength errorConst = "hex encoded span-id must have length equals to 16"
errNilSpanID errorConst = "span-id can't be all zero"
)
type errorConst string
func ( errorConst) () string {
return string()
}
type TraceID [16]byte
var (
nilTraceID TraceID
_ json.Marshaler = nilTraceID
)
func ( TraceID) () bool {
return != nilTraceID
}
func ( TraceID) () ([]byte, error) {
:= [32 + 2]byte{0: '"', 33: '"'}
:= .hexBytes()
copy([1:], [:])
return [:], nil
}
func ( TraceID) () string {
:= .hexBytes()
return string([:])
}
func ( TraceID) () [32]byte {
return [32]byte{
hexLU[[0x0]>>4], hexLU[[0x0]&0xf],
hexLU[[0x1]>>4], hexLU[[0x1]&0xf],
hexLU[[0x2]>>4], hexLU[[0x2]&0xf],
hexLU[[0x3]>>4], hexLU[[0x3]&0xf],
hexLU[[0x4]>>4], hexLU[[0x4]&0xf],
hexLU[[0x5]>>4], hexLU[[0x5]&0xf],
hexLU[[0x6]>>4], hexLU[[0x6]&0xf],
hexLU[[0x7]>>4], hexLU[[0x7]&0xf],
hexLU[[0x8]>>4], hexLU[[0x8]&0xf],
hexLU[[0x9]>>4], hexLU[[0x9]&0xf],
hexLU[[0xa]>>4], hexLU[[0xa]&0xf],
hexLU[[0xb]>>4], hexLU[[0xb]&0xf],
hexLU[[0xc]>>4], hexLU[[0xc]&0xf],
hexLU[[0xd]>>4], hexLU[[0xd]&0xf],
hexLU[[0xe]>>4], hexLU[[0xe]&0xf],
hexLU[[0xf]>>4], hexLU[[0xf]&0xf],
}
}
type SpanID [8]byte
var (
nilSpanID SpanID
_ json.Marshaler = nilSpanID
)
func ( SpanID) () bool {
return != nilSpanID
}
func ( SpanID) () ([]byte, error) {
:= [16 + 2]byte{0: '"', 17: '"'}
:= .hexBytes()
copy([1:], [:])
return [:], nil
}
func ( SpanID) () string {
:= .hexBytes()
return string([:])
}
func ( SpanID) () [16]byte {
return [16]byte{
hexLU[[0]>>4], hexLU[[0]&0xf],
hexLU[[1]>>4], hexLU[[1]&0xf],
hexLU[[2]>>4], hexLU[[2]&0xf],
hexLU[[3]>>4], hexLU[[3]&0xf],
hexLU[[4]>>4], hexLU[[4]&0xf],
hexLU[[5]>>4], hexLU[[5]&0xf],
hexLU[[6]>>4], hexLU[[6]&0xf],
hexLU[[7]>>4], hexLU[[7]&0xf],
}
}
func ( string) (TraceID, error) {
if len() != 32 {
return [16]byte{}, errInvalidTraceIDLength
}
, := hexRev[[0]], hexRev[[1]]
, := hexRev[[2]], hexRev[[3]]
, := hexRev[[4]], hexRev[[5]]
, := hexRev[[6]], hexRev[[7]]
, := hexRev[[8]], hexRev[[9]]
, := hexRev[[10]], hexRev[[11]]
, := hexRev[[12]], hexRev[[13]]
, := hexRev[[14]], hexRev[[15]]
, := hexRev[[16]], hexRev[[17]]
, := hexRev[[18]], hexRev[[19]]
, := hexRev[[20]], hexRev[[21]]
, := hexRev[[22]], hexRev[[23]]
, := hexRev[[24]], hexRev[[25]]
, := hexRev[[26]], hexRev[[27]]
, := hexRev[[28]], hexRev[[29]]
, := hexRev[[30]], hexRev[[31]]
:= [16]byte{
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
}
:= | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | |
if &0xf0 != 0 {
return [16]byte{}, errInvalidHexID
}
if == 0 {
return [16]byte{}, errNilTraceID
}
return , nil
}
func ( string) (SpanID, error) {
if len() != 16 {
return [8]byte{}, errInvalidSpanIDLength
}
, := hexRev[[0]], hexRev[[1]]
, := hexRev[[2]], hexRev[[3]]
, := hexRev[[4]], hexRev[[5]]
, := hexRev[[6]], hexRev[[7]]
, := hexRev[[8]], hexRev[[9]]
, := hexRev[[10]], hexRev[[11]]
, := hexRev[[12]], hexRev[[13]]
, := hexRev[[14]], hexRev[[15]]
:= [8]byte{
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
( << 4) | ,
}
:= | | | | | | | |
| | | | | | |
if &0xf0 != 0 {
return [8]byte{}, errInvalidHexID
}
if == 0 {
return [8]byte{}, errNilSpanID
}
return , nil
}
type TraceFlags byte
func ( TraceFlags) () bool {
return &FlagsSampled == FlagsSampled
}
func ( TraceFlags) ( bool) TraceFlags {
if {
return | FlagsSampled
}
return &^ FlagsSampled
}
func ( TraceFlags) () bool {
return &FlagsRandom == FlagsRandom
}
func ( TraceFlags) ( bool) TraceFlags {
if {
return | FlagsRandom
}
return &^ FlagsRandom
}
func ( TraceFlags) () ([]byte, error) {
:= [2 + 2]byte{0: '"', 3: '"'}
:= .hexBytes()
copy([1:], [:])
return [:], nil
}
func ( TraceFlags) () string {
:= .hexBytes()
return string([:])
}
func ( TraceFlags) () [2]byte {
return [2]byte{hexLU[>>4], hexLU[&0xf]}
}
type SpanContextConfig struct {
TraceID TraceID
SpanID SpanID
TraceFlags TraceFlags
TraceState TraceState
Remote bool
}
func ( SpanContextConfig) SpanContext {
return SpanContext{
traceID: .TraceID,
spanID: .SpanID,
traceFlags: .TraceFlags,
traceState: .TraceState,
remote: .Remote,
}
}
type SpanContext struct {
traceID TraceID
spanID SpanID
traceFlags TraceFlags
traceState TraceState
remote bool
}
var _ json.Marshaler = SpanContext{}
func ( SpanContext) () bool {
return .HasTraceID() && .HasSpanID()
}
func ( SpanContext) () bool {
return .remote
}
func ( SpanContext) ( bool) SpanContext {
return SpanContext{
traceID: .traceID,
spanID: .spanID,
traceFlags: .traceFlags,
traceState: .traceState,
remote: ,
}
}
func ( SpanContext) () TraceID {
return .traceID
}
func ( SpanContext) () bool {
return .traceID.IsValid()
}
func ( SpanContext) ( TraceID) SpanContext {
return SpanContext{
traceID: ,
spanID: .spanID,
traceFlags: .traceFlags,
traceState: .traceState,
remote: .remote,
}
}
func ( SpanContext) () SpanID {
return .spanID
}
func ( SpanContext) () bool {
return .spanID.IsValid()
}
func ( SpanContext) ( SpanID) SpanContext {
return SpanContext{
traceID: .traceID,
spanID: ,
traceFlags: .traceFlags,
traceState: .traceState,
remote: .remote,
}
}
func ( SpanContext) () TraceFlags {
return .traceFlags
}
func ( SpanContext) () bool {
return .traceFlags.IsSampled()
}
func ( SpanContext) () bool {
return .traceFlags.IsRandom()
}
func ( SpanContext) ( TraceFlags) SpanContext {
return SpanContext{
traceID: .traceID,
spanID: .spanID,
traceFlags: ,
traceState: .traceState,
remote: .remote,
}
}
func ( SpanContext) () TraceState {
return .traceState
}
func ( SpanContext) ( TraceState) SpanContext {
return SpanContext{
traceID: .traceID,
spanID: .spanID,
traceFlags: .traceFlags,
traceState: ,
remote: .remote,
}
}
func ( SpanContext) ( SpanContext) bool {
return .traceID == .traceID &&
.spanID == .spanID &&
.traceFlags == .traceFlags &&
.traceState.String() == .traceState.String() &&
.remote == .remote
}
func ( SpanContext) () ([]byte, error) {
return json.Marshal(SpanContextConfig{
TraceID: .traceID,
SpanID: .spanID,
TraceFlags: .traceFlags,
TraceState: .traceState,
Remote: .remote,
})
}