@sentry/node
Version:
Official Sentry SDK for Node.js
174 lines (152 loc) • 5.89 kB
JavaScript
var {
_optionalChain
} = require('@sentry/utils/cjs/buildPolyfills');
Object.defineProperty(exports, '__esModule', { value: true });
const core = require('@sentry/core');
const utils = require('@sentry/utils');
const os = require('os');
const util = require('util');
const eventbuilder = require('./eventbuilder.js');
/**
* The Sentry Node SDK Client.
*
* @see NodeClientOptions for documentation on configuration options.
* @see SentryClient for usage documentation.
*/
class NodeClient extends core.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: core.SDK_VERSION,
},
],
version: core.SDK_VERSION,
};
// Until node supports global TextEncoder in all versions we support, we are forced to pass it from util
options.transportOptions = {
textEncoder: new util.TextEncoder(),
...options.transportOptions,
};
// The Node client always supports tracing
core.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__) && utils.logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
} else {
this._sessionFlusher = new core.SessionFlusher(this, {
release,
environment,
});
}
}
/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
eventFromException(exception, hint) {
return utils.resolvedSyncPromise(eventbuilder.eventFromUnknownInput(this._options.stackParser, exception, hint));
}
/**
* @inheritDoc
*/
eventFromMessage(
message,
// eslint-disable-next-line deprecation/deprecation
level = 'info',
hint,
) {
return utils.resolvedSyncPromise(
eventbuilder.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__) && utils.logger.warn('Discarded request mode session because autoSessionTracking option was disabled');
} else {
this._sessionFlusher.incrementSessionStatusCount();
}
}
}
exports.NodeClient = NodeClient;
//# sourceMappingURL=client.js.map