Skip to content

Commit 6c1d18f

Browse files
committed
Remove op from getHttpSpanDetailsFromUrlObject func
1 parent f9a4fc3 commit 6c1d18f

File tree

3 files changed

+83
-25
lines changed

3 files changed

+83
-25
lines changed

packages/cloudflare/src/request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
flush,
66
getHttpSpanDetailsFromUrlObject,
77
parseStringToURLObject,
8+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
89
setHttpStatus,
910
startSpan,
1011
withIsolationScope,
@@ -38,13 +39,15 @@ export function wrapRequestHandler(
3839
isolationScope.setClient(client);
3940

4041
const urlObject = parseStringToURLObject(request.url);
41-
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'auto.http.cloudflare', request);
42+
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'auto.http.cloudflare', request);
4243

4344
const contentLength = request.headers.get('content-length');
4445
if (contentLength) {
4546
attributes['http.request.body.size'] = parseInt(contentLength, 10);
4647
}
4748

49+
attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = 'http.server';
50+
4851
addCloudResourceContext(isolationScope);
4952
if (request) {
5053
addRequest(isolationScope, request);

packages/core/src/utils-hoist/url.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD,
3-
SEMANTIC_ATTRIBUTE_SENTRY_OP,
43
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
54
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
65
SEMANTIC_ATTRIBUTE_URL_FULL,
@@ -62,7 +61,7 @@ export function isURLObjectRelative(url: URLObject): url is RelativeURL {
6261
* @returns The parsed URL object or undefined if the URL is invalid
6362
*/
6463
export function parseStringToURLObject(url: string, urlBase?: string | URL | undefined): URLObject | undefined {
65-
const isRelative = url.startsWith('/');
64+
const isRelative = url.indexOf('://') <= 0 && url.indexOf('//') !== 0;
6665
const base = urlBase ?? (isRelative ? DEFAULT_BASE_URL : undefined);
6766
try {
6867
// Use `canParse` to short-circuit the URL constructor if it's not a valid URL
@@ -147,15 +146,13 @@ function getHttpSpanNameFromUrlObject(
147146
*/
148147
export function getHttpSpanDetailsFromUrlObject(
149148
urlObject: URLObject | undefined,
150-
httpType: 'server' | 'client',
151149
spanOrigin: string,
152150
request?: PartialRequest,
153151
routeName?: string,
154152
): [name: string, attributes: SpanAttributes] {
155153
const attributes: SpanAttributes = {
156154
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin,
157155
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
158-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `http.${httpType}`,
159156
};
160157

161158
if (routeName) {

packages/core/test/utils-hoist/url.test.ts

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,86 @@ describe('parseUrl', () => {
203203
});
204204

205205
describe('parseStringToURLObject', () => {
206-
it('returns undefined for invalid URLs', () => {
207-
expect(parseStringToURLObject('invalid-url')).toBeUndefined();
208-
});
209-
210-
it('returns a URL object for valid URLs', () => {
211-
expect(parseStringToURLObject('https://somedomain.com')).toBeInstanceOf(URL);
212-
});
213-
214-
it('returns a URL object for valid URLs with a base URL', () => {
215-
expect(parseStringToURLObject('https://somedomain.com', 'https://base.com')).toBeInstanceOf(URL);
216-
});
217-
218-
it('returns a relative URL object for relative URLs', () => {
219-
expect(parseStringToURLObject('/path/to/happiness')).toEqual({
220-
isRelative: true,
221-
pathname: '/path/to/happiness',
222-
search: '',
223-
hash: '',
224-
});
206+
it.each([
207+
[
208+
'invalid URL',
209+
'invalid-url',
210+
{
211+
isRelative: true,
212+
pathname: '/invalid-url',
213+
search: '',
214+
hash: '',
215+
},
216+
],
217+
['valid absolute URL', 'https://somedomain.com', expect.any(URL)],
218+
['valid absolute URL with base', 'https://somedomain.com', expect.any(URL), 'https://base.com'],
219+
[
220+
'relative URL',
221+
'/path/to/happiness',
222+
{
223+
isRelative: true,
224+
pathname: '/path/to/happiness',
225+
search: '',
226+
hash: '',
227+
},
228+
],
229+
[
230+
'relative URL with query',
231+
'/path/to/happiness?q=1',
232+
{
233+
isRelative: true,
234+
pathname: '/path/to/happiness',
235+
search: '?q=1',
236+
hash: '',
237+
},
238+
],
239+
[
240+
'relative URL with hash',
241+
'/path/to/happiness#section',
242+
{
243+
isRelative: true,
244+
pathname: '/path/to/happiness',
245+
search: '',
246+
hash: '#section',
247+
},
248+
],
249+
[
250+
'relative URL with query and hash',
251+
'/path/to/happiness?q=1#section',
252+
{
253+
isRelative: true,
254+
pathname: '/path/to/happiness',
255+
search: '?q=1',
256+
hash: '#section',
257+
},
258+
],
259+
['URL with port', 'https://somedomain.com:8080/path', expect.any(URL)],
260+
['URL with auth', 'https://user:[email protected]', expect.any(URL)],
261+
['URL with special chars', 'https://somedomain.com/path/with spaces/and/special@chars', expect.any(URL)],
262+
['URL with unicode', 'https://somedomain.com/path/with/unicode/测试', expect.any(URL)],
263+
['URL with multiple query params', 'https://somedomain.com/path?q1=1&q2=2&q3=3', expect.any(URL)],
264+
['URL with encoded chars', 'https://somedomain.com/path/%20%2F%3F%23', expect.any(URL)],
265+
['URL with IPv4', 'https://192.168.1.1/path', expect.any(URL)],
266+
['URL with IPv6', 'https://[2001:db8::1]/path', expect.any(URL)],
267+
['URL with subdomain', 'https://sub.somedomain.com/path', expect.any(URL)],
268+
['URL with multiple subdomains', 'https://sub1.sub2.somedomain.com/path', expect.any(URL)],
269+
['URL with trailing slash', 'https://somedomain.com/path/', expect.any(URL)],
270+
['URL with empty path', 'https://somedomain.com', expect.any(URL)],
271+
['URL with root path', 'https://somedomain.com/', expect.any(URL)],
272+
['URL with file extension', 'https://somedomain.com/path/file.html', expect.any(URL)],
273+
['URL with custom protocol', 'custom://somedomain.com/path', expect.any(URL)],
274+
['URL with query containing special chars', 'https://somedomain.com/path?q=hello+world&x=1/2', expect.any(URL)],
275+
['URL with hash containing special chars', 'https://somedomain.com/path#section/1/2', expect.any(URL)],
276+
[
277+
'URL with all components',
278+
'https://user:[email protected]:8080/path/file.html?q=1#section',
279+
expect.any(URL),
280+
],
281+
])('handles %s', (_, url: string, expected: any, base?: string) => {
282+
expect(parseStringToURLObject(url, base)).toEqual(expected);
225283
});
226284

227-
it('does not throw an error if URl.canParse is not defined', () => {
285+
it('does not throw an error if URL.canParse is not defined', () => {
228286
const canParse = (URL as any).canParse;
229287
delete (URL as any).canParse;
230288
expect(parseStringToURLObject('https://somedomain.com')).toBeInstanceOf(URL);

0 commit comments

Comments
 (0)