Skip to content

Commit 74eacc0

Browse files
committed
fix: Make rewritten http integration work in pre v9 node versions
1 parent d5b3df6 commit 74eacc0

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

packages/node/src/integrations/http.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { getCurrentHub, Span } from '@sentry/core';
22
import { Integration } from '@sentry/types';
3-
import { fill } from '@sentry/utils';
3+
import { fill, parseSemver } from '@sentry/utils';
44
import * as http from 'http';
55
import * as https from 'https';
66

7+
const NODE_VERSION = parseSemver(process.versions.node);
8+
79
/** http module integration */
810
export class Http implements Integration {
911
/**
@@ -48,9 +50,14 @@ export class Http implements Integration {
4850
fill(httpModule, 'get', handlerWrapper);
4951
fill(httpModule, 'request', handlerWrapper);
5052

51-
const httpsModule = require('https');
52-
fill(httpsModule, 'get', handlerWrapper);
53-
fill(httpsModule, 'request', handlerWrapper);
53+
// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
54+
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
55+
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
56+
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
57+
const httpsModule = require('https');
58+
fill(httpsModule, 'get', handlerWrapper);
59+
fill(httpsModule, 'request', handlerWrapper);
60+
}
5461
}
5562
}
5663

@@ -74,7 +81,7 @@ function createHandlerWrapper(
7481
let span: Span;
7582
if (tracingEnabled) {
7683
span = getCurrentHub().startSpan({
77-
description: `${typeof options === 'string' ? 'GET' : options.method}|${requestUrl}`,
84+
description: `${typeof options === 'string' || !options.method ? 'GET' : options.method}|${requestUrl}`,
7885
op: 'request',
7986
});
8087
}

packages/utils/src/misc.ts

+32
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,35 @@ function _htmlElementAsString(elem: HTMLElement): string {
342342
export function timestampWithMs(): number {
343343
return new Date().getTime() / 1000;
344344
}
345+
346+
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
347+
const SEMVER_REGEXP = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
348+
349+
/**
350+
* Represents Semantic Versioning object
351+
*/
352+
interface SemVer {
353+
major?: number;
354+
minor?: number;
355+
patch?: number;
356+
prerelease?: string;
357+
buildmetadata?: string;
358+
}
359+
360+
/**
361+
* Parses input into a SemVer interface
362+
* @param input string representation of a semver version
363+
*/
364+
export function parseSemver(input: string): SemVer {
365+
const match = input.match(SEMVER_REGEXP) || [];
366+
const major = parseInt(match[1], 10);
367+
const minor = parseInt(match[2], 10);
368+
const patch = parseInt(match[3], 10);
369+
return {
370+
buildmetadata: match[5],
371+
major: isNaN(major) ? undefined : major,
372+
minor: isNaN(minor) ? undefined : minor,
373+
patch: isNaN(patch) ? undefined : patch,
374+
prerelease: match[4],
375+
};
376+
}

0 commit comments

Comments
 (0)