propagation.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright The OpenTelemetry 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 // import "go.opentelemetry.io/otel/propagation"
  15. import (
  16. "context"
  17. "net/http"
  18. )
  19. // TextMapCarrier is the storage medium used by a TextMapPropagator.
  20. type TextMapCarrier interface {
  21. // DO NOT CHANGE: any modification will not be backwards compatible and
  22. // must never be done outside of a new major release.
  23. // Get returns the value associated with the passed key.
  24. Get(key string) string
  25. // DO NOT CHANGE: any modification will not be backwards compatible and
  26. // must never be done outside of a new major release.
  27. // Set stores the key-value pair.
  28. Set(key string, value string)
  29. // DO NOT CHANGE: any modification will not be backwards compatible and
  30. // must never be done outside of a new major release.
  31. // Keys lists the keys stored in this carrier.
  32. Keys() []string
  33. // DO NOT CHANGE: any modification will not be backwards compatible and
  34. // must never be done outside of a new major release.
  35. }
  36. // HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface.
  37. type HeaderCarrier http.Header
  38. // Get returns the value associated with the passed key.
  39. func (hc HeaderCarrier) Get(key string) string {
  40. return http.Header(hc).Get(key)
  41. }
  42. // Set stores the key-value pair.
  43. func (hc HeaderCarrier) Set(key string, value string) {
  44. http.Header(hc).Set(key, value)
  45. }
  46. // Keys lists the keys stored in this carrier.
  47. func (hc HeaderCarrier) Keys() []string {
  48. keys := make([]string, 0, len(hc))
  49. for k := range hc {
  50. keys = append(keys, k)
  51. }
  52. return keys
  53. }
  54. // TextMapPropagator propagates cross-cutting concerns as key-value text
  55. // pairs within a carrier that travels in-band across process boundaries.
  56. type TextMapPropagator interface {
  57. // DO NOT CHANGE: any modification will not be backwards compatible and
  58. // must never be done outside of a new major release.
  59. // Inject set cross-cutting concerns from the Context into the carrier.
  60. Inject(ctx context.Context, carrier TextMapCarrier)
  61. // DO NOT CHANGE: any modification will not be backwards compatible and
  62. // must never be done outside of a new major release.
  63. // Extract reads cross-cutting concerns from the carrier into a Context.
  64. Extract(ctx context.Context, carrier TextMapCarrier) context.Context
  65. // DO NOT CHANGE: any modification will not be backwards compatible and
  66. // must never be done outside of a new major release.
  67. // Fields returns the keys who's values are set with Inject.
  68. Fields() []string
  69. // DO NOT CHANGE: any modification will not be backwards compatible and
  70. // must never be done outside of a new major release.
  71. }
  72. type compositeTextMapPropagator []TextMapPropagator
  73. func (p compositeTextMapPropagator) Inject(ctx context.Context, carrier TextMapCarrier) {
  74. for _, i := range p {
  75. i.Inject(ctx, carrier)
  76. }
  77. }
  78. func (p compositeTextMapPropagator) Extract(ctx context.Context, carrier TextMapCarrier) context.Context {
  79. for _, i := range p {
  80. ctx = i.Extract(ctx, carrier)
  81. }
  82. return ctx
  83. }
  84. func (p compositeTextMapPropagator) Fields() []string {
  85. unique := make(map[string]struct{})
  86. for _, i := range p {
  87. for _, k := range i.Fields() {
  88. unique[k] = struct{}{}
  89. }
  90. }
  91. fields := make([]string, 0, len(unique))
  92. for k := range unique {
  93. fields = append(fields, k)
  94. }
  95. return fields
  96. }
  97. // NewCompositeTextMapPropagator returns a unified TextMapPropagator from the
  98. // group of passed TextMapPropagator. This allows different cross-cutting
  99. // concerns to be propagates in a unified manner.
  100. //
  101. // The returned TextMapPropagator will inject and extract cross-cutting
  102. // concerns in the order the TextMapPropagators were provided. Additionally,
  103. // the Fields method will return a de-duplicated slice of the keys that are
  104. // set with the Inject method.
  105. func NewCompositeTextMapPropagator(p ...TextMapPropagator) TextMapPropagator {
  106. return compositeTextMapPropagator(p)
  107. }