trace.go 8.3 KB

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