1 | import { startTransaction, getCurrentHub, withScope, captureException } from '@sentry/core';
|
2 | import { isString, extractTraceparentData, parseBaggageSetMutability, extractPathForTransaction, addRequestDataToTransaction, logger } from '@sentry/utils';
|
3 | import * as domain from 'domain';
|
4 | import { parseRequest } from './requestDataDeprecated.js';
|
5 | export { extractRequestData, parseRequest } from './requestDataDeprecated.js';
|
6 | import { extractRequestData, isAutoSessionTrackingEnabled, addRequestDataToEvent, flush } from './sdk.js';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function tracingHandler()
|
13 |
|
14 | {
|
15 | return function sentryTracingMiddleware(
|
16 | req,
|
17 | res,
|
18 | next,
|
19 | ) {
|
20 |
|
21 | var traceparentData =
|
22 | req.headers && isString(req.headers['sentry-trace']) && extractTraceparentData(req.headers['sentry-trace']);
|
23 | var rawBaggageString = req.headers && isString(req.headers.baggage) && req.headers.baggage;
|
24 |
|
25 | var baggage = parseBaggageSetMutability(rawBaggageString, traceparentData);
|
26 |
|
27 | var transaction = startTransaction(
|
28 | {
|
29 | name: extractPathForTransaction(req, { path: true, method: true }),
|
30 | op: 'http.server',
|
31 | ...traceparentData,
|
32 | metadata: { baggage },
|
33 | },
|
34 |
|
35 | { request: extractRequestData(req) },
|
36 | );
|
37 |
|
38 |
|
39 | getCurrentHub().configureScope(scope => {
|
40 | scope.setSpan(transaction);
|
41 | });
|
42 |
|
43 |
|
44 |
|
45 | (res ).__sentry_transaction = transaction;
|
46 |
|
47 | res.once('finish', () => {
|
48 |
|
49 |
|
50 | setImmediate(() => {
|
51 | addRequestDataToTransaction(transaction, req);
|
52 | transaction.setHttpStatus(res.statusCode);
|
53 | transaction.finish();
|
54 | });
|
55 | });
|
56 |
|
57 | next();
|
58 | };
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function requestHandler(
|
66 | options,
|
67 | ) {
|
68 | var currentHub = getCurrentHub();
|
69 | var client = currentHub.getClient();
|
70 |
|
71 |
|
72 | if (client && isAutoSessionTrackingEnabled(client)) {
|
73 | client.initSessionFlusher();
|
74 |
|
75 |
|
76 | var scope = currentHub.getScope();
|
77 | if (scope && scope.getSession()) {
|
78 | scope.setSession();
|
79 | }
|
80 | }
|
81 |
|
82 | return function sentryRequestMiddleware(
|
83 | req,
|
84 | res,
|
85 | next,
|
86 | ) {
|
87 |
|
88 | let backwardsCompatibleEventProcessor;
|
89 | if (options && 'include' in options) {
|
90 | backwardsCompatibleEventProcessor = (event) => addRequestDataToEvent(event, req, options);
|
91 | } else {
|
92 | backwardsCompatibleEventProcessor = (event) => parseRequest(event, req, options );
|
93 | }
|
94 |
|
95 | if (options && options.flushTimeout && options.flushTimeout > 0) {
|
96 | var _end = res.end;
|
97 | res.end = function (chunk, encoding, cb) {
|
98 | void flush(options.flushTimeout)
|
99 | .then(() => {
|
100 | _end.call(this, chunk, encoding, cb);
|
101 | })
|
102 | .then(null, e => {
|
103 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(e);
|
104 | _end.call(this, chunk, encoding, cb);
|
105 | });
|
106 | };
|
107 | }
|
108 | var local = domain.create();
|
109 | local.add(req);
|
110 | local.add(res);
|
111 | local.on('error', next);
|
112 |
|
113 | local.run(() => {
|
114 | var currentHub = getCurrentHub();
|
115 |
|
116 | currentHub.configureScope(scope => {
|
117 | scope.addEventProcessor(backwardsCompatibleEventProcessor);
|
118 | var client = currentHub.getClient();
|
119 | if (isAutoSessionTrackingEnabled(client)) {
|
120 | var scope = currentHub.getScope();
|
121 | if (scope) {
|
122 |
|
123 | scope.setRequestSession({ status: 'ok' });
|
124 | }
|
125 | }
|
126 | });
|
127 |
|
128 | res.once('finish', () => {
|
129 | var client = currentHub.getClient();
|
130 | if (isAutoSessionTrackingEnabled(client)) {
|
131 | setImmediate(() => {
|
132 | if (client && (client )._captureRequestSession) {
|
133 |
|
134 |
|
135 | (client )._captureRequestSession();
|
136 | }
|
137 | });
|
138 | }
|
139 | });
|
140 | next();
|
141 | });
|
142 | };
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function getStatusCodeFromResponse(error) {
|
149 | var statusCode = error.status || error.statusCode || error.status_code || (error.output && error.output.statusCode);
|
150 | return statusCode ? parseInt(statusCode , 10) : 500;
|
151 | }
|
152 |
|
153 |
|
154 | function defaultShouldHandleError(error) {
|
155 | var status = getStatusCodeFromResponse(error);
|
156 | return status >= 500;
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function errorHandler(options
|
164 |
|
165 | )
|
166 |
|
167 | {
|
168 | return function sentryErrorMiddleware(
|
169 | error,
|
170 | _req,
|
171 | res,
|
172 | next,
|
173 | ) {
|
174 | var shouldHandleError = (options && options.shouldHandleError) || defaultShouldHandleError;
|
175 |
|
176 | if (shouldHandleError(error)) {
|
177 | withScope(_scope => {
|
178 |
|
179 | var transaction = (res ).__sentry_transaction ;
|
180 | if (transaction && _scope.getSpan() === undefined) {
|
181 | _scope.setSpan(transaction);
|
182 | }
|
183 |
|
184 | var client = getCurrentHub().getClient();
|
185 | if (client && isAutoSessionTrackingEnabled(client)) {
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | var isSessionAggregatesMode = (client )._sessionFlusher !== undefined;
|
191 | if (isSessionAggregatesMode) {
|
192 | var requestSession = _scope.getRequestSession();
|
193 |
|
194 |
|
195 |
|
196 | if (requestSession && requestSession.status !== undefined) {
|
197 | requestSession.status = 'crashed';
|
198 | }
|
199 | }
|
200 | }
|
201 |
|
202 | var eventId = captureException(error);
|
203 | (res ).sentry = eventId;
|
204 | next(error);
|
205 | });
|
206 |
|
207 | return;
|
208 | }
|
209 |
|
210 | next(error);
|
211 | };
|
212 | }
|
213 |
|
214 |
|
215 | ;
|
216 |
|
217 | export { errorHandler, requestHandler, tracingHandler };
|
218 |
|