http.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2022 The OpenZipkin 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 b3
  15. import (
  16. "net/http"
  17. "github.com/openzipkin/zipkin-go/model"
  18. "github.com/openzipkin/zipkin-go/propagation"
  19. )
  20. // InjectOption provides functional option handler type.
  21. type InjectOption func(opts *InjectOptions)
  22. // InjectOptions provides the available functional options.
  23. type InjectOptions struct {
  24. shouldInjectSingleHeader bool
  25. shouldInjectMultiHeader bool
  26. }
  27. // WithSingleAndMultiHeader allows to include both single and multiple
  28. // headers in the context injection
  29. func WithSingleAndMultiHeader() InjectOption {
  30. return func(opts *InjectOptions) {
  31. opts.shouldInjectSingleHeader = true
  32. opts.shouldInjectMultiHeader = true
  33. }
  34. }
  35. // WithSingleHeaderOnly allows to include only single header in the context
  36. // injection
  37. func WithSingleHeaderOnly() InjectOption {
  38. return func(opts *InjectOptions) {
  39. opts.shouldInjectSingleHeader = true
  40. opts.shouldInjectMultiHeader = false
  41. }
  42. }
  43. // ExtractHTTP will extract a span.Context from the HTTP Request if found in
  44. // B3 header format.
  45. func ExtractHTTP(r *http.Request) propagation.Extractor {
  46. return func() (*model.SpanContext, error) {
  47. var (
  48. traceIDHeader = r.Header.Get(TraceID)
  49. spanIDHeader = r.Header.Get(SpanID)
  50. parentSpanIDHeader = r.Header.Get(ParentSpanID)
  51. sampledHeader = r.Header.Get(Sampled)
  52. flagsHeader = r.Header.Get(Flags)
  53. singleHeader = r.Header.Get(Context)
  54. )
  55. var (
  56. sc *model.SpanContext
  57. sErr error
  58. mErr error
  59. )
  60. if singleHeader != "" {
  61. sc, sErr = ParseSingleHeader(singleHeader)
  62. if sErr == nil {
  63. return sc, nil
  64. }
  65. }
  66. sc, mErr = ParseHeaders(
  67. traceIDHeader, spanIDHeader, parentSpanIDHeader,
  68. sampledHeader, flagsHeader,
  69. )
  70. if mErr != nil && sErr != nil {
  71. return nil, sErr
  72. }
  73. return sc, mErr
  74. }
  75. }
  76. // InjectHTTP will inject a span.Context into a HTTP Request
  77. func InjectHTTP(r *http.Request, opts ...InjectOption) propagation.Injector {
  78. options := InjectOptions{shouldInjectMultiHeader: true}
  79. for _, opt := range opts {
  80. opt(&options)
  81. }
  82. return func(sc model.SpanContext) error {
  83. if (model.SpanContext{}) == sc {
  84. return ErrEmptyContext
  85. }
  86. if options.shouldInjectMultiHeader {
  87. if sc.Debug {
  88. r.Header.Set(Flags, "1")
  89. } else if sc.Sampled != nil {
  90. // Debug is encoded as X-B3-Flags: 1. Since Debug implies Sampled,
  91. // so don't also send "X-B3-Sampled: 1".
  92. if *sc.Sampled {
  93. r.Header.Set(Sampled, "1")
  94. } else {
  95. r.Header.Set(Sampled, "0")
  96. }
  97. }
  98. if !sc.TraceID.Empty() && sc.ID > 0 {
  99. r.Header.Set(TraceID, sc.TraceID.String())
  100. r.Header.Set(SpanID, sc.ID.String())
  101. if sc.ParentID != nil {
  102. r.Header.Set(ParentSpanID, sc.ParentID.String())
  103. }
  104. }
  105. }
  106. if options.shouldInjectSingleHeader {
  107. r.Header.Set(Context, BuildSingleHeader(sc))
  108. }
  109. return nil
  110. }
  111. }