propagation.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // MapCarrier is a TextMapCarrier that uses a map held in memory as a storage
  37. // medium for propagated key-value pairs.
  38. type MapCarrier map[string]string
  39. // Compile time check that MapCarrier implements the TextMapCarrier.
  40. var _ TextMapCarrier = MapCarrier{}
  41. // Get returns the value associated with the passed key.
  42. func (c MapCarrier) Get(key string) string {
  43. return c[key]
  44. }
  45. // Set stores the key-value pair.
  46. func (c MapCarrier) Set(key, value string) {
  47. c[key] = value
  48. }
  49. // Keys lists the keys stored in this carrier.
  50. func (c MapCarrier) Keys() []string {
  51. keys := make([]string, 0, len(c))
  52. for k := range c {
  53. keys = append(keys, k)
  54. }
  55. return keys
  56. }
  57. // HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface.
  58. type HeaderCarrier http.Header
  59. // Get returns the value associated with the passed key.
  60. func (hc HeaderCarrier) Get(key string) string {
  61. return http.Header(hc).Get(key)
  62. }
  63. // Set stores the key-value pair.
  64. func (hc HeaderCarrier) Set(key string, value string) {
  65. http.Header(hc).Set(key, value)
  66. }
  67. // Keys lists the keys stored in this carrier.
  68. func (hc HeaderCarrier) Keys() []string {
  69. keys := make([]string, 0, len(hc))
  70. for k := range hc {
  71. keys = append(keys, k)
  72. }
  73. return keys
  74. }
  75. // TextMapPropagator propagates cross-cutting concerns as key-value text
  76. // pairs within a carrier that travels in-band across process boundaries.
  77. type TextMapPropagator interface {
  78. // DO NOT CHANGE: any modification will not be backwards compatible and
  79. // must never be done outside of a new major release.
  80. // Inject set cross-cutting concerns from the Context into the carrier.
  81. Inject(ctx context.Context, carrier TextMapCarrier)
  82. // DO NOT CHANGE: any modification will not be backwards compatible and
  83. // must never be done outside of a new major release.
  84. // Extract reads cross-cutting concerns from the carrier into a Context.
  85. Extract(ctx context.Context, carrier TextMapCarrier) context.Context
  86. // DO NOT CHANGE: any modification will not be backwards compatible and
  87. // must never be done outside of a new major release.
  88. // Fields returns the keys whose values are set with Inject.
  89. Fields() []string
  90. // DO NOT CHANGE: any modification will not be backwards compatible and
  91. // must never be done outside of a new major release.
  92. }
  93. type compositeTextMapPropagator []TextMapPropagator
  94. func (p compositeTextMapPropagator) Inject(ctx context.Context, carrier TextMapCarrier) {
  95. for _, i := range p {
  96. i.Inject(ctx, carrier)
  97. }
  98. }
  99. func (p compositeTextMapPropagator) Extract(ctx context.Context, carrier TextMapCarrier) context.Context {
  100. for _, i := range p {
  101. ctx = i.Extract(ctx, carrier)
  102. }
  103. return ctx
  104. }
  105. func (p compositeTextMapPropagator) Fields() []string {
  106. unique := make(map[string]struct{})
  107. for _, i := range p {
  108. for _, k := range i.Fields() {
  109. unique[k] = struct{}{}
  110. }
  111. }
  112. fields := make([]string, 0, len(unique))
  113. for k := range unique {
  114. fields = append(fields, k)
  115. }
  116. return fields
  117. }
  118. // NewCompositeTextMapPropagator returns a unified TextMapPropagator from the
  119. // group of passed TextMapPropagator. This allows different cross-cutting
  120. // concerns to be propagates in a unified manner.
  121. //
  122. // The returned TextMapPropagator will inject and extract cross-cutting
  123. // concerns in the order the TextMapPropagators were provided. Additionally,
  124. // the Fields method will return a de-duplicated slice of the keys that are
  125. // set with the Inject method.
  126. func NewCompositeTextMapPropagator(p ...TextMapPropagator) TextMapPropagator {
  127. return compositeTextMapPropagator(p)
  128. }