Skip to content

Commit bd4dba1

Browse files
committed
feat(core): Deprecate APIs around RequestSessions
1 parent 0b349eb commit bd4dba1

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

docs/migration/draft-v9-migration-guide.md

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
- Deprecated `addTracingHeadersToFetchRequest` method - this was only meant for internal use and is not needed anymore.
4848
- Deprecated `generatePropagationContext()` in favor of using `generateTraceId()` directly.
4949
- Deprecated `spanId` field on `propagationContext` - this field will be removed in v9, and should neither be read or set anymore.
50+
- Deprecated `RequestSession` type. No replacements.
51+
- Deprecated `RequestSessionStatus` type. No replacements.
52+
- Deprecated `SessionFlusherLike` type. No replacements.
53+
- Deprecated `SessionFlusher`. No replacements.
5054

5155
## `@sentry/nestjs`
5256

@@ -69,6 +73,9 @@
6973
- **The `@sentry/types` package has been deprecated. Import everything from `@sentry/core` instead.**
7074

7175
- Deprecated `Request` in favor of `RequestEventData`.
76+
- Deprecated `RequestSession`. No replacements.
77+
- Deprecated `RequestSessionStatus`. No replacements.
78+
- Deprecated `SessionFlusherLike`. No replacements.
7279

7380
## `@sentry/nuxt`
7481

packages/core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export {
4747
export { setAsyncContextStrategy } from './asyncContext';
4848
export { getMainCarrier } from './carrier';
4949
export { makeSession, closeSession, updateSession } from './session';
50+
// eslint-disable-next-line deprecation/deprecation
5051
export { SessionFlusher } from './sessionflusher';
5152
export { Scope } from './scope';
5253
export { notifyEventProcessors } from './eventProcessors';
@@ -92,6 +93,7 @@ export { inboundFiltersIntegration } from './integrations/inboundfilters';
9293
export { linkedErrorsIntegration } from './integrations/linkederrors';
9394
export { moduleMetadataIntegration } from './integrations/metadata';
9495
export { requestDataIntegration } from './integrations/requestdata';
96+
export { serverRequestSessionIntegration } from './integrations/serverrequestsession';
9597
export { captureConsoleIntegration } from './integrations/captureconsole';
9698
// eslint-disable-next-line deprecation/deprecation
9799
export { debugIntegration } from './integrations/debug';

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportO
4242
export class ServerRuntimeClient<
4343
O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions,
4444
> extends BaseClient<O> {
45+
// eslint-disable-next-line deprecation/deprecation
4546
protected _sessionFlusher: SessionFlusher | undefined;
4647

4748
/**
@@ -80,10 +81,12 @@ export class ServerRuntimeClient<
8081
*/
8182
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8283
public captureException(exception: any, hint?: EventHint, scope?: Scope): string {
83-
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
84-
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
85-
// sent to the Server only when the `requestHandler` middleware is used
86-
if (this._options.autoSessionTracking && this._sessionFlusher) {
84+
// Check if `_sessionFlusher` exists because it is initialized (defined) only when the `autoSessionTracking` is enabled.
85+
// The expectation is that session aggregates are only sent when `autoSessionTracking` is enabled.
86+
// TODO(v9): Our goal in the future is to not have the `autoSessionTracking` option and instead rely on integrations doing the creation and sending of sessions. We will not have a central kill-switch for sessions.
87+
// TODO(v9): This should move into the httpIntegration.
88+
if (this._sessionFlusher) {
89+
// eslint-disable-next-line deprecation/deprecation
8790
const requestSession = getIsolationScope().getRequestSession();
8891

8992
// Necessary checks to ensure this is code block is executed only within a request
@@ -100,16 +103,18 @@ export class ServerRuntimeClient<
100103
* @inheritDoc
101104
*/
102105
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string {
103-
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
104-
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
105-
// sent to the Server only when the `requestHandler` middleware is used
106-
if (this._options.autoSessionTracking && this._sessionFlusher) {
106+
// Check if `_sessionFlusher` exists because it is initialized only when the `autoSessionTracking` is enabled.
107+
// The expectation is that session aggregates are only sent when `autoSessionTracking` is enabled.
108+
// TODO(v9): Our goal in the future is to not have the `autoSessionTracking` option and instead rely on integrations doing the creation and sending of sessions. We will not have a central kill-switch for sessions.
109+
// TODO(v9): This should move into the httpIntegration.
110+
if (this._sessionFlusher) {
107111
const eventType = event.type || 'exception';
108112
const isException =
109113
eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;
110114

111115
// If the event is of type Exception, then a request session should be captured
112116
if (isException) {
117+
// eslint-disable-next-line deprecation/deprecation
113118
const requestSession = getIsolationScope().getRequestSession();
114119

115120
// Ensure that this is happening within the bounds of a request, and make sure not to override
@@ -134,12 +139,19 @@ export class ServerRuntimeClient<
134139
return super.close(timeout);
135140
}
136141

137-
/** Method that initialises an instance of SessionFlusher on Client */
142+
/**
143+
* Initializes an instance of SessionFlusher on the client which will aggregate and periodically flush session data.
144+
*
145+
* NOTICE: This method will implicitly create an interval that is periodically called.
146+
* To clean up this resources, call `.close()` when you no longer intend to use the client.
147+
* Not doing so will result in a memory leak.
148+
*/
138149
public initSessionFlusher(): void {
139150
const { release, environment } = this._options;
140151
if (!release) {
141-
DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
152+
DEBUG_BUILD && logger.warn('Cannot initialize an instance of SessionFlusher if no release is provided!');
142153
} else {
154+
// eslint-disable-next-line deprecation/deprecation
143155
this._sessionFlusher = new SessionFlusher(this, {
144156
release,
145157
environment,
@@ -214,6 +226,8 @@ export class ServerRuntimeClient<
214226
/**
215227
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
216228
* appropriate session aggregates bucket
229+
*
230+
* @deprecated This method should not be used or extended. It's functionality will move into the `httpIntegration` and not be part of any public API.
217231
*/
218232
protected _captureRequestSession(): void {
219233
if (!this._sessionFlusher) {

packages/core/src/sessionflusher.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ type ReleaseHealthAttributes = {
1414
};
1515

1616
/**
17-
* @inheritdoc
17+
* @deprecated `SessionFlusher` is deprecated and will be removed in the next major version of the SDK.
1818
*/
19+
// TODO(v9): The goal for the SessionFlusher is to become a stupidly simple mechanism to aggregate "Sessions" (actually "RequestSessions"). It should probably live directly inside the Http integration/instrumentation.
20+
// eslint-disable-next-line deprecation/deprecation
1921
export class SessionFlusher implements SessionFlusherLike {
2022
public readonly flushTimeout: number;
2123
private _pendingAggregates: Map<number, AggregationCounts>;
@@ -80,12 +82,14 @@ export class SessionFlusher implements SessionFlusherLike {
8082
return;
8183
}
8284
const isolationScope = getIsolationScope();
85+
// eslint-disable-next-line deprecation/deprecation
8386
const requestSession = isolationScope.getRequestSession();
8487

8588
if (requestSession && requestSession.status) {
8689
this._incrementSessionStatusCount(requestSession.status, new Date());
8790
// This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in
8891
// case captureRequestSession is called more than once to prevent double count
92+
// eslint-disable-next-line deprecation/deprecation
8993
isolationScope.setRequestSession(undefined);
9094
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
9195
}
@@ -95,6 +99,7 @@ export class SessionFlusher implements SessionFlusherLike {
9599
* Increments status bucket in pendingAggregates buffer (internal state) corresponding to status of
96100
* the session received
97101
*/
102+
// eslint-disable-next-line deprecation/deprecation
98103
private _incrementSessionStatusCount(status: RequestSessionStatus, date: Date): number {
99104
// Truncate minutes and seconds on Session Started attribute to have one minute bucket keys
100105
const sessionStartedTrunc = new Date(date).setSeconds(0, 0);

packages/core/src/types-hoist/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ export type {
104104
Session,
105105
SessionContext,
106106
SessionStatus,
107+
// eslint-disable-next-line deprecation/deprecation
107108
RequestSession,
109+
// eslint-disable-next-line deprecation/deprecation
108110
RequestSessionStatus,
111+
// eslint-disable-next-line deprecation/deprecation
109112
SessionFlusherLike,
110113
SerializedSession,
111114
} from './session';

packages/core/src/types-hoist/scope.ts

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ScopeContext {
2323
contexts: Contexts;
2424
tags: { [key: string]: Primitive };
2525
fingerprint: string[];
26+
// eslint-disable-next-line deprecation/deprecation
2627
requestSession: RequestSession;
2728
propagationContext: PropagationContext;
2829
}
@@ -168,12 +169,18 @@ export interface Scope {
168169

169170
/**
170171
* Returns the `RequestSession` if there is one
172+
*
173+
* @deprecated Use `getSession()` and `setSession()` instead of `getRequestSession()` and `setRequestSession()`;
171174
*/
175+
// eslint-disable-next-line deprecation/deprecation
172176
getRequestSession(): RequestSession | undefined;
173177

174178
/**
175179
* Sets the `RequestSession` on the scope
180+
*
181+
* @deprecated Use `getSession()` and `setSession()` instead of `getRequestSession()` and `setRequestSession()`;
176182
*/
183+
// eslint-disable-next-line deprecation/deprecation
177184
setRequestSession(requestSession?: RequestSession): this;
178185

179186
/**

packages/core/src/types-hoist/session.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { User } from './user';
22

3+
/**
4+
* @deprecated This type is deprecated and will be removed in the next major version of the SDK.
5+
*/
36
export interface RequestSession {
7+
// eslint-disable-next-line deprecation/deprecation
48
status?: RequestSessionStatus;
59
}
610

@@ -35,6 +39,10 @@ export interface Session {
3539
export type SessionContext = Partial<Session>;
3640

3741
export type SessionStatus = 'ok' | 'exited' | 'crashed' | 'abnormal';
42+
43+
/**
44+
* @deprecated This type is deprecated and will be removed in the next major version of the SDK.
45+
*/
3846
export type RequestSessionStatus = 'ok' | 'errored' | 'crashed';
3947

4048
/** JSDoc */
@@ -46,6 +54,9 @@ export interface SessionAggregates {
4654
aggregates: Array<AggregationCounts>;
4755
}
4856

57+
/**
58+
* @deprecated This type is deprecated and will be removed in the next major version of the SDK.
59+
*/
4960
export interface SessionFlusherLike {
5061
/**
5162
* Increments the Session Status bucket in SessionAggregates Object corresponding to the status of the session

packages/node/src/sdk/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ function _init(
156156

157157
logger.log(`Running in ${isCjs() ? 'CommonJS' : 'ESM'} mode.`);
158158

159+
// TODO(V9): Remove this code since all of the logic should be in an integration
159160
if (options.autoSessionTracking) {
160161
startSessionTracking();
161162
}

0 commit comments

Comments
 (0)