span.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package span
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. )
  7. // A SpanID refers to a single span.
  8. type SpanID struct {
  9. // Trace is the root ID of the tree that contains all of the spans
  10. // related to this one.
  11. Trace ID
  12. // Span is an ID that probabilistically uniquely identifies this
  13. // span.
  14. Span ID
  15. // Parent is the ID of the parent span, if any.
  16. Parent ID
  17. }
  18. var (
  19. // ErrBadSpanID is returned when the span ID cannot be parsed.
  20. ErrBadSpanID = errors.New("bad span ID")
  21. )
  22. // String returns the SpanID as a slash-separated, set of hex-encoded
  23. // parameters (root, ID, parent). If the SpanID has no parent, that value is
  24. // elided.
  25. func (id SpanID) String() string {
  26. if id.Parent == 0 {
  27. return fmt.Sprintf("%s%s%s", id.Trace, SpanIDDelimiter, id.Span)
  28. }
  29. return fmt.Sprintf(
  30. "%s%s%s%s%s",
  31. id.Trace,
  32. SpanIDDelimiter,
  33. id.Span,
  34. SpanIDDelimiter,
  35. id.Parent,
  36. )
  37. }
  38. // Format formats according to a format specifier and returns the
  39. // resulting string. The receiver's string representation is the first
  40. // argument.
  41. func (id SpanID) Format(s string, args ...interface{}) string {
  42. args = append([]interface{}{id.String()}, args...)
  43. return fmt.Sprintf(s, args...)
  44. }
  45. // IsRoot returns whether id is the root ID of a trace.
  46. func (id SpanID) IsRoot() bool {
  47. return id.Parent == 0
  48. }
  49. // NewRootSpanID generates a new span ID for a root span. This should
  50. // only be used to generate entries for spans caused exclusively by
  51. // spans which are outside of your system as a whole (e.g., a root
  52. // span for the first time you see a user request).
  53. func NewRootSpanID() SpanID {
  54. return SpanID{
  55. Trace: generateID(),
  56. Span: generateID(),
  57. }
  58. }
  59. // NewSpanID returns a new ID for an span which is the child of the
  60. // given parent ID. This should be used to track causal relationships
  61. // between spans.
  62. func NewSpanID(parent SpanID) SpanID {
  63. return SpanID{
  64. Trace: parent.Trace,
  65. Span: generateID(),
  66. Parent: parent.Span,
  67. }
  68. }
  69. const (
  70. // SpanIDDelimiter is the delimiter used to concatenate an
  71. // SpanID's components.
  72. SpanIDDelimiter = "/"
  73. )
  74. // ParseSpanID parses the given string as a slash-separated set of parameters.
  75. func ParseSpanID(s string) (*SpanID, error) {
  76. parts := strings.Split(s, SpanIDDelimiter)
  77. if len(parts) != 2 && len(parts) != 3 {
  78. return nil, ErrBadSpanID
  79. }
  80. root, err := ParseID(parts[0])
  81. if err != nil {
  82. return nil, ErrBadSpanID
  83. }
  84. id, err := ParseID(parts[1])
  85. if err != nil {
  86. return nil, ErrBadSpanID
  87. }
  88. var parent ID
  89. if len(parts) == 3 {
  90. i, err := ParseID(parts[2])
  91. if err != nil {
  92. return nil, ErrBadSpanID
  93. }
  94. parent = i
  95. }
  96. return &SpanID{
  97. Trace: root,
  98. Span: id,
  99. Parent: parent,
  100. }, nil
  101. }