trace.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package trace // import "go.opentelemetry.io/otel/trace"
  4. import (
  5. "encoding/json"
  6. )
  7. const (
  8. // FlagsSampled is a bitmask with the sampled bit set. A SpanContext
  9. // with the sampling bit set means the span is sampled.
  10. FlagsSampled = TraceFlags(0x01)
  11. errInvalidHexID errorConst = "trace-id and span-id can only contain [0-9a-f] characters, all lowercase"
  12. errInvalidTraceIDLength errorConst = "hex encoded trace-id must have length equals to 32"
  13. errNilTraceID errorConst = "trace-id can't be all zero"
  14. errInvalidSpanIDLength errorConst = "hex encoded span-id must have length equals to 16"
  15. errNilSpanID errorConst = "span-id can't be all zero"
  16. )
  17. type errorConst string
  18. func (e errorConst) Error() string {
  19. return string(e)
  20. }
  21. // TraceID is a unique identity of a trace.
  22. // nolint:revive // revive complains about stutter of `trace.TraceID`.
  23. type TraceID [16]byte
  24. var (
  25. nilTraceID TraceID
  26. _ json.Marshaler = nilTraceID
  27. )
  28. // IsValid reports whether the trace TraceID is valid. A valid trace ID does
  29. // not consist of zeros only.
  30. func (t TraceID) IsValid() bool {
  31. return t != nilTraceID
  32. }
  33. // MarshalJSON implements a custom marshal function to encode TraceID
  34. // as a hex string.
  35. func (t TraceID) MarshalJSON() ([]byte, error) {
  36. b := [32 + 2]byte{0: '"', 33: '"'}
  37. h := t.hexBytes()
  38. copy(b[1:], h[:])
  39. return b[:], nil
  40. }
  41. // String returns the hex string representation form of a TraceID.
  42. func (t TraceID) String() string {
  43. h := t.hexBytes()
  44. return string(h[:])
  45. }
  46. // hexBytes returns the hex string representation form of a TraceID.
  47. func (t TraceID) hexBytes() [32]byte {
  48. return [32]byte{
  49. hexLU[t[0x0]>>4], hexLU[t[0x0]&0xf],
  50. hexLU[t[0x1]>>4], hexLU[t[0x1]&0xf],
  51. hexLU[t[0x2]>>4], hexLU[t[0x2]&0xf],
  52. hexLU[t[0x3]>>4], hexLU[t[0x3]&0xf],
  53. hexLU[t[0x4]>>4], hexLU[t[0x4]&0xf],
  54. hexLU[t[0x5]>>4], hexLU[t[0x5]&0xf],
  55. hexLU[t[0x6]>>4], hexLU[t[0x6]&0xf],
  56. hexLU[t[0x7]>>4], hexLU[t[0x7]&0xf],
  57. hexLU[t[0x8]>>4], hexLU[t[0x8]&0xf],
  58. hexLU[t[0x9]>>4], hexLU[t[0x9]&0xf],
  59. hexLU[t[0xa]>>4], hexLU[t[0xa]&0xf],
  60. hexLU[t[0xb]>>4], hexLU[t[0xb]&0xf],
  61. hexLU[t[0xc]>>4], hexLU[t[0xc]&0xf],
  62. hexLU[t[0xd]>>4], hexLU[t[0xd]&0xf],
  63. hexLU[t[0xe]>>4], hexLU[t[0xe]&0xf],
  64. hexLU[t[0xf]>>4], hexLU[t[0xf]&0xf],
  65. }
  66. }
  67. // SpanID is a unique identity of a span in a trace.
  68. type SpanID [8]byte
  69. var (
  70. nilSpanID SpanID
  71. _ json.Marshaler = nilSpanID
  72. )
  73. // IsValid reports whether the SpanID is valid. A valid SpanID does not consist
  74. // of zeros only.
  75. func (s SpanID) IsValid() bool {
  76. return s != nilSpanID
  77. }
  78. // MarshalJSON implements a custom marshal function to encode SpanID
  79. // as a hex string.
  80. func (s SpanID) MarshalJSON() ([]byte, error) {
  81. b := [16 + 2]byte{0: '"', 17: '"'}
  82. h := s.hexBytes()
  83. copy(b[1:], h[:])
  84. return b[:], nil
  85. }
  86. // String returns the hex string representation form of a SpanID.
  87. func (s SpanID) String() string {
  88. b := s.hexBytes()
  89. return string(b[:])
  90. }
  91. func (s SpanID) hexBytes() [16]byte {
  92. return [16]byte{
  93. hexLU[s[0]>>4], hexLU[s[0]&0xf],
  94. hexLU[s[1]>>4], hexLU[s[1]&0xf],
  95. hexLU[s[2]>>4], hexLU[s[2]&0xf],
  96. hexLU[s[3]>>4], hexLU[s[3]&0xf],
  97. hexLU[s[4]>>4], hexLU[s[4]&0xf],
  98. hexLU[s[5]>>4], hexLU[s[5]&0xf],
  99. hexLU[s[6]>>4], hexLU[s[6]&0xf],
  100. hexLU[s[7]>>4], hexLU[s[7]&0xf],
  101. }
  102. }
  103. // TraceIDFromHex returns a TraceID from a hex string if it is compliant with
  104. // the W3C trace-context specification. See more at
  105. // https://www.w3.org/TR/trace-context/#trace-id
  106. // nolint:revive // revive complains about stutter of `trace.TraceIDFromHex`.
  107. func TraceIDFromHex(h string) (TraceID, error) {
  108. if len(h) != 32 {
  109. return [16]byte{}, errInvalidTraceIDLength
  110. }
  111. var b [16]byte
  112. invalidMark := byte(0)
  113. for i := 0; i < len(h); i += 4 {
  114. b[i/2] = (hexRev[h[i]] << 4) | hexRev[h[i+1]]
  115. b[i/2+1] = (hexRev[h[i+2]] << 4) | hexRev[h[i+3]]
  116. invalidMark |= hexRev[h[i]] | hexRev[h[i+1]] | hexRev[h[i+2]] | hexRev[h[i+3]]
  117. }
  118. // If the upper 4 bits of any byte are not zero, there was an invalid hex
  119. // character since invalid hex characters are 0xff in hexRev.
  120. if invalidMark&0xf0 != 0 {
  121. return [16]byte{}, errInvalidHexID
  122. }
  123. // If we didn't set any bits, then h was all zeros.
  124. if invalidMark == 0 {
  125. return [16]byte{}, errNilTraceID
  126. }
  127. return b, nil
  128. }
  129. // SpanIDFromHex returns a SpanID from a hex string if it is compliant
  130. // with the w3c trace-context specification.
  131. // See more at https://www.w3.org/TR/trace-context/#parent-id
  132. func SpanIDFromHex(h string) (SpanID, error) {
  133. if len(h) != 16 {
  134. return [8]byte{}, errInvalidSpanIDLength
  135. }
  136. var b [8]byte
  137. invalidMark := byte(0)
  138. for i := 0; i < len(h); i += 4 {
  139. b[i/2] = (hexRev[h[i]] << 4) | hexRev[h[i+1]]
  140. b[i/2+1] = (hexRev[h[i+2]] << 4) | hexRev[h[i+3]]
  141. invalidMark |= hexRev[h[i]] | hexRev[h[i+1]] | hexRev[h[i+2]] | hexRev[h[i+3]]
  142. }
  143. // If the upper 4 bits of any byte are not zero, there was an invalid hex
  144. // character since invalid hex characters are 0xff in hexRev.
  145. if invalidMark&0xf0 != 0 {
  146. return [8]byte{}, errInvalidHexID
  147. }
  148. // If we didn't set any bits, then h was all zeros.
  149. if invalidMark == 0 {
  150. return [8]byte{}, errNilSpanID
  151. }
  152. return b, nil
  153. }
  154. // TraceFlags contains flags that can be set on a SpanContext.
  155. type TraceFlags byte //nolint:revive // revive complains about stutter of `trace.TraceFlags`.
  156. // IsSampled reports whether the sampling bit is set in the TraceFlags.
  157. func (tf TraceFlags) IsSampled() bool {
  158. return tf&FlagsSampled == FlagsSampled
  159. }
  160. // WithSampled sets the sampling bit in a new copy of the TraceFlags.
  161. func (tf TraceFlags) WithSampled(sampled bool) TraceFlags { // nolint:revive // sampled is not a control flag.
  162. if sampled {
  163. return tf | FlagsSampled
  164. }
  165. return tf &^ FlagsSampled
  166. }
  167. // MarshalJSON implements a custom marshal function to encode TraceFlags
  168. // as a hex string.
  169. func (tf TraceFlags) MarshalJSON() ([]byte, error) {
  170. b := [2 + 2]byte{0: '"', 3: '"'}
  171. h := tf.hexBytes()
  172. copy(b[1:], h[:])
  173. return b[:], nil
  174. }
  175. // String returns the hex string representation form of TraceFlags.
  176. func (tf TraceFlags) String() string {
  177. h := tf.hexBytes()
  178. return string(h[:])
  179. }
  180. func (tf TraceFlags) hexBytes() [2]byte {
  181. return [2]byte{hexLU[tf>>4], hexLU[tf&0xf]}
  182. }
  183. // SpanContextConfig contains mutable fields usable for constructing
  184. // an immutable SpanContext.
  185. type SpanContextConfig struct {
  186. TraceID TraceID
  187. SpanID SpanID
  188. TraceFlags TraceFlags
  189. TraceState TraceState
  190. Remote bool
  191. }
  192. // NewSpanContext constructs a SpanContext using values from the provided
  193. // SpanContextConfig.
  194. func NewSpanContext(config SpanContextConfig) SpanContext {
  195. return SpanContext{
  196. traceID: config.TraceID,
  197. spanID: config.SpanID,
  198. traceFlags: config.TraceFlags,
  199. traceState: config.TraceState,
  200. remote: config.Remote,
  201. }
  202. }
  203. // SpanContext contains identifying trace information about a Span.
  204. type SpanContext struct {
  205. traceID TraceID
  206. spanID SpanID
  207. traceFlags TraceFlags
  208. traceState TraceState
  209. remote bool
  210. }
  211. var _ json.Marshaler = SpanContext{}
  212. // IsValid reports whether the SpanContext is valid. A valid span context has a
  213. // valid TraceID and SpanID.
  214. func (sc SpanContext) IsValid() bool {
  215. return sc.HasTraceID() && sc.HasSpanID()
  216. }
  217. // IsRemote reports whether the SpanContext represents a remotely-created Span.
  218. func (sc SpanContext) IsRemote() bool {
  219. return sc.remote
  220. }
  221. // WithRemote returns a copy of sc with the Remote property set to remote.
  222. func (sc SpanContext) WithRemote(remote bool) SpanContext {
  223. return SpanContext{
  224. traceID: sc.traceID,
  225. spanID: sc.spanID,
  226. traceFlags: sc.traceFlags,
  227. traceState: sc.traceState,
  228. remote: remote,
  229. }
  230. }
  231. // TraceID returns the TraceID from the SpanContext.
  232. func (sc SpanContext) TraceID() TraceID {
  233. return sc.traceID
  234. }
  235. // HasTraceID reports whether the SpanContext has a valid TraceID.
  236. func (sc SpanContext) HasTraceID() bool {
  237. return sc.traceID.IsValid()
  238. }
  239. // WithTraceID returns a new SpanContext with the TraceID replaced.
  240. func (sc SpanContext) WithTraceID(traceID TraceID) SpanContext {
  241. return SpanContext{
  242. traceID: traceID,
  243. spanID: sc.spanID,
  244. traceFlags: sc.traceFlags,
  245. traceState: sc.traceState,
  246. remote: sc.remote,
  247. }
  248. }
  249. // SpanID returns the SpanID from the SpanContext.
  250. func (sc SpanContext) SpanID() SpanID {
  251. return sc.spanID
  252. }
  253. // HasSpanID reports whether the SpanContext has a valid SpanID.
  254. func (sc SpanContext) HasSpanID() bool {
  255. return sc.spanID.IsValid()
  256. }
  257. // WithSpanID returns a new SpanContext with the SpanID replaced.
  258. func (sc SpanContext) WithSpanID(spanID SpanID) SpanContext {
  259. return SpanContext{
  260. traceID: sc.traceID,
  261. spanID: spanID,
  262. traceFlags: sc.traceFlags,
  263. traceState: sc.traceState,
  264. remote: sc.remote,
  265. }
  266. }
  267. // TraceFlags returns the flags from the SpanContext.
  268. func (sc SpanContext) TraceFlags() TraceFlags {
  269. return sc.traceFlags
  270. }
  271. // IsSampled reports whether the sampling bit is set in the SpanContext's TraceFlags.
  272. func (sc SpanContext) IsSampled() bool {
  273. return sc.traceFlags.IsSampled()
  274. }
  275. // WithTraceFlags returns a new SpanContext with the TraceFlags replaced.
  276. func (sc SpanContext) WithTraceFlags(flags TraceFlags) SpanContext {
  277. return SpanContext{
  278. traceID: sc.traceID,
  279. spanID: sc.spanID,
  280. traceFlags: flags,
  281. traceState: sc.traceState,
  282. remote: sc.remote,
  283. }
  284. }
  285. // TraceState returns the TraceState from the SpanContext.
  286. func (sc SpanContext) TraceState() TraceState {
  287. return sc.traceState
  288. }
  289. // WithTraceState returns a new SpanContext with the TraceState replaced.
  290. func (sc SpanContext) WithTraceState(state TraceState) SpanContext {
  291. return SpanContext{
  292. traceID: sc.traceID,
  293. spanID: sc.spanID,
  294. traceFlags: sc.traceFlags,
  295. traceState: state,
  296. remote: sc.remote,
  297. }
  298. }
  299. // Equal reports whether two SpanContext values are equal.
  300. func (sc SpanContext) Equal(other SpanContext) bool {
  301. return sc.traceID == other.traceID &&
  302. sc.spanID == other.spanID &&
  303. sc.traceFlags == other.traceFlags &&
  304. sc.traceState.String() == other.traceState.String() &&
  305. sc.remote == other.remote
  306. }
  307. // MarshalJSON implements a custom marshal function to encode a SpanContext.
  308. func (sc SpanContext) MarshalJSON() ([]byte, error) {
  309. return json.Marshal(SpanContextConfig{
  310. TraceID: sc.traceID,
  311. SpanID: sc.spanID,
  312. TraceFlags: sc.traceFlags,
  313. TraceState: sc.traceState,
  314. Remote: sc.remote,
  315. })
  316. }