|
4 | 4 | defineIntegration, |
5 | 5 | fill, |
6 | 6 | getBreadcrumbLogLevelFromHttpStatusCode, |
7 | | - getClient, |
8 | 7 | getTraceData, |
9 | 8 | LRUMap, |
10 | 9 | SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
@@ -89,6 +88,7 @@ type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod; |
89 | 88 |
|
90 | 89 | function createWrappedRequestFactory( |
91 | 90 | options: NetOptions, |
| 91 | + enableLogs: boolean, |
92 | 92 | tracePropagationTargets: TracePropagationTargets | undefined, |
93 | 93 | ): WrappedRequestMethodFactory { |
94 | 94 | // We're caching results so we don't have to recompute regexp every time we create a request. |
@@ -136,6 +136,57 @@ function createWrappedRequestFactory( |
136 | 136 | return true; |
137 | 137 | }; |
138 | 138 |
|
| 139 | + /** |
| 140 | + * Captures Breadcrumb based on provided request/response pair |
| 141 | + */ |
| 142 | + const addRequestBreadcrumb = ( |
| 143 | + event: string, |
| 144 | + method: string, |
| 145 | + url: string, |
| 146 | + req: ClientRequest, |
| 147 | + res?: IncomingMessage, |
| 148 | + ): void => { |
| 149 | + const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode); |
| 150 | + |
| 151 | + addBreadcrumb( |
| 152 | + { |
| 153 | + type: 'http', |
| 154 | + category: 'electron.net', |
| 155 | + data: { |
| 156 | + url, |
| 157 | + method: method, |
| 158 | + status_code: res?.statusCode, |
| 159 | + }, |
| 160 | + level, |
| 161 | + }, |
| 162 | + { |
| 163 | + event, |
| 164 | + request: req, |
| 165 | + response: res, |
| 166 | + }, |
| 167 | + ); |
| 168 | + |
| 169 | + if (!enableLogs) { |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + const attributes = { |
| 174 | + 'sentry.origin': 'auto.electron.net', |
| 175 | + statusCode: res?.statusCode, |
| 176 | + }; |
| 177 | + |
| 178 | + switch (level) { |
| 179 | + case 'error': |
| 180 | + logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes); |
| 181 | + break; |
| 182 | + case 'warning': |
| 183 | + logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes); |
| 184 | + break; |
| 185 | + default: |
| 186 | + logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes); |
| 187 | + } |
| 188 | + }; |
| 189 | + |
139 | 190 | return function wrappedRequestMethodFactory(originalRequestMethod: RequestMethod): RequestMethod { |
140 | 191 | return function requestMethod(this: typeof electronNet, reqOptions: RequestOptions): ClientRequest { |
141 | 192 | const { url, method } = parseOptions(reqOptions); |
@@ -191,64 +242,26 @@ function createWrappedRequestFactory( |
191 | 242 | }; |
192 | 243 | } |
193 | 244 |
|
194 | | -/** |
195 | | - * Captures Breadcrumb based on provided request/response pair |
196 | | - */ |
197 | | -function addRequestBreadcrumb( |
198 | | - event: string, |
199 | | - method: string, |
200 | | - url: string, |
201 | | - req: ClientRequest, |
202 | | - res?: IncomingMessage, |
203 | | -): void { |
204 | | - const level = getBreadcrumbLogLevelFromHttpStatusCode(res?.statusCode); |
205 | | - addBreadcrumb( |
206 | | - { |
207 | | - type: 'http', |
208 | | - category: 'electron.net', |
209 | | - data: { |
210 | | - url, |
211 | | - method: method, |
212 | | - status_code: res?.statusCode, |
213 | | - }, |
214 | | - level, |
215 | | - }, |
216 | | - { |
217 | | - event, |
218 | | - request: req, |
219 | | - response: res, |
220 | | - }, |
221 | | - ); |
222 | | - |
223 | | - const attributes = { statusCode: res?.statusCode }; |
224 | | - |
225 | | - switch (level) { |
226 | | - case 'error': |
227 | | - logger.error(logger.fmt`Electron.net request failed: ${method} ${url}`, attributes); |
228 | | - break; |
229 | | - case 'warning': |
230 | | - logger.warn(logger.fmt`Electron.net request warning: ${method} ${url}`, attributes); |
231 | | - break; |
232 | | - default: |
233 | | - logger.info(logger.fmt`Electron.net request succeeded: ${method} ${url}`, attributes); |
234 | | - } |
235 | | -} |
236 | | - |
237 | 245 | /** |
238 | 246 | * Electron 'net' module integration |
239 | 247 | */ |
240 | 248 | export const electronNetIntegration = defineIntegration((options: NetOptions = {}) => { |
241 | 249 | return { |
242 | 250 | name: 'ElectronNet', |
243 | | - setup() { |
244 | | - const clientOptions = getClient()?.getOptions(); |
| 251 | + setup(client) { |
| 252 | + const clientOptions = client.getOptions(); |
| 253 | + const enableLogs = !!clientOptions?.enableLogs; |
245 | 254 |
|
246 | 255 | // No need to instrument if we don't want to track anything |
247 | 256 | if (options.breadcrumbs === false && options.tracing === false) { |
248 | 257 | return; |
249 | 258 | } |
250 | 259 |
|
251 | | - fill(electronNet, 'request', createWrappedRequestFactory(options, clientOptions?.tracePropagationTargets)); |
| 260 | + fill( |
| 261 | + electronNet, |
| 262 | + 'request', |
| 263 | + createWrappedRequestFactory(options, enableLogs, clientOptions?.tracePropagationTargets), |
| 264 | + ); |
252 | 265 | }, |
253 | 266 | }; |
254 | 267 | }); |
0 commit comments