propagation.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package propagation implements the binary trace context format.
  15. package propagation // import "go.opencensus.io/trace/propagation"
  16. // TODO: link to external spec document.
  17. // BinaryFormat format:
  18. //
  19. // Binary value: <version_id><version_format>
  20. // version_id: 1 byte representing the version id.
  21. //
  22. // For version_id = 0:
  23. //
  24. // version_format: <field><field>
  25. // field_format: <field_id><field_format>
  26. //
  27. // Fields:
  28. //
  29. // TraceId: (field_id = 0, len = 16, default = "0000000000000000") - 16-byte array representing the trace_id.
  30. // SpanId: (field_id = 1, len = 8, default = "00000000") - 8-byte array representing the span_id.
  31. // TraceOptions: (field_id = 2, len = 1, default = "0") - 1-byte array representing the trace_options.
  32. //
  33. // Fields MUST be encoded using the field id order (smaller to higher).
  34. //
  35. // Valid value example:
  36. //
  37. // {0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97,
  38. // 98, 99, 100, 101, 102, 103, 104, 2, 1}
  39. //
  40. // version_id = 0;
  41. // trace_id = {64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}
  42. // span_id = {97, 98, 99, 100, 101, 102, 103, 104};
  43. // trace_options = {1};
  44. import (
  45. "net/http"
  46. "go.opencensus.io/trace"
  47. )
  48. // Binary returns the binary format representation of a SpanContext.
  49. //
  50. // If sc is the zero value, Binary returns nil.
  51. func Binary(sc trace.SpanContext) []byte {
  52. if sc == (trace.SpanContext{}) {
  53. return nil
  54. }
  55. var b [29]byte
  56. copy(b[2:18], sc.TraceID[:])
  57. b[18] = 1
  58. copy(b[19:27], sc.SpanID[:])
  59. b[27] = 2
  60. b[28] = uint8(sc.TraceOptions)
  61. return b[:]
  62. }
  63. // FromBinary returns the SpanContext represented by b.
  64. //
  65. // If b has an unsupported version ID or contains no TraceID, FromBinary
  66. // returns with ok==false.
  67. func FromBinary(b []byte) (sc trace.SpanContext, ok bool) {
  68. if len(b) == 0 || b[0] != 0 {
  69. return trace.SpanContext{}, false
  70. }
  71. b = b[1:]
  72. if len(b) >= 17 && b[0] == 0 {
  73. copy(sc.TraceID[:], b[1:17])
  74. b = b[17:]
  75. } else {
  76. return trace.SpanContext{}, false
  77. }
  78. if len(b) >= 9 && b[0] == 1 {
  79. copy(sc.SpanID[:], b[1:9])
  80. b = b[9:]
  81. }
  82. if len(b) >= 2 && b[0] == 2 {
  83. sc.TraceOptions = trace.TraceOptions(b[1])
  84. }
  85. return sc, true
  86. }
  87. // HTTPFormat implementations propagate span contexts
  88. // in HTTP requests.
  89. //
  90. // SpanContextFromRequest extracts a span context from incoming
  91. // requests.
  92. //
  93. // SpanContextToRequest modifies the given request to include the given
  94. // span context.
  95. type HTTPFormat interface {
  96. SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool)
  97. SpanContextToRequest(sc trace.SpanContext, req *http.Request)
  98. }
  99. // TODO(jbd): Find a more representative but short name for HTTPFormat.