-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Deprecate parseUrl
#14404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* eslint-disable max-lines */ | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getActiveSpan } from '@sentry/core'; | ||
import { setMeasurement } from '@sentry/core'; | ||
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger, parseUrl } from '@sentry/core'; | ||
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger } from '@sentry/core'; | ||
import type { Measurements, Span, SpanAttributes, StartSpanOptions } from '@sentry/types'; | ||
|
||
import { spanToJSON } from '@sentry/core'; | ||
|
@@ -545,8 +545,6 @@ export function _addResourceSpans( | |
return; | ||
} | ||
|
||
const parsedUrl = parseUrl(resourceUrl); | ||
|
||
const attributes: SpanAttributes = { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics', | ||
}; | ||
|
@@ -561,12 +559,18 @@ export function _addResourceSpans( | |
if ('renderBlockingStatus' in entry) { | ||
attributes['resource.render_blocking_status'] = entry.renderBlockingStatus; | ||
} | ||
if (parsedUrl.protocol) { | ||
attributes['url.scheme'] = parsedUrl.protocol.split(':').pop(); // the protocol returned by parseUrl includes a :, but OTEL spec does not, so we remove it. | ||
} | ||
|
||
if (parsedUrl.host) { | ||
attributes['server.address'] = parsedUrl.host; | ||
try { | ||
// The URL constructor can throw when there is no protocol or host. | ||
const parsedUrl = new URL(resourceUrl); | ||
if (parsedUrl.protocol) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can protocol be empty? |
||
attributes['url.scheme'] = parsedUrl.protocol.split(':').pop(); // the protocol returned by parseUrl includes a :, but OTEL spec does not, so we remove it. | ||
} | ||
if (parsedUrl.host) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can host even be empty? 🤔 |
||
attributes['server.address'] = parsedUrl.host; | ||
} | ||
} catch { | ||
// noop | ||
} | ||
|
||
attributes['url.same_origin'] = resourceUrl.includes(WINDOW.location.origin); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
getEventDescription, | ||
htmlTreeAsString, | ||
logger, | ||
parseUrl, | ||
safeJoin, | ||
severityLevelFromString, | ||
} from '@sentry/core'; | ||
|
@@ -328,6 +327,9 @@ function _getFetchBreadcrumbHandler(client: Client): (handlerData: HandlerDataFe | |
}; | ||
} | ||
|
||
// Just a dummy url base for the `URL` constructor. | ||
const DUMMY_URL_BASE = 'a://'; | ||
|
||
/** | ||
* Creates breadcrumbs from history API calls | ||
*/ | ||
|
@@ -337,24 +339,25 @@ function _getHistoryBreadcrumbHandler(client: Client): (handlerData: HandlerData | |
return; | ||
} | ||
|
||
const currentUrl = new URL(WINDOW.location.href); | ||
|
||
let from: string | undefined = handlerData.from; | ||
let to: string | undefined = handlerData.to; | ||
const parsedLoc = parseUrl(WINDOW.location.href); | ||
let parsedFrom = from ? parseUrl(from) : undefined; | ||
const parsedTo = parseUrl(to); | ||
let parsedFrom = from ? new URL(from, DUMMY_URL_BASE) : undefined; | ||
const parsedTo = new URL(to, DUMMY_URL_BASE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should always try-catch this, to avoid stuff blowing up IMHO! |
||
|
||
// Initial pushState doesn't provide `from` information | ||
if (!parsedFrom || !parsedFrom.path) { | ||
parsedFrom = parsedLoc; | ||
if (!parsedFrom || !parsedFrom.pathname) { | ||
parsedFrom = currentUrl; | ||
} | ||
|
||
// Use only the path component of the URL if the URL matches the current | ||
// document (almost all the time when using pushState) | ||
if (parsedLoc.protocol === parsedTo.protocol && parsedLoc.host === parsedTo.host) { | ||
to = parsedTo.relative; | ||
if (currentUrl.origin === parsedTo.origin) { | ||
to = `${parsedTo.pathname}${parsedTo.search}${parsedTo.hash}`; | ||
} | ||
if (parsedLoc.protocol === parsedFrom.protocol && parsedLoc.host === parsedFrom.host) { | ||
from = parsedFrom.relative; | ||
if (currentUrl.origin === parsedFrom.origin) { | ||
from = `${parsedTo.pathname}${parsedTo.search}${parsedTo.hash}`; | ||
} | ||
|
||
addBreadcrumb({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import { | |
startSpan, | ||
withIsolationScope, | ||
} from '@sentry/core'; | ||
import { extractQueryParamsFromUrl, getSanitizedUrlString, parseUrl } from '@sentry/core'; | ||
import type { IntegrationFn, RequestEventData, SpanAttributes } from '@sentry/types'; | ||
|
||
const INTEGRATION_NAME = 'BunServer'; | ||
|
@@ -50,6 +49,9 @@ export function instrumentBunServe(): void { | |
}); | ||
} | ||
|
||
// Just a dummy url base for the `URL` constructor. | ||
const DUMMY_URL_BASE = 'a://'; | ||
|
||
/** | ||
* Instruments Bun.serve `fetch` option to automatically create spans and capture errors. | ||
*/ | ||
|
@@ -63,24 +65,23 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0] | |
return fetchTarget.apply(fetchThisArg, fetchArgs); | ||
} | ||
|
||
const parsedUrl = parseUrl(request.url); | ||
const attributes: SpanAttributes = { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.bun.serve', | ||
[SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD]: request.method || 'GET', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', | ||
}; | ||
|
||
const parsedUrl = new URL(request.url, DUMMY_URL_BASE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try-catch, maybe? |
||
|
||
if (parsedUrl.search) { | ||
attributes['http.query'] = parsedUrl.search; | ||
} | ||
|
||
const url = getSanitizedUrlString(parsedUrl); | ||
|
||
isolationScope.setSDKProcessingMetadata({ | ||
normalizedRequest: { | ||
url, | ||
url: `${parsedUrl.pathname}${parsedUrl.search}`, | ||
method: request.method, | ||
headers: request.headers.toJSON(), | ||
query_string: extractQueryParamsFromUrl(url), | ||
} satisfies RequestEventData, | ||
}); | ||
|
||
|
@@ -91,7 +92,7 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0] | |
{ | ||
attributes, | ||
op: 'http.server', | ||
name: `${request.method} ${parsedUrl.path || '/'}`, | ||
name: `${request.method} ${parsedUrl.pathname || '/'}`, | ||
}, | ||
async span => { | ||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can
resourceUrl
be a path-only, without a host? 🤔 just double checking.