trace-event.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. /**
  3. * trace-event - A library to create a trace of your node app per
  4. * Google's Trace Event format:
  5. * // JSSTYLED
  6. * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
  7. */
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. var tslib_1 = require("tslib");
  10. var stream_1 = require("stream");
  11. function evCommon() {
  12. var hrtime = process.hrtime(); // [seconds, nanoseconds]
  13. var ts = hrtime[0] * 1000000 + Math.round(hrtime[1] / 1000); // microseconds
  14. return {
  15. ts: ts,
  16. pid: process.pid,
  17. tid: process.pid // no meaningful tid for node.js
  18. };
  19. }
  20. var Tracer = /** @class */ (function (_super) {
  21. tslib_1.__extends(Tracer, _super);
  22. function Tracer(opts) {
  23. if (opts === void 0) { opts = {}; }
  24. var _this = _super.call(this) || this;
  25. _this.noStream = false;
  26. _this.events = [];
  27. if (typeof opts !== "object") {
  28. throw new Error("Invalid options passed (must be an object)");
  29. }
  30. if (opts.parent != null && typeof opts.parent !== "object") {
  31. throw new Error("Invalid option (parent) passed (must be an object)");
  32. }
  33. if (opts.fields != null && typeof opts.fields !== "object") {
  34. throw new Error("Invalid option (fields) passed (must be an object)");
  35. }
  36. if (opts.objectMode != null &&
  37. (opts.objectMode !== true && opts.objectMode !== false)) {
  38. throw new Error("Invalid option (objectsMode) passed (must be a boolean)");
  39. }
  40. _this.noStream = opts.noStream || false;
  41. _this.parent = opts.parent;
  42. if (_this.parent) {
  43. _this.fields = Object.assign({}, opts.parent && opts.parent.fields);
  44. }
  45. else {
  46. _this.fields = {};
  47. }
  48. if (opts.fields) {
  49. Object.assign(_this.fields, opts.fields);
  50. }
  51. if (!_this.fields.cat) {
  52. // trace-viewer *requires* `cat`, so let's have a fallback.
  53. _this.fields.cat = "default";
  54. }
  55. else if (Array.isArray(_this.fields.cat)) {
  56. _this.fields.cat = _this.fields.cat.join(",");
  57. }
  58. if (!_this.fields.args) {
  59. // trace-viewer *requires* `args`, so let's have a fallback.
  60. _this.fields.args = {};
  61. }
  62. if (_this.parent) {
  63. // TODO: Not calling Readable ctor here. Does that cause probs?
  64. // Probably if trying to pipe from the child.
  65. // Might want a serpate TracerChild class for these guys.
  66. _this._push = _this.parent._push.bind(_this.parent);
  67. }
  68. else {
  69. _this._objectMode = Boolean(opts.objectMode);
  70. var streamOpts = { objectMode: _this._objectMode };
  71. if (_this._objectMode) {
  72. _this._push = _this.push;
  73. }
  74. else {
  75. _this._push = _this._pushString;
  76. streamOpts.encoding = "utf8";
  77. }
  78. stream_1.Readable.call(_this, streamOpts);
  79. }
  80. return _this;
  81. }
  82. /**
  83. * If in no streamMode in order to flush out the trace
  84. * you need to call flush.
  85. */
  86. Tracer.prototype.flush = function () {
  87. if (this.noStream === true) {
  88. for (var _i = 0, _a = this.events; _i < _a.length; _i++) {
  89. var evt = _a[_i];
  90. this._push(evt);
  91. }
  92. }
  93. };
  94. Tracer.prototype._read = function (_) { };
  95. Tracer.prototype._pushString = function (ev) {
  96. if (!this.firstPush) {
  97. this.push("[");
  98. this.firstPush = true;
  99. }
  100. this.push(JSON.stringify(ev) + ",\n", "utf8");
  101. };
  102. // TODO Perhaps figure out getting a trailing ']' without ',]' if helpful.
  103. //Tracer.prototype._flush = function _flush() {
  104. // if (!this._objectMode) {
  105. // this.push(']')
  106. // }
  107. //};
  108. Tracer.prototype.child = function (fields) {
  109. return new Tracer({
  110. parent: this,
  111. fields: fields
  112. });
  113. };
  114. Tracer.prototype.begin = function (fields) {
  115. return this.mkEventFunc("b")(fields);
  116. };
  117. Tracer.prototype.end = function (fields) {
  118. return this.mkEventFunc("e")(fields);
  119. };
  120. Tracer.prototype.completeEvent = function (fields) {
  121. return this.mkEventFunc("X")(fields);
  122. };
  123. Tracer.prototype.instantEvent = function (fields) {
  124. return this.mkEventFunc("I")(fields);
  125. };
  126. Tracer.prototype.mkEventFunc = function (ph) {
  127. var _this = this;
  128. return function (fields) {
  129. var ev = evCommon();
  130. // Assign the event phase.
  131. ev.ph = ph;
  132. if (fields) {
  133. if (typeof fields === "string") {
  134. ev.name = fields;
  135. }
  136. else {
  137. for (var _i = 0, _a = Object.keys(fields); _i < _a.length; _i++) {
  138. var k = _a[_i];
  139. if (k === "cat") {
  140. ev.cat = fields.cat.join(",");
  141. }
  142. else {
  143. ev[k] = fields[k];
  144. }
  145. }
  146. }
  147. }
  148. if (!_this.noStream) {
  149. _this._push(ev);
  150. }
  151. else {
  152. _this.events.push(ev);
  153. }
  154. };
  155. };
  156. return Tracer;
  157. }(stream_1.Readable));
  158. exports.Tracer = Tracer;
  159. /*
  160. * These correspond to the "Async events" in the Trace Events doc.
  161. *
  162. * Required fields:
  163. * - name
  164. * - id
  165. *
  166. * Optional fields:
  167. * - cat (array)
  168. * - args (object)
  169. * - TODO: stack fields, other optional fields?
  170. *
  171. * Dev Note: We don't explicitly assert that correct fields are
  172. * used for speed (premature optimization alert!).
  173. */
  174. //# sourceMappingURL=trace-event.js.map