|
| 1 | +import { addBreadcrumb, getCurrentClient } from '@sentry/shim'; |
| 2 | +import { Integration } from '@sentry/types'; |
| 3 | +import { fill } from '@sentry/utils'; |
| 4 | +import { ClientRequest, ClientRequestArgs, ServerResponse } from 'http'; |
| 5 | +import { inherits } from 'util'; |
| 6 | + |
| 7 | +let lastResponse: ServerResponse | undefined; |
| 8 | + |
| 9 | +/** |
| 10 | + * Request interface which can carry around unified url |
| 11 | + * independently of used framework |
| 12 | + */ |
| 13 | +interface SentryRequest extends Request { |
| 14 | + __ravenBreadcrumbUrl?: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Function that can combine together a url that'll be used for our breadcrumbs. |
| 19 | + * |
| 20 | + * @param options url that should be returned or an object containing it's parts. |
| 21 | + * @returns constructed url |
| 22 | + */ |
| 23 | +function createBreadcrumbUrl(options: string | ClientRequestArgs): string { |
| 24 | + // We could just always reconstruct this from this.agent, this._headers, this.path, etc |
| 25 | + // but certain other http-instrumenting libraries (like nock, which we use for tests) fail to |
| 26 | + // maintain the guarantee that after calling origClientRequest, those fields will be populated |
| 27 | + if (typeof options === 'string') { |
| 28 | + return options; |
| 29 | + } else { |
| 30 | + const protocol = options.protocol || ''; |
| 31 | + const hostname = options.hostname || options.host || ''; |
| 32 | + // Don't log standard :80 (http) and :443 (https) ports to reduce the noise |
| 33 | + const port = |
| 34 | + !options.port || options.port === 80 || options.port === 443 |
| 35 | + ? '' |
| 36 | + : `:${options.port}`; |
| 37 | + const path = options.path || '/'; |
| 38 | + return `${protocol}//${hostname}${port}${path}`; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Wrapper function for internal _load calls within `require` |
| 44 | + */ |
| 45 | +function loadWrapper(nativeModule: any): any { |
| 46 | + // We need to use some functional-style currying to pass values around |
| 47 | + // as we cannot rely on `bind`, because this has to preserve correct |
| 48 | + // context for native calls |
| 49 | + return function(originalLoad: () => any): any { |
| 50 | + return function(this: SentryRequest, moduleId: string): any { |
| 51 | + const originalModule = originalLoad.apply(nativeModule, arguments); |
| 52 | + |
| 53 | + if (moduleId !== 'http') { |
| 54 | + return originalModule; |
| 55 | + } |
| 56 | + |
| 57 | + const origClientRequest = originalModule.ClientRequest; |
| 58 | + const clientRequest = function( |
| 59 | + this: SentryRequest, |
| 60 | + options: ClientRequestArgs | string, |
| 61 | + callback: () => void, |
| 62 | + ): any { |
| 63 | + // Note: this won't capture a breadcrumb if a response never comes |
| 64 | + // It would be useful to know if that was the case, though, so |
| 65 | + // todo: revisit to see if we can capture sth indicating response never came |
| 66 | + // possibility: capture one breadcrumb for "req sent" and one for "res recvd" |
| 67 | + // seems excessive but solves the problem and *is* strictly more information |
| 68 | + // could be useful for weird response sequencing bug scenarios |
| 69 | + |
| 70 | + origClientRequest.call(this, options, callback); |
| 71 | + this.__ravenBreadcrumbUrl = createBreadcrumbUrl(options); |
| 72 | + }; |
| 73 | + |
| 74 | + inherits(clientRequest, origClientRequest); |
| 75 | + |
| 76 | + fill(clientRequest.prototype, 'emit', emitWrapper); |
| 77 | + |
| 78 | + fill(originalModule, 'ClientRequest', function(): any { |
| 79 | + return clientRequest; |
| 80 | + }); |
| 81 | + |
| 82 | + // http.request orig refs module-internal ClientRequest, not exported one, so |
| 83 | + // it still points at orig ClientRequest after our monkeypatch; these reimpls |
| 84 | + // just get that reference updated to use our new ClientRequest |
| 85 | + fill(originalModule, 'request', function(): any { |
| 86 | + return function(options: ClientRequestArgs, callback: () => void): any { |
| 87 | + return new originalModule.ClientRequest( |
| 88 | + options, |
| 89 | + callback, |
| 90 | + ) as ClientRequest; |
| 91 | + }; |
| 92 | + }); |
| 93 | + |
| 94 | + fill(originalModule, 'get', function(): any { |
| 95 | + return function(options: ClientRequestArgs, callback: () => void): any { |
| 96 | + const req = originalModule.request(options, callback); |
| 97 | + req.end(); |
| 98 | + return req; |
| 99 | + }; |
| 100 | + }); |
| 101 | + |
| 102 | + return originalModule; |
| 103 | + }; |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Wrapper function for request's `emit` calls |
| 109 | + */ |
| 110 | +function emitWrapper( |
| 111 | + origEmit: EventListener, |
| 112 | +): (event: string, response: ServerResponse) => EventListener { |
| 113 | + return function( |
| 114 | + this: SentryRequest, |
| 115 | + event: string, |
| 116 | + response: ServerResponse, |
| 117 | + ): any { |
| 118 | + // I'm not sure why but Node.js (at least in v8.X) |
| 119 | + // is emitting all events twice :| |
| 120 | + if (lastResponse === undefined || lastResponse !== response) { |
| 121 | + lastResponse = response; |
| 122 | + } else { |
| 123 | + return origEmit.apply(this, arguments); |
| 124 | + } |
| 125 | + |
| 126 | + const DSN = getCurrentClient().getDSN(); |
| 127 | + |
| 128 | + const isInterestingEvent = event === 'response' || event === 'error'; |
| 129 | + const isNotSentryRequest = |
| 130 | + DSN && |
| 131 | + this.__ravenBreadcrumbUrl && |
| 132 | + !this.__ravenBreadcrumbUrl.includes(DSN.host); |
| 133 | + |
| 134 | + if (isInterestingEvent && isNotSentryRequest) { |
| 135 | + addBreadcrumb({ |
| 136 | + category: 'http', |
| 137 | + data: { |
| 138 | + method: this.method, |
| 139 | + status_code: response.statusCode, |
| 140 | + |
| 141 | + url: this.__ravenBreadcrumbUrl, |
| 142 | + }, |
| 143 | + type: 'http', |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + return origEmit.apply(this, arguments); |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +/** http module integration */ |
| 152 | +export class Http implements Integration { |
| 153 | + /** |
| 154 | + * @inheritDoc |
| 155 | + */ |
| 156 | + public name: string = 'Console'; |
| 157 | + /** |
| 158 | + * @inheritDoc |
| 159 | + */ |
| 160 | + public install(): void { |
| 161 | + const nativeModule = require('module'); |
| 162 | + fill(nativeModule, '_load', loadWrapper(nativeModule)); |
| 163 | + // observation: when the https module does its own require('http'), it *does not* hit our hooked require to instrument http on the fly |
| 164 | + // but if we've previously instrumented http, https *does* get our already-instrumented version |
| 165 | + // this is because raven's transports are required before this instrumentation takes place, which loads https (and http) |
| 166 | + // so module cache will have uninstrumented http; proactively loading it here ensures instrumented version is in module cache |
| 167 | + // alternatively we could refactor to load our transports later, but this is easier and doesn't have much drawback |
| 168 | + require('http'); |
| 169 | + } |
| 170 | +} |
0 commit comments