context.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  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. switch v := parent.Value(baggageKey).(type) {
  34. case baggageState:
  35. s = v
  36. }
  37. s.setHook = hook
  38. return context.WithValue(parent, baggageKey, s)
  39. }
  40. // ContextWithGetHook returns a copy of parent with hook configured to be
  41. // invoked every time FromContext is called.
  42. //
  43. // Passing nil GetHookFunc creates a context with no get hook to call.
  44. func ContextWithGetHook(parent context.Context, hook GetHookFunc) context.Context {
  45. var s baggageState
  46. switch v := parent.Value(baggageKey).(type) {
  47. case baggageState:
  48. s = v
  49. }
  50. s.getHook = hook
  51. return context.WithValue(parent, baggageKey, s)
  52. }
  53. // ContextWithList returns a copy of parent with baggage. Passing nil list
  54. // returns a context without any baggage.
  55. func ContextWithList(parent context.Context, list List) context.Context {
  56. var s baggageState
  57. switch v := parent.Value(baggageKey).(type) {
  58. case baggageState:
  59. s = v
  60. }
  61. s.list = list
  62. ctx := context.WithValue(parent, baggageKey, s)
  63. if s.setHook != nil {
  64. ctx = s.setHook(ctx, list)
  65. }
  66. return ctx
  67. }
  68. // ListFromContext returns the baggage contained in ctx.
  69. func ListFromContext(ctx context.Context) List {
  70. switch v := ctx.Value(baggageKey).(type) {
  71. case baggageState:
  72. if v.getHook != nil {
  73. return v.getHook(ctx, v.list)
  74. }
  75. return v.list
  76. default:
  77. return nil
  78. }
  79. }