UNPKG

4.22 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.decorateWithTracer = exports.Tracer = undefined;
7
8var _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
10var _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
12var _nodeUuid = require('node-uuid');
13
14var _nodeUuid2 = _interopRequireDefault(_nodeUuid);
15
16var _performanceNow = require('performance-now');
17
18var _performanceNow2 = _interopRequireDefault(_performanceNow);
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
24var Tracer = function () {
25 // @eventGroupId: an id to group the events of this tracer together
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 // TODO ensure props is a valid props thingy
44 // TODO ensure info is a valid info thingy
45 // TODO ensure type is a valid type thingy
46 var id = _nodeUuid2.default.v4();
47 // TODO make sure we know what that timestamp is relative to
48 var timestamp = (0, _performanceNow2.default)();
49 // const timestamp = (new Date()).getTime();
50 this.events.push(_extends({ id: id }, props, { type: type, info: info, timestamp: timestamp }));
51 // console.log(this.gid, 'logged event', type, info, 'at', timestamp);
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 // send the serialized events to url;
79 // console.log(`reporting to ${url}`);
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
92function 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 // console.log('whoa, it threw an error!');
103 tracer.stopInterval(intervalId, info);
104 throw err;
105 });
106 } else {
107 // console.log('did not return a promise. logging now');
108 tracer.stopInterval(intervalId, info);
109 }
110 return result;
111 } catch (e) {
112 // console.log('yeah, it errored directly');
113 tracer.stopInterval(intervalId, info);
114 throw e;
115 }
116 };
117}
118
119exports.Tracer = Tracer;
120exports.decorateWithTracer = decorateWithTracer;
\No newline at end of file