UNPKG

5.45 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { BaseClient, SDK_VERSION } from '@sentry/core';
3import { SessionFlusher } from '@sentry/hub';
4import { RequestSessionStatus } from '@sentry/types';
5import { logger } from '@sentry/utils';
6import { NodeBackend } from './backend';
7/**
8 * The Sentry Node SDK Client.
9 *
10 * @see NodeOptions for documentation on configuration options.
11 * @see SentryClient for usage documentation.
12 */
13var NodeClient = /** @class */ (function (_super) {
14 __extends(NodeClient, _super);
15 /**
16 * Creates a new Node SDK instance.
17 * @param options Configuration options for this SDK.
18 */
19 function NodeClient(options) {
20 var _this = this;
21 options._metadata = options._metadata || {};
22 options._metadata.sdk = options._metadata.sdk || {
23 name: 'sentry.javascript.node',
24 packages: [
25 {
26 name: 'npm:@sentry/node',
27 version: SDK_VERSION,
28 },
29 ],
30 version: SDK_VERSION,
31 };
32 _this = _super.call(this, NodeBackend, options) || this;
33 return _this;
34 }
35 /**
36 * @inheritDoc
37 */
38 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
39 NodeClient.prototype.captureException = function (exception, hint, scope) {
40 // Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only
41 // when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload
42 // sent to the Server only when the `requestHandler` middleware is used
43 if (this._options.autoSessionTracking && this._sessionFlusher && scope) {
44 var requestSession = scope.getRequestSession();
45 // Necessary checks to ensure this is code block is executed only within a request
46 // Should override the status only if `requestSession.status` is `Ok`, which is its initial stage
47 if (requestSession && requestSession.status === RequestSessionStatus.Ok) {
48 requestSession.status = RequestSessionStatus.Errored;
49 }
50 }
51 return _super.prototype.captureException.call(this, exception, hint, scope);
52 };
53 /**
54 * @inheritDoc
55 */
56 NodeClient.prototype.captureEvent = function (event, hint, scope) {
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 var eventType = event.type || 'exception';
62 var isException = eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;
63 // If the event is of type Exception, then a request session should be captured
64 if (isException) {
65 var requestSession = scope.getRequestSession();
66 // Ensure that this is happening within the bounds of a request, and make sure not to override
67 // Session Status if Errored / Crashed
68 if (requestSession && requestSession.status === RequestSessionStatus.Ok) {
69 requestSession.status = RequestSessionStatus.Errored;
70 }
71 }
72 }
73 return _super.prototype.captureEvent.call(this, event, hint, scope);
74 };
75 /**
76 *
77 * @inheritdoc
78 */
79 NodeClient.prototype.close = function (timeout) {
80 var _a;
81 (_a = this._sessionFlusher) === null || _a === void 0 ? void 0 : _a.close();
82 return _super.prototype.close.call(this, timeout);
83 };
84 /** Method that initialises an instance of SessionFlusher on Client */
85 NodeClient.prototype.initSessionFlusher = function () {
86 var _a = this._options, release = _a.release, environment = _a.environment;
87 if (!release) {
88 logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
89 }
90 else {
91 this._sessionFlusher = new SessionFlusher(this.getTransport(), {
92 release: release,
93 environment: environment,
94 });
95 }
96 };
97 /**
98 * @inheritDoc
99 */
100 NodeClient.prototype._prepareEvent = function (event, scope, hint) {
101 event.platform = event.platform || 'node';
102 if (this.getOptions().serverName) {
103 event.server_name = this.getOptions().serverName;
104 }
105 return _super.prototype._prepareEvent.call(this, event, scope, hint);
106 };
107 /**
108 * Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
109 * appropriate session aggregates bucket
110 */
111 NodeClient.prototype._captureRequestSession = function () {
112 if (!this._sessionFlusher) {
113 logger.warn('Discarded request mode session because autoSessionTracking option was disabled');
114 }
115 else {
116 this._sessionFlusher.incrementSessionStatusCount();
117 }
118 };
119 return NodeClient;
120}(BaseClient));
121export { NodeClient };
122//# sourceMappingURL=client.js.map
\No newline at end of file