NoopTracer.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { getSpanContext, setSpan } from '../trace/context-utils';
  18. import { NonRecordingSpan } from './NonRecordingSpan';
  19. import { isSpanContextValid } from './spancontext-utils';
  20. const contextApi = ContextAPI.getInstance();
  21. /**
  22. * No-op implementations of {@link Tracer}.
  23. */
  24. export class NoopTracer {
  25. // startSpan starts a noop span.
  26. startSpan(name, options, context = contextApi.active()) {
  27. const root = Boolean(options === null || options === void 0 ? void 0 : options.root);
  28. if (root) {
  29. return new NonRecordingSpan();
  30. }
  31. const parentFromContext = context && getSpanContext(context);
  32. if (isSpanContext(parentFromContext) &&
  33. isSpanContextValid(parentFromContext)) {
  34. return new NonRecordingSpan(parentFromContext);
  35. }
  36. else {
  37. return new NonRecordingSpan();
  38. }
  39. }
  40. startActiveSpan(name, arg2, arg3, arg4) {
  41. let opts;
  42. let ctx;
  43. let fn;
  44. if (arguments.length < 2) {
  45. return;
  46. }
  47. else if (arguments.length === 2) {
  48. fn = arg2;
  49. }
  50. else if (arguments.length === 3) {
  51. opts = arg2;
  52. fn = arg3;
  53. }
  54. else {
  55. opts = arg2;
  56. ctx = arg3;
  57. fn = arg4;
  58. }
  59. const parentContext = ctx !== null && ctx !== void 0 ? ctx : contextApi.active();
  60. const span = this.startSpan(name, opts, parentContext);
  61. const contextWithSpanSet = setSpan(parentContext, span);
  62. return contextApi.with(contextWithSpanSet, fn, undefined, span);
  63. }
  64. }
  65. function isSpanContext(spanContext) {
  66. return (typeof spanContext === 'object' &&
  67. typeof spanContext['spanId'] === 'string' &&
  68. typeof spanContext['traceId'] === 'string' &&
  69. typeof spanContext['traceFlags'] === 'number');
  70. }
  71. //# sourceMappingURL=NoopTracer.js.map