1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.decorateWithTracer = exports.Tracer = undefined;
|
7 |
|
8 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
9 |
|
10 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
11 |
|
12 | var _nodeUuid = require('node-uuid');
|
13 |
|
14 | var _nodeUuid2 = _interopRequireDefault(_nodeUuid);
|
15 |
|
16 | var _performanceNow = require('performance-now');
|
17 |
|
18 | var _performanceNow2 = _interopRequireDefault(_performanceNow);
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
23 |
|
24 | var Tracer = function () {
|
25 |
|
26 |
|
27 | function Tracer(eventGroupId) {
|
28 | _classCallCheck(this, Tracer);
|
29 |
|
30 | this.gid = eventGroupId;
|
31 | this.events = [];
|
32 | this.startTime = new Date().getTime();
|
33 | this.startHrTime = (0, _performanceNow2.default)();
|
34 | }
|
35 |
|
36 | _createClass(Tracer, [{
|
37 | key: 'logEvent',
|
38 | value: function logEvent(_ref) {
|
39 | var props = _ref.props;
|
40 | var info = _ref.info;
|
41 | var type = _ref.type;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var id = _nodeUuid2.default.v4();
|
47 |
|
48 | var timestamp = (0, _performanceNow2.default)();
|
49 |
|
50 | this.events.push(_extends({ id: id }, props, { type: type, info: info, timestamp: timestamp }));
|
51 |
|
52 | }
|
53 | }, {
|
54 | key: 'startInterval',
|
55 | value: function startInterval(info) {
|
56 | var intervalId = _nodeUuid2.default.v4();
|
57 | var type = 'startInterval';
|
58 | this.logEvent({
|
59 | props: { intervalId: intervalId },
|
60 | info: info,
|
61 | type: type
|
62 | });
|
63 | return intervalId;
|
64 | }
|
65 | }, {
|
66 | key: 'stopInterval',
|
67 | value: function stopInterval(intervalId, info) {
|
68 | var type = 'stopInterval';
|
69 | this.logEvent({
|
70 | props: { intervalId: intervalId },
|
71 | info: info,
|
72 | type: type
|
73 | });
|
74 | }
|
75 | }, {
|
76 | key: 'reportEvents',
|
77 | value: function reportEvents(url) {
|
78 |
|
79 |
|
80 | return {
|
81 | url: url,
|
82 | startTime: this.startTime,
|
83 | startHrTime: this.startHrTime,
|
84 | events: this.events
|
85 | };
|
86 | }
|
87 | }]);
|
88 |
|
89 | return Tracer;
|
90 | }();
|
91 |
|
92 | function decorateWithTracer(fn, tracer, info) {
|
93 | return function () {
|
94 | var intervalId = tracer.startInterval(info);
|
95 | try {
|
96 | var result = fn.apply(undefined, arguments);
|
97 | if (typeof result.then === 'function') {
|
98 | result.then(function (res) {
|
99 | tracer.stopInterval(intervalId, info);
|
100 | return res;
|
101 | }).catch(function (err) {
|
102 |
|
103 | tracer.stopInterval(intervalId, info);
|
104 | throw err;
|
105 | });
|
106 | } else {
|
107 |
|
108 | tracer.stopInterval(intervalId, info);
|
109 | }
|
110 | return result;
|
111 | } catch (e) {
|
112 |
|
113 | tracer.stopInterval(intervalId, info);
|
114 | throw e;
|
115 | }
|
116 | };
|
117 | }
|
118 |
|
119 | exports.Tracer = Tracer;
|
120 | exports.decorateWithTracer = decorateWithTracer; |
\ | No newline at end of file |