SugaredTracer.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { context, SpanStatusCode } from '../../';
  2. var defaultOnException = function (e, span) {
  3. span.recordException(e);
  4. span.setStatus({
  5. code: SpanStatusCode.ERROR,
  6. });
  7. };
  8. /**
  9. * return a new SugaredTracer created from the supplied one
  10. * @param tracer
  11. */
  12. export function wrapTracer(tracer) {
  13. return new SugaredTracer(tracer);
  14. }
  15. var SugaredTracer = /** @class */ (function () {
  16. function SugaredTracer(tracer) {
  17. this._tracer = tracer;
  18. this.startSpan = tracer.startSpan.bind(this._tracer);
  19. this.startActiveSpan = tracer.startActiveSpan.bind(this._tracer);
  20. }
  21. SugaredTracer.prototype.withActiveSpan = function (name, arg2, arg3, arg4) {
  22. var _a = massageParams(arg2, arg3, arg4), opts = _a.opts, ctx = _a.ctx, fn = _a.fn;
  23. return this._tracer.startActiveSpan(name, opts, ctx, function (span) {
  24. return handleFn(span, opts, fn);
  25. });
  26. };
  27. SugaredTracer.prototype.withSpan = function (name, arg2, arg3, arg4) {
  28. var _a = massageParams(arg2, arg3, arg4), opts = _a.opts, ctx = _a.ctx, fn = _a.fn;
  29. var span = this._tracer.startSpan(name, opts, ctx);
  30. return handleFn(span, opts, fn);
  31. };
  32. return SugaredTracer;
  33. }());
  34. export { SugaredTracer };
  35. /**
  36. * Massages parameters of withSpan and withActiveSpan to allow signature overwrites
  37. * @param arg
  38. * @param arg2
  39. * @param arg3
  40. */
  41. function massageParams(arg, arg2, arg3) {
  42. var opts;
  43. var ctx;
  44. var fn;
  45. if (!arg2 && !arg3) {
  46. fn = arg;
  47. }
  48. else if (!arg3) {
  49. opts = arg;
  50. fn = arg2;
  51. }
  52. else {
  53. opts = arg;
  54. ctx = arg2;
  55. fn = arg3;
  56. }
  57. opts = opts !== null && opts !== void 0 ? opts : {};
  58. ctx = ctx !== null && ctx !== void 0 ? ctx : context.active();
  59. return { opts: opts, ctx: ctx, fn: fn };
  60. }
  61. /**
  62. * Executes fn, returns results and runs onException in the case of exception to allow overwriting of error handling
  63. * @param span
  64. * @param opts
  65. * @param fn
  66. */
  67. function handleFn(span, opts, fn) {
  68. var _a;
  69. var onException = (_a = opts.onException) !== null && _a !== void 0 ? _a : defaultOnException;
  70. var errorHandler = function (e) {
  71. onException(e, span);
  72. span.end();
  73. throw e;
  74. };
  75. try {
  76. var ret = fn(span);
  77. // if fn is an async function, attach a recordException and spanEnd callback to the promise
  78. if (typeof (ret === null || ret === void 0 ? void 0 : ret.then) === 'function') {
  79. return ret.then(function (val) {
  80. span.end();
  81. return val;
  82. }, errorHandler);
  83. }
  84. span.end();
  85. return ret;
  86. }
  87. catch (e) {
  88. // add throw to signal the compiler that this will throw in the inner scope
  89. throw errorHandler(e);
  90. }
  91. }
  92. //# sourceMappingURL=SugaredTracer.js.map