spancontext-utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 { INVALID_SPANID, INVALID_TRACEID } from './invalid-span-constants';
  17. import { NonRecordingSpan } from './NonRecordingSpan';
  18. const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
  19. const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
  20. export function isValidTraceId(traceId) {
  21. return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID;
  22. }
  23. export function isValidSpanId(spanId) {
  24. return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID;
  25. }
  26. /**
  27. * Returns true if this {@link SpanContext} is valid.
  28. * @return true if this {@link SpanContext} is valid.
  29. */
  30. export function isSpanContextValid(spanContext) {
  31. return (isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId));
  32. }
  33. /**
  34. * Wrap the given {@link SpanContext} in a new non-recording {@link Span}
  35. *
  36. * @param spanContext span context to be wrapped
  37. * @returns a new non-recording {@link Span} with the provided context
  38. */
  39. export function wrapSpanContext(spanContext) {
  40. return new NonRecordingSpan(spanContext);
  41. }
  42. //# sourceMappingURL=spancontext-utils.js.map