1 |
3a515b92
|
cagy
|
"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 |
|
|
this._flush();
|
93 |
|
|
}
|
94 |
|
|
};
|
95 |
|
|
Tracer.prototype._read = function (_) { };
|
96 |
|
|
Tracer.prototype._pushString = function (ev) {
|
97 |
|
|
var separator = "";
|
98 |
|
|
if (!this.firstPush) {
|
99 |
|
|
this.push("[");
|
100 |
|
|
this.firstPush = true;
|
101 |
|
|
}
|
102 |
|
|
else {
|
103 |
|
|
separator = ",\n";
|
104 |
|
|
}
|
105 |
|
|
this.push(separator + JSON.stringify(ev), "utf8");
|
106 |
|
|
};
|
107 |
|
|
Tracer.prototype._flush = function () {
|
108 |
|
|
if (!this._objectMode) {
|
109 |
|
|
this.push("]");
|
110 |
|
|
}
|
111 |
|
|
};
|
112 |
|
|
Tracer.prototype.child = function (fields) {
|
113 |
|
|
return new Tracer({
|
114 |
|
|
parent: this,
|
115 |
|
|
fields: fields
|
116 |
|
|
});
|
117 |
|
|
};
|
118 |
|
|
Tracer.prototype.begin = function (fields) {
|
119 |
|
|
return this.mkEventFunc("b")(fields);
|
120 |
|
|
};
|
121 |
|
|
Tracer.prototype.end = function (fields) {
|
122 |
|
|
return this.mkEventFunc("e")(fields);
|
123 |
|
|
};
|
124 |
|
|
Tracer.prototype.completeEvent = function (fields) {
|
125 |
|
|
return this.mkEventFunc("X")(fields);
|
126 |
|
|
};
|
127 |
|
|
Tracer.prototype.instantEvent = function (fields) {
|
128 |
|
|
return this.mkEventFunc("I")(fields);
|
129 |
|
|
};
|
130 |
|
|
Tracer.prototype.mkEventFunc = function (ph) {
|
131 |
|
|
var _this = this;
|
132 |
|
|
return function (fields) {
|
133 |
|
|
var ev = evCommon();
|
134 |
|
|
// Assign the event phase.
|
135 |
|
|
ev.ph = ph;
|
136 |
|
|
if (fields) {
|
137 |
|
|
if (typeof fields === "string") {
|
138 |
|
|
ev.name = fields;
|
139 |
|
|
}
|
140 |
|
|
else {
|
141 |
|
|
for (var _i = 0, _a = Object.keys(fields); _i < _a.length; _i++) {
|
142 |
|
|
var k = _a[_i];
|
143 |
|
|
if (k === "cat") {
|
144 |
|
|
ev.cat = fields.cat.join(",");
|
145 |
|
|
}
|
146 |
|
|
else {
|
147 |
|
|
ev[k] = fields[k];
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
if (!_this.noStream) {
|
153 |
|
|
_this._push(ev);
|
154 |
|
|
}
|
155 |
|
|
else {
|
156 |
|
|
_this.events.push(ev);
|
157 |
|
|
}
|
158 |
|
|
};
|
159 |
|
|
};
|
160 |
|
|
return Tracer;
|
161 |
|
|
}(stream_1.Readable));
|
162 |
|
|
exports.Tracer = Tracer;
|
163 |
|
|
/*
|
164 |
|
|
* These correspond to the "Async events" in the Trace Events doc.
|
165 |
|
|
*
|
166 |
|
|
* Required fields:
|
167 |
|
|
* - name
|
168 |
|
|
* - id
|
169 |
|
|
*
|
170 |
|
|
* Optional fields:
|
171 |
|
|
* - cat (array)
|
172 |
|
|
* - args (object)
|
173 |
|
|
* - TODO: stack fields, other optional fields?
|
174 |
|
|
*
|
175 |
|
|
* Dev Note: We don't explicitly assert that correct fields are
|
176 |
|
|
* used for speed (premature optimization alert!).
|
177 |
|
|
*/
|
178 |
|
|
//# sourceMappingURL=trace-event.js.map
|