utils.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 { DiagAPI } from '../api/diag';
  17. import { BaggageImpl } from './internal/baggage-impl';
  18. import { baggageEntryMetadataSymbol } from './internal/symbol';
  19. const diag = DiagAPI.instance();
  20. /**
  21. * Create a new Baggage with optional entries
  22. *
  23. * @param entries An array of baggage entries the new baggage should contain
  24. */
  25. export function createBaggage(entries = {}) {
  26. return new BaggageImpl(new Map(Object.entries(entries)));
  27. }
  28. /**
  29. * Create a serializable BaggageEntryMetadata object from a string.
  30. *
  31. * @param str string metadata. Format is currently not defined by the spec and has no special meaning.
  32. *
  33. */
  34. export function baggageEntryMetadataFromString(str) {
  35. if (typeof str !== 'string') {
  36. diag.error(`Cannot create baggage metadata from unknown type: ${typeof str}`);
  37. str = '';
  38. }
  39. return {
  40. __TYPE__: baggageEntryMetadataSymbol,
  41. toString() {
  42. return str;
  43. },
  44. };
  45. }
  46. //# sourceMappingURL=utils.js.map