UNPKG

@sentry/node

Version:
169 lines (149 loc) 5.84 kB
import { _optionalChain } from '@sentry/utils/esm/buildPolyfills'; import { BaseClient, SDK_VERSION, addTracingExtensions, SessionFlusher } from '@sentry/core'; import { logger, resolvedSyncPromise } from '@sentry/utils'; import * as os from 'os'; import { TextEncoder } from 'util'; import { eventFromUnknownInput, eventFromMessage } from './eventbuilder.js'; /** * The Sentry Node SDK Client. * * @see NodeClientOptions for documentation on configuration options. * @see SentryClient for usage documentation. */ class NodeClient extends BaseClient { /** * Creates a new Node SDK instance. * @param options Configuration options for this SDK. */ constructor(options) { options._metadata = options._metadata || {}; options._metadata.sdk = options._metadata.sdk || { name: 'sentry.javascript.node', packages: [ { name: 'npm:@sentry/node', version: SDK_VERSION, }, ], version: SDK_VERSION, }; // Until node supports global TextEncoder in all versions we support, we are forced to pass it from util options.transportOptions = { textEncoder: new TextEncoder(), ...options.transportOptions, }; // The Node client always supports tracing addTracingExtensions(); super(options); } /** * @inheritDoc */ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types captureException(exception, hint, scope) { // 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 */ captureEvent(event, hint, scope) { // 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 */ close(timeout) { _optionalChain([this, 'access', _ => _._sessionFlusher, 'optionalAccess', _2 => _2.close, 'call', _3 => _3()]); return super.close(timeout); } /** Method that initialises an instance of SessionFlusher on Client */ initSessionFlusher() { const { release, environment } = this._options; if (!release) { (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!'); } else { this._sessionFlusher = new SessionFlusher(this, { release, environment, }); } } /** * @inheritDoc */ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types eventFromException(exception, hint) { return resolvedSyncPromise(eventFromUnknownInput(this._options.stackParser, exception, hint)); } /** * @inheritDoc */ eventFromMessage( message, // eslint-disable-next-line deprecation/deprecation level = 'info', hint, ) { return resolvedSyncPromise( eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace), ); } /** * @inheritDoc */ _prepareEvent(event, hint, scope) { event.platform = event.platform || 'node'; event.contexts = { ...event.contexts, runtime: _optionalChain([event, 'access', _4 => _4.contexts, 'optionalAccess', _5 => _5.runtime]) || { name: 'node', version: global.process.version, }, }; event.server_name = event.server_name || this.getOptions().serverName || global.process.env.SENTRY_NAME || os.hostname(); return super._prepareEvent(event, hint, scope); } /** * Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment * appropriate session aggregates bucket */ _captureRequestSession() { if (!this._sessionFlusher) { (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Discarded request mode session because autoSessionTracking option was disabled'); } else { this._sessionFlusher.incrementSessionStatusCount(); } } } export { NodeClient }; //# sourceMappingURL=client.js.map