trace.go 11 KB

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