Skip to content

feat(node): Node client extends ServerRuntimeClient rather than BaseClient #8933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 90 additions & 3 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import type {
SeverityLevel,
TraceContext,
} from '@sentry/types';
import { eventFromMessage, eventFromUnknownInput, logger, uuid4 } from '@sentry/utils';
import { eventFromMessage, eventFromUnknownInput, logger, resolvedSyncPromise, uuid4 } from '@sentry/utils';

import { BaseClient } from './baseclient';
import { createCheckInEnvelope } from './checkin';
import { getCurrentHub } from './hub';
import type { Scope } from './scope';
import { SessionFlusher } from './sessionflusher';
import { addTracingExtensions, getDynamicSamplingContextFromClient } from './tracing';

export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
Expand All @@ -31,6 +32,8 @@ export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportO
export class ServerRuntimeClient<
O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions,
> extends BaseClient<O> {
protected _sessionFlusher: SessionFlusher | undefined;

/**
* Creates a new Edge SDK instance.
* @param options Configuration options for this SDK.
Expand All @@ -46,7 +49,7 @@ export class ServerRuntimeClient<
* @inheritDoc
*/
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
return Promise.resolve(eventFromUnknownInput(getCurrentHub, this._options.stackParser, exception, hint));
return resolvedSyncPromise(eventFromUnknownInput(getCurrentHub, this._options.stackParser, exception, hint));
Copy link
Collaborator Author

@timfish timfish Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cost me hours! 😭

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry you had to go through this! 😭

Thanks for figuring it out!!

}

/**
Expand All @@ -58,11 +61,83 @@ export class ServerRuntimeClient<
level: Severity | SeverityLevel = 'info',
hint?: EventHint,
): PromiseLike<Event> {
return Promise.resolve(
return resolvedSyncPromise(
eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace),
);
}

/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
// sent to the Server only when the `requestHandler` middleware is used
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
const requestSession = scope.getRequestSession();

// Necessary checks to ensure this is code block is executed only within a request
// Should override the status only if `requestSession.status` is `Ok`, which is its initial stage
if (requestSession && requestSession.status === 'ok') {
requestSession.status = 'errored';
}
}

return super.captureException(exception, hint, scope);
}

/**
* @inheritDoc
*/
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
// Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
// when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
// sent to the Server only when the `requestHandler` middleware is used
if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
const eventType = event.type || 'exception';
const isException =
eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;

// If the event is of type Exception, then a request session should be captured
if (isException) {
const requestSession = scope.getRequestSession();

// Ensure that this is happening within the bounds of a request, and make sure not to override
// Session Status if Errored / Crashed
if (requestSession && requestSession.status === 'ok') {
requestSession.status = 'errored';
}
}
}

return super.captureEvent(event, hint, scope);
}

/**
*
* @inheritdoc
*/
public close(timeout?: number): PromiseLike<boolean> {
if (this._sessionFlusher) {
this._sessionFlusher.close();
}
return super.close(timeout);
}

/** Method that initialises an instance of SessionFlusher on Client */
public initSessionFlusher(): void {
const { release, environment } = this._options;
if (!release) {
__DEBUG_BUILD__ && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
} else {
this._sessionFlusher = new SessionFlusher(this, {
release,
environment,
});
}
}

/**
* Create a cron monitor check in and send it to Sentry.
*
Expand Down Expand Up @@ -121,6 +196,18 @@ export class ServerRuntimeClient<
return id;
}

/**
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
* appropriate session aggregates bucket
*/
protected _captureRequestSession(): void {
if (!this._sessionFlusher) {
__DEBUG_BUILD__ && logger.warn('Discarded request mode session because autoSessionTracking option was disabled');
} else {
this._sessionFlusher.incrementSessionStatusCount();
}
}

/**
* @inheritDoc
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@
"volta": {
"extends": "../../package.json"
},
"madge":{
"detectiveOptions": {
"ts": {
"skipTypeImports": true
}
}
},
"sideEffects": false
}
Loading