NoopTracer.js 2.7 KB

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