context.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 baggage // import "go.opentelemetry.io/otel/internal/baggage"
  15. import "context"
  16. type baggageContextKeyType int
  17. const baggageKey baggageContextKeyType = iota
  18. // SetHookFunc is a callback called when storing baggage in the context.
  19. type SetHookFunc func(context.Context, List) context.Context
  20. // GetHookFunc is a callback called when getting baggage from the context.
  21. type GetHookFunc func(context.Context, List) List
  22. type baggageState struct {
  23. list List
  24. setHook SetHookFunc
  25. getHook GetHookFunc
  26. }
  27. // ContextWithSetHook returns a copy of parent with hook configured to be
  28. // invoked every time ContextWithBaggage is called.
  29. //
  30. // Passing nil SetHookFunc creates a context with no set hook to call.
  31. func ContextWithSetHook(parent context.Context, hook SetHookFunc) context.Context {
  32. var s baggageState
  33. if v, ok := parent.Value(baggageKey).(baggageState); ok {
  34. s = v
  35. }
  36. s.setHook = hook
  37. return context.WithValue(parent, baggageKey, s)
  38. }
  39. // ContextWithGetHook returns a copy of parent with hook configured to be
  40. // invoked every time FromContext is called.
  41. //
  42. // Passing nil GetHookFunc creates a context with no get hook to call.
  43. func ContextWithGetHook(parent context.Context, hook GetHookFunc) context.Context {
  44. var s baggageState
  45. if v, ok := parent.Value(baggageKey).(baggageState); ok {
  46. s = v
  47. }
  48. s.getHook = hook
  49. return context.WithValue(parent, baggageKey, s)
  50. }
  51. // ContextWithList returns a copy of parent with baggage. Passing nil list
  52. // returns a context without any baggage.
  53. func ContextWithList(parent context.Context, list List) context.Context {
  54. var s baggageState
  55. if v, ok := parent.Value(baggageKey).(baggageState); ok {
  56. s = v
  57. }
  58. s.list = list
  59. ctx := context.WithValue(parent, baggageKey, s)
  60. if s.setHook != nil {
  61. ctx = s.setHook(ctx, list)
  62. }
  63. return ctx
  64. }
  65. // ListFromContext returns the baggage contained in ctx.
  66. func ListFromContext(ctx context.Context) List {
  67. switch v := ctx.Value(baggageKey).(type) {
  68. case baggageState:
  69. if v.getHook != nil {
  70. return v.getHook(ctx, v.list)
  71. }
  72. return v.list
  73. default:
  74. return nil
  75. }
  76. }