context-helpers.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { ContextAPI } from '../api/context';
  17. import { createContextKey } from '../context/context';
  18. /**
  19. * Baggage key
  20. */
  21. const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
  22. /**
  23. * Retrieve the current baggage from the given context
  24. *
  25. * @param {Context} Context that manage all context values
  26. * @returns {Baggage} Extracted baggage from the context
  27. */
  28. export function getBaggage(context) {
  29. return context.getValue(BAGGAGE_KEY) || undefined;
  30. }
  31. /**
  32. * Retrieve the current baggage from the active/current context
  33. *
  34. * @returns {Baggage} Extracted baggage from the context
  35. */
  36. export function getActiveBaggage() {
  37. return getBaggage(ContextAPI.getInstance().active());
  38. }
  39. /**
  40. * Store a baggage in the given context
  41. *
  42. * @param {Context} Context that manage all context values
  43. * @param {Baggage} baggage that will be set in the actual context
  44. */
  45. export function setBaggage(context, baggage) {
  46. return context.setValue(BAGGAGE_KEY, baggage);
  47. }
  48. /**
  49. * Delete the baggage stored in the given context
  50. *
  51. * @param {Context} Context that manage all context values
  52. */
  53. export function deleteBaggage(context) {
  54. return context.deleteValue(BAGGAGE_KEY);
  55. }
  56. //# sourceMappingURL=context-helpers.js.map