UNPKG

5.05 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var types_1 = require("@sentry/types");
3var utils_1 = require("@sentry/utils");
4var hub_1 = require("./hub");
5/**
6 * @inheritdoc
7 */
8var SessionFlusher = /** @class */ (function () {
9 function SessionFlusher(transport, attrs) {
10 var _this = this;
11 this.flushTimeout = 60;
12 this._pendingAggregates = {};
13 this._isEnabled = true;
14 this._transport = transport;
15 // Call to setInterval, so that flush is called every 60 seconds
16 this._intervalId = setInterval(function () { return _this.flush(); }, this.flushTimeout * 1000);
17 this._sessionAttrs = attrs;
18 }
19 /** Sends session aggregates to Transport */
20 SessionFlusher.prototype.sendSessionAggregates = function (sessionAggregates) {
21 if (!this._transport.sendSession) {
22 utils_1.logger.warn("Dropping session because custom transport doesn't implement sendSession");
23 return;
24 }
25 void this._transport.sendSession(sessionAggregates).then(null, function (reason) {
26 utils_1.logger.error("Error while sending session: " + reason);
27 });
28 };
29 /** Checks if `pendingAggregates` has entries, and if it does flushes them by calling `sendSessions` */
30 SessionFlusher.prototype.flush = function () {
31 var sessionAggregates = this.getSessionAggregates();
32 if (sessionAggregates.aggregates.length === 0) {
33 return;
34 }
35 this._pendingAggregates = {};
36 this.sendSessionAggregates(sessionAggregates);
37 };
38 /** Massages the entries in `pendingAggregates` and returns aggregated sessions */
39 SessionFlusher.prototype.getSessionAggregates = function () {
40 var _this = this;
41 var aggregates = Object.keys(this._pendingAggregates).map(function (key) {
42 return _this._pendingAggregates[parseInt(key)];
43 });
44 var sessionAggregates = {
45 attrs: this._sessionAttrs,
46 aggregates: aggregates,
47 };
48 return utils_1.dropUndefinedKeys(sessionAggregates);
49 };
50 /** JSDoc */
51 SessionFlusher.prototype.close = function () {
52 clearInterval(this._intervalId);
53 this._isEnabled = false;
54 this.flush();
55 };
56 /**
57 * Wrapper function for _incrementSessionStatusCount that checks if the instance of SessionFlusher is enabled then
58 * fetches the session status of the request from `Scope.getRequestSession().status` on the scope and passes them to
59 * `_incrementSessionStatusCount` along with the start date
60 */
61 SessionFlusher.prototype.incrementSessionStatusCount = function () {
62 var _a, _b;
63 if (!this._isEnabled) {
64 return;
65 }
66 var scope = hub_1.getCurrentHub().getScope();
67 var requestSession = (_a = scope) === null || _a === void 0 ? void 0 : _a.getRequestSession();
68 if (requestSession && requestSession.status) {
69 this._incrementSessionStatusCount(requestSession.status, new Date());
70 // This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in
71 // case captureRequestSession is called more than once to prevent double count
72 (_b = scope) === null || _b === void 0 ? void 0 : _b.setRequestSession(undefined);
73 /* eslint-enable @typescript-eslint/no-unsafe-member-access */
74 }
75 };
76 /**
77 * Increments status bucket in pendingAggregates buffer (internal state) corresponding to status of
78 * the session received
79 */
80 SessionFlusher.prototype._incrementSessionStatusCount = function (status, date) {
81 // Truncate minutes and seconds on Session Started attribute to have one minute bucket keys
82 var sessionStartedTrunc = new Date(date).setSeconds(0, 0);
83 this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {};
84 // corresponds to aggregated sessions in one specific minute bucket
85 // for example, {"started":"2021-03-16T08:00:00.000Z","exited":4, "errored": 1}
86 var aggregationCounts = this._pendingAggregates[sessionStartedTrunc];
87 if (!aggregationCounts.started) {
88 aggregationCounts.started = new Date(sessionStartedTrunc).toISOString();
89 }
90 switch (status) {
91 case types_1.RequestSessionStatus.Errored:
92 aggregationCounts.errored = (aggregationCounts.errored || 0) + 1;
93 return aggregationCounts.errored;
94 case types_1.RequestSessionStatus.Ok:
95 aggregationCounts.exited = (aggregationCounts.exited || 0) + 1;
96 return aggregationCounts.exited;
97 case types_1.RequestSessionStatus.Crashed:
98 aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1;
99 return aggregationCounts.crashed;
100 }
101 };
102 return SessionFlusher;
103}());
104exports.SessionFlusher = SessionFlusher;
105//# sourceMappingURL=sessionflusher.js.map
\No newline at end of file