Skip to content

Commit 6d34c9f

Browse files
committed
respond to code reviews
1 parent 18366e7 commit 6d34c9f

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

packages/node/src/integrations/http.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
normalizeRequestArgs,
1212
RequestMethod,
1313
RequestMethodArgs,
14-
RequestOptions,
1514
} from './utils/http';
1615

1716
const NODE_VERSION = parseSemver(process.versions.node);
@@ -92,13 +91,16 @@ function _createWrappedRequestMethodFactory(
9291
): WrappedRequestMethodFactory {
9392
return function wrappedRequestMethodFactory(originalRequestMethod: OriginalRequestMethod): WrappedRequestMethod {
9493
return function wrappedMethod(this: typeof http | typeof https, ...args: RequestMethodArgs): http.ClientRequest {
94+
// eslint-disable-next-line @typescript-eslint/no-this-alias
95+
const httpModule = this;
96+
9597
const requestArgs = normalizeRequestArgs(args);
9698
const requestOptions = requestArgs[0];
9799
const requestUrl = extractUrl(requestOptions);
98100

99101
// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method
100102
if (isSentryRequest(requestUrl)) {
101-
return originalRequestMethod.apply(this, requestArgs);
103+
return originalRequestMethod.apply(httpModule, requestArgs);
102104
}
103105

104106
let span: Span | undefined;
@@ -112,30 +114,40 @@ function _createWrappedRequestMethodFactory(
112114
description: `${requestOptions.method || 'GET'} ${requestUrl}`,
113115
op: 'request',
114116
});
115-
addSentryTraceHeader(span, requestOptions);
117+
118+
const sentryTraceHeader = span.toTraceparent();
119+
logger.log(`[Tracing] Adding sentry-trace header to outgoing request: ${sentryTraceHeader}`);
120+
requestOptions.headers = { ...requestOptions.headers, 'sentry-trace': sentryTraceHeader };
116121
}
117122
}
118123

119124
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
120125
return originalRequestMethod
121-
.apply(this, requestArgs)
122-
.once('response', function(this: http.IncomingMessage, res: http.ServerResponse): void {
126+
.apply(httpModule, requestArgs)
127+
.once('response', function(this: http.ClientRequest, res: http.IncomingMessage): void {
128+
// eslint-disable-next-line @typescript-eslint/no-this-alias
129+
const req = this;
123130
if (breadcrumbsEnabled) {
124-
addRequestBreadcrumb('response', requestUrl, this, res);
131+
addRequestBreadcrumb('response', requestUrl, req, res);
125132
}
126133
if (tracingEnabled && span) {
127-
span.setHttpStatus(res.statusCode);
128-
cleanSpanDescription(requestOptions, this, span);
134+
if (res.statusCode) {
135+
span.setHttpStatus(res.statusCode);
136+
}
137+
span.description = cleanSpanDescription(span.description, requestOptions, req);
129138
span.finish();
130139
}
131140
})
132-
.once('error', function(this: http.IncomingMessage): void {
141+
.once('error', function(this: http.ClientRequest): void {
142+
// eslint-disable-next-line @typescript-eslint/no-this-alias
143+
const req = this;
144+
133145
if (breadcrumbsEnabled) {
134-
addRequestBreadcrumb('error', requestUrl, this);
146+
addRequestBreadcrumb('error', requestUrl, req);
135147
}
136148
if (tracingEnabled && span) {
137149
span.setHttpStatus(500);
138-
cleanSpanDescription(requestOptions, this, span);
150+
span.description = cleanSpanDescription(span.description, requestOptions, req);
139151
span.finish();
140152
}
141153
});
@@ -146,7 +158,7 @@ function _createWrappedRequestMethodFactory(
146158
/**
147159
* Captures Breadcrumb based on provided request/response pair
148160
*/
149-
function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMessage, res?: http.ServerResponse): void {
161+
function addRequestBreadcrumb(event: string, url: string, req: http.ClientRequest, res?: http.IncomingMessage): void {
150162
if (!getCurrentHub().getIntegration(Http)) {
151163
return;
152164
}
@@ -168,16 +180,3 @@ function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMess
168180
},
169181
);
170182
}
171-
172-
/**
173-
* Add the `sentry-trace` header to the request options passed to `http(s).request()` and `http(s).get()`.
174-
*
175-
* @param span The span representing this outgoing request.
176-
* @param requestArgs The arguments passed to `http(s).request()` or `http(s).get()`
177-
*
178-
*/
179-
function addSentryTraceHeader(span: Span, requestOptions: RequestOptions): void {
180-
const headerValue = span.toTraceparent();
181-
logger.log(`[Tracing] Adding sentry-trace header to outgoing request: ${headerValue}`);
182-
requestOptions.headers = { ...requestOptions.headers, 'sentry-trace': headerValue };
183-
}

packages/node/src/integrations/utils/http.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getCurrentHub } from '@sentry/core';
2-
import { Span } from '@sentry/types';
32
import * as http from 'http';
43
import { URL } from 'url';
54

@@ -39,26 +38,41 @@ export function extractUrl(requestOptions: RequestOptions): string {
3938
}
4039

4140
/**
42-
* Handle various edge cases in the span description. Runs just before the span closes because in one case it relies on
43-
* data from the response object.
41+
* Handle various edge cases in the span description (for spans representing http(s) requests).
4442
*
43+
* @param description current `description` property of the span representing the request
4544
* @param requestOptions Configuration data for the request
46-
* @param response Response object
47-
* @param span Span representing the request
45+
* @param Request Request object
46+
*
47+
* @returns The cleaned description
4848
*/
49-
export function cleanSpanDescription(requestOptions: RequestOptions, response: http.IncomingMessage, span: Span): void {
49+
export function cleanSpanDescription(
50+
description: string | undefined,
51+
requestOptions: RequestOptions,
52+
request: http.ClientRequest,
53+
): string | undefined {
54+
// nothing to clean
55+
if (!description) {
56+
return description;
57+
}
58+
59+
// eslint-disable-next-line prefer-const
60+
let [method, requestUrl] = description.split(' ');
61+
5062
// superagent sticks the protocol in a weird place (we check for host because if both host *and* protocol are missing,
5163
// we're likely dealing with an internal route and this doesn't apply)
52-
if (requestOptions.host && !('protocol' in requestOptions)) {
64+
if (requestOptions.host && !requestOptions.protocol) {
5365
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
54-
requestOptions.protocol = (response as any).agent?.protocol; // worst comes to worst, this is undefined and nothing changes
55-
span.description = `${requestOptions.method || 'GET'} ${extractUrl(requestOptions)}`;
66+
requestOptions.protocol = (request as any)?.agent?.protocol; // worst comes to worst, this is undefined and nothing changes
67+
requestUrl = extractUrl(requestOptions);
5668
}
5769

5870
// internal routes can end up starting with a triple slash rather than a single one
59-
if (span.description?.startsWith('///')) {
60-
span.description = span.description.slice(2);
71+
if (requestUrl?.startsWith('///')) {
72+
requestUrl = requestUrl.slice(2);
6173
}
74+
75+
return `${method} ${requestUrl}`;
6276
}
6377

6478
// the node types are missing a few properties which node's `urlToOptions` function spits out

scripts/test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -le 6 ]]; then
99
# current versions of nock don't support node 6
1010
cd packages/node
1111
yarn add --dev --ignore-engines [email protected]
12-
yarn list --pattern nock
1312
cd ../..
1413
# ember requires Node >= 10 to build
1514
yarn build --ignore="@sentry/ember" --ignore="@sentry/serverless" --ignore="@sentry/gatsby" --ignore="@sentry/react"

0 commit comments

Comments
 (0)