Skip to content

Commit 5ff28a6

Browse files
committed
Move request sessions and flusher to ServerRuntimeClient
1 parent 088ea88 commit 5ff28a6

File tree

2 files changed

+89
-88
lines changed

2 files changed

+89
-88
lines changed

packages/core/src/server-runtime-client.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { BaseClient } from './baseclient';
1717
import { createCheckInEnvelope } from './checkin';
1818
import { getCurrentHub } from './hub';
1919
import type { Scope } from './scope';
20+
import { SessionFlusher } from './sessionflusher';
2021
import { addTracingExtensions, getDynamicSamplingContextFromClient } from './tracing';
2122

2223
export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
@@ -31,6 +32,8 @@ export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportO
3132
export class ServerRuntimeClient<
3233
O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions,
3334
> extends BaseClient<O> {
35+
protected _sessionFlusher: SessionFlusher | undefined;
36+
3437
/**
3538
* Creates a new Edge SDK instance.
3639
* @param options Configuration options for this SDK.
@@ -63,6 +66,78 @@ export class ServerRuntimeClient<
6366
);
6467
}
6568

69+
/**
70+
* @inheritDoc
71+
*/
72+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
73+
public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {
74+
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
75+
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
76+
// sent to the Server only when the `requestHandler` middleware is used
77+
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
78+
const requestSession = scope.getRequestSession();
79+
80+
// Necessary checks to ensure this is code block is executed only within a request
81+
// Should override the status only if `requestSession.status` is `Ok`, which is its initial stage
82+
if (requestSession && requestSession.status === 'ok') {
83+
requestSession.status = 'errored';
84+
}
85+
}
86+
87+
return super.captureException(exception, hint, scope);
88+
}
89+
90+
/**
91+
* @inheritDoc
92+
*/
93+
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
94+
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
95+
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
96+
// sent to the Server only when the `requestHandler` middleware is used
97+
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
98+
const eventType = event.type || 'exception';
99+
const isException =
100+
eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;
101+
102+
// If the event is of type Exception, then a request session should be captured
103+
if (isException) {
104+
const requestSession = scope.getRequestSession();
105+
106+
// Ensure that this is happening within the bounds of a request, and make sure not to override
107+
// Session Status if Errored / Crashed
108+
if (requestSession && requestSession.status === 'ok') {
109+
requestSession.status = 'errored';
110+
}
111+
}
112+
}
113+
114+
return super.captureEvent(event, hint, scope);
115+
}
116+
117+
/**
118+
*
119+
* @inheritdoc
120+
*/
121+
public close(timeout?: number): PromiseLike<boolean> {
122+
if (this._sessionFlusher) {
123+
this._sessionFlusher.close();
124+
}
125+
return super.close(timeout);
126+
}
127+
128+
/** Method that initialises an instance of SessionFlusher on Client */
129+
public initSessionFlusher(): void {
130+
const { release, environment } = this._options;
131+
if (!release) {
132+
__DEBUG_BUILD__ && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
133+
} else {
134+
this._sessionFlusher = new SessionFlusher(this, {
135+
release,
136+
environment,
137+
});
138+
}
139+
}
140+
66141
/**
67142
* Create a cron monitor check in and send it to Sentry.
68143
*
@@ -121,6 +196,18 @@ export class ServerRuntimeClient<
121196
return id;
122197
}
123198

199+
/**
200+
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
201+
* appropriate session aggregates bucket
202+
*/
203+
protected _captureRequestSession(): void {
204+
if (!this._sessionFlusher) {
205+
__DEBUG_BUILD__ && logger.warn('Discarded request mode session because autoSessionTracking option was disabled');
206+
} else {
207+
this._sessionFlusher.incrementSessionStatusCount();
208+
}
209+
}
210+
124211
/**
125212
* @inheritDoc
126213
*/

packages/node/src/client.ts

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { Scope, ServerRuntimeClientOptions } from '@sentry/core';
2-
import { SDK_VERSION, ServerRuntimeClient, SessionFlusher } from '@sentry/core';
3-
import type { Event, EventHint } from '@sentry/types';
4-
import { logger } from '@sentry/utils';
1+
import type { ServerRuntimeClientOptions } from '@sentry/core';
2+
import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core';
53
import * as os from 'os';
64
import { TextEncoder } from 'util';
75

@@ -14,8 +12,6 @@ import type { NodeClientOptions } from './types';
1412
* @see SentryClient for usage documentation.
1513
*/
1614
export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
17-
protected _sessionFlusher: SessionFlusher | undefined;
18-
1915
/**
2016
* Creates a new Node SDK instance.
2117
* @param options Configuration options for this SDK.
@@ -48,86 +44,4 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
4844

4945
super(clientOptions);
5046
}
51-
52-
/**
53-
* @inheritDoc
54-
*/
55-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
56-
public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {
57-
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
58-
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
59-
// sent to the Server only when the `requestHandler` middleware is used
60-
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
61-
const requestSession = scope.getRequestSession();
62-
63-
// Necessary checks to ensure this is code block is executed only within a request
64-
// Should override the status only if `requestSession.status` is `Ok`, which is its initial stage
65-
if (requestSession && requestSession.status === 'ok') {
66-
requestSession.status = 'errored';
67-
}
68-
}
69-
70-
return super.captureException(exception, hint, scope);
71-
}
72-
73-
/**
74-
* @inheritDoc
75-
*/
76-
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
77-
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
78-
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
79-
// sent to the Server only when the `requestHandler` middleware is used
80-
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
81-
const eventType = event.type || 'exception';
82-
const isException =
83-
eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;
84-
85-
// If the event is of type Exception, then a request session should be captured
86-
if (isException) {
87-
const requestSession = scope.getRequestSession();
88-
89-
// Ensure that this is happening within the bounds of a request, and make sure not to override
90-
// Session Status if Errored / Crashed
91-
if (requestSession && requestSession.status === 'ok') {
92-
requestSession.status = 'errored';
93-
}
94-
}
95-
}
96-
97-
return super.captureEvent(event, hint, scope);
98-
}
99-
100-
/**
101-
*
102-
* @inheritdoc
103-
*/
104-
public close(timeout?: number): PromiseLike<boolean> {
105-
this._sessionFlusher?.close();
106-
return super.close(timeout);
107-
}
108-
109-
/** Method that initialises an instance of SessionFlusher on Client */
110-
public initSessionFlusher(): void {
111-
const { release, environment } = this._options;
112-
if (!release) {
113-
__DEBUG_BUILD__ && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
114-
} else {
115-
this._sessionFlusher = new SessionFlusher(this, {
116-
release,
117-
environment,
118-
});
119-
}
120-
}
121-
122-
/**
123-
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
124-
* appropriate session aggregates bucket
125-
*/
126-
protected _captureRequestSession(): void {
127-
if (!this._sessionFlusher) {
128-
__DEBUG_BUILD__ && logger.warn('Discarded request mode session because autoSessionTracking option was disabled');
129-
} else {
130-
this._sessionFlusher.incrementSessionStatusCount();
131-
}
132-
}
13347
}

0 commit comments

Comments
 (0)