state.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 global
  15. import (
  16. "sync"
  17. "sync/atomic"
  18. "go.opentelemetry.io/otel/propagation"
  19. "go.opentelemetry.io/otel/trace"
  20. )
  21. type (
  22. tracerProviderHolder struct {
  23. tp trace.TracerProvider
  24. }
  25. propagatorsHolder struct {
  26. tm propagation.TextMapPropagator
  27. }
  28. )
  29. var (
  30. globalTracer = defaultTracerValue()
  31. globalPropagators = defaultPropagatorsValue()
  32. delegateTraceOnce sync.Once
  33. delegateTextMapPropagatorOnce sync.Once
  34. )
  35. // TracerProvider is the internal implementation for global.TracerProvider.
  36. func TracerProvider() trace.TracerProvider {
  37. return globalTracer.Load().(tracerProviderHolder).tp
  38. }
  39. // SetTracerProvider is the internal implementation for global.SetTracerProvider.
  40. func SetTracerProvider(tp trace.TracerProvider) {
  41. delegateTraceOnce.Do(func() {
  42. current := TracerProvider()
  43. if current == tp {
  44. // Setting the provider to the prior default is nonsense, panic.
  45. // Panic is acceptable because we are likely still early in the
  46. // process lifetime.
  47. panic("invalid TracerProvider, the global instance cannot be reinstalled")
  48. } else if def, ok := current.(*tracerProvider); ok {
  49. def.setDelegate(tp)
  50. }
  51. })
  52. globalTracer.Store(tracerProviderHolder{tp: tp})
  53. }
  54. // TextMapPropagator is the internal implementation for global.TextMapPropagator.
  55. func TextMapPropagator() propagation.TextMapPropagator {
  56. return globalPropagators.Load().(propagatorsHolder).tm
  57. }
  58. // SetTextMapPropagator is the internal implementation for global.SetTextMapPropagator.
  59. func SetTextMapPropagator(p propagation.TextMapPropagator) {
  60. // For the textMapPropagator already returned by TextMapPropagator
  61. // delegate to p.
  62. delegateTextMapPropagatorOnce.Do(func() {
  63. if current := TextMapPropagator(); current == p {
  64. // Setting the provider to the prior default is nonsense, panic.
  65. // Panic is acceptable because we are likely still early in the
  66. // process lifetime.
  67. panic("invalid TextMapPropagator, the global instance cannot be reinstalled")
  68. } else if def, ok := current.(*textMapPropagator); ok {
  69. def.SetDelegate(p)
  70. }
  71. })
  72. // Return p when subsequent calls to TextMapPropagator are made.
  73. globalPropagators.Store(propagatorsHolder{tm: p})
  74. }
  75. func defaultTracerValue() *atomic.Value {
  76. v := &atomic.Value{}
  77. v.Store(tracerProviderHolder{tp: &tracerProvider{}})
  78. return v
  79. }
  80. func defaultPropagatorsValue() *atomic.Value {
  81. v := &atomic.Value{}
  82. v.Store(propagatorsHolder{tm: newTextMapPropagator()})
  83. return v
  84. }
  85. // ResetForTest restores the initial global state, for testing purposes.
  86. func ResetForTest() {
  87. globalTracer = defaultTracerValue()
  88. globalPropagators = defaultPropagatorsValue()
  89. delegateTraceOnce = sync.Once{}
  90. delegateTextMapPropagatorOnce = sync.Once{}
  91. }