|
1 |
| -import { |
2 |
| - AggregationCounts, |
3 |
| - RequestSessionStatus, |
4 |
| - Session as SessionInterface, |
5 |
| - SessionAggregates, |
6 |
| - SessionContext, |
7 |
| - SessionFlusherLike, |
8 |
| - SessionStatus, |
9 |
| - Transport, |
10 |
| -} from '@sentry/types'; |
11 |
| -import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils'; |
12 |
| - |
13 |
| -import { getCurrentHub } from './hub'; |
| 1 | +import { Session as SessionInterface, SessionContext, SessionStatus } from '@sentry/types'; |
| 2 | +import { dropUndefinedKeys, timestampInSeconds, uuid4 } from '@sentry/utils'; |
14 | 3 |
|
15 | 4 | /**
|
16 | 5 | * @inheritdoc
|
@@ -145,119 +134,3 @@ export class Session implements SessionInterface {
|
145 | 134 | });
|
146 | 135 | }
|
147 | 136 | }
|
148 |
| - |
149 |
| -type ReleaseHealthAttributes = { |
150 |
| - environment?: string; |
151 |
| - release: string; |
152 |
| -}; |
153 |
| - |
154 |
| -/** |
155 |
| - * @inheritdoc |
156 |
| - */ |
157 |
| -export class SessionFlusher implements SessionFlusherLike { |
158 |
| - public readonly flushTimeout: number = 60; |
159 |
| - private _pendingAggregates: Record<number, AggregationCounts> = {}; |
160 |
| - private _sessionAttrs: ReleaseHealthAttributes; |
161 |
| - private _intervalId: ReturnType<typeof setInterval>; |
162 |
| - private _isEnabled: boolean = true; |
163 |
| - private _transport: Transport; |
164 |
| - |
165 |
| - public constructor(transport: Transport, attrs: ReleaseHealthAttributes) { |
166 |
| - this._transport = transport; |
167 |
| - // Call to setInterval, so that flush is called every 60 seconds |
168 |
| - this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000); |
169 |
| - this._sessionAttrs = attrs; |
170 |
| - } |
171 |
| - |
172 |
| - /** Sends session aggregates to Transport */ |
173 |
| - public sendSessionAggregates(sessionAggregates: SessionAggregates): void { |
174 |
| - if (!this._transport.sendSession) { |
175 |
| - logger.warn("Dropping session because custom transport doesn't implement sendSession"); |
176 |
| - return; |
177 |
| - } |
178 |
| - void this._transport.sendSession(sessionAggregates).then(null, reason => { |
179 |
| - logger.error(`Error while sending session: ${reason}`); |
180 |
| - }); |
181 |
| - } |
182 |
| - |
183 |
| - /** Checks if `pendingAggregates` has entries, and if it does flushes them by calling `sendSessions` */ |
184 |
| - public flush(): void { |
185 |
| - const sessionAggregates = this.getSessionAggregates(); |
186 |
| - if (sessionAggregates.aggregates.length === 0) { |
187 |
| - return; |
188 |
| - } |
189 |
| - this._pendingAggregates = {}; |
190 |
| - this.sendSessionAggregates(sessionAggregates); |
191 |
| - } |
192 |
| - |
193 |
| - /** Massages the entries in `pendingAggregates` and returns aggregated sessions */ |
194 |
| - public getSessionAggregates(): SessionAggregates { |
195 |
| - const aggregates: AggregationCounts[] = Object.keys(this._pendingAggregates).map((key: string) => { |
196 |
| - return this._pendingAggregates[parseInt(key)]; |
197 |
| - }); |
198 |
| - |
199 |
| - const sessionAggregates: SessionAggregates = { |
200 |
| - attrs: this._sessionAttrs, |
201 |
| - aggregates, |
202 |
| - }; |
203 |
| - return dropUndefinedKeys(sessionAggregates); |
204 |
| - } |
205 |
| - |
206 |
| - /** JSDoc */ |
207 |
| - public close(): void { |
208 |
| - clearInterval(this._intervalId); |
209 |
| - this._isEnabled = false; |
210 |
| - this.flush(); |
211 |
| - } |
212 |
| - |
213 |
| - /** |
214 |
| - * Wrapper function for _incrementSessionStatusCount that checks if the instance of SessionFlusher is enabled then |
215 |
| - * fetches the session status of the request from `Scope.getRequestSession().status` on the scope and passes them to |
216 |
| - * `_incrementSessionStatusCount` along with the start date |
217 |
| - */ |
218 |
| - public incrementSessionStatusCount(): void { |
219 |
| - if (!this._isEnabled) { |
220 |
| - return; |
221 |
| - } |
222 |
| - const scope = getCurrentHub().getScope(); |
223 |
| - const requestSession = scope?.getRequestSession(); |
224 |
| - |
225 |
| - if (requestSession && requestSession.status) { |
226 |
| - this._incrementSessionStatusCount(requestSession.status, new Date()); |
227 |
| - // This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in |
228 |
| - // case captureRequestSession is called more than once to prevent double count |
229 |
| - scope?.setRequestSession(undefined); |
230 |
| - |
231 |
| - /* eslint-enable @typescript-eslint/no-unsafe-member-access */ |
232 |
| - } |
233 |
| - } |
234 |
| - |
235 |
| - /** |
236 |
| - * Increments status bucket in pendingAggregates buffer (internal state) corresponding to status of |
237 |
| - * the session received |
238 |
| - */ |
239 |
| - private _incrementSessionStatusCount(status: RequestSessionStatus, date: Date): number { |
240 |
| - // Truncate minutes and seconds on Session Started attribute to have one minute bucket keys |
241 |
| - const sessionStartedTrunc = new Date(date).setSeconds(0, 0); |
242 |
| - this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {}; |
243 |
| - |
244 |
| - // corresponds to aggregated sessions in one specific minute bucket |
245 |
| - // for example, {"started":"2021-03-16T08:00:00.000Z","exited":4, "errored": 1} |
246 |
| - const aggregationCounts: AggregationCounts = this._pendingAggregates[sessionStartedTrunc]; |
247 |
| - if (!aggregationCounts.started) { |
248 |
| - aggregationCounts.started = new Date(sessionStartedTrunc).toISOString(); |
249 |
| - } |
250 |
| - |
251 |
| - switch (status) { |
252 |
| - case RequestSessionStatus.Errored: |
253 |
| - aggregationCounts.errored = (aggregationCounts.errored || 0) + 1; |
254 |
| - return aggregationCounts.errored; |
255 |
| - case RequestSessionStatus.Ok: |
256 |
| - aggregationCounts.exited = (aggregationCounts.exited || 0) + 1; |
257 |
| - return aggregationCounts.exited; |
258 |
| - case RequestSessionStatus.Crashed: |
259 |
| - aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1; |
260 |
| - return aggregationCounts.crashed; |
261 |
| - } |
262 |
| - } |
263 |
| -} |
0 commit comments