Skip to content

Commit 19b3bae

Browse files
AbhiPrasadbillyvg
authored andcommitted
feat(node): Add http.method to node http spans (#7991)
1 parent 319e5c6 commit 19b3bae

File tree

9 files changed

+44
-13
lines changed

9 files changed

+44
-13
lines changed

packages/node/src/integrations/http.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ function _createWrappedRequestMethodFactory(
187187

188188
const scope = getCurrentHub().getScope();
189189

190+
const method = requestOptions.method || 'GET';
190191
const requestSpanData: SanitizedRequestData = {
191192
url: requestUrl,
192-
method: requestOptions.method || 'GET',
193+
'http.method': method,
193194
};
194195
if (requestOptions.hash) {
195196
// strip leading "#"
@@ -205,7 +206,7 @@ function _createWrappedRequestMethodFactory(
205206

206207
if (parentSpan) {
207208
requestSpan = parentSpan.startChild({
208-
description: `${requestSpanData.method} ${requestSpanData.url}`,
209+
description: `${method} ${requestSpanData.url}`,
209210
op: 'http.client',
210211
data: requestSpanData,
211212
});

packages/node/src/integrations/undici/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,20 @@ export class Undici implements Integration {
111111
const shouldCreateSpan = this._options.shouldCreateSpanForRequest(stringUrl);
112112

113113
if (shouldCreateSpan) {
114-
const data: Record<string, unknown> = {};
114+
const method = request.method || 'GET';
115+
const data: Record<string, unknown> = {
116+
'http.method': method,
117+
};
115118
const params = url.searchParams.toString();
116119
if (params) {
117120
data['http.query'] = `?${params}`;
118121
}
119122
if (url.hash) {
120123
data['http.fragment'] = url.hash;
121124
}
122-
123125
const span = activeSpan.startChild({
124126
op: 'http.client',
125-
description: `${request.method || 'GET'} ${stripUrlQueryAndFragment(stringUrl)}`,
127+
description: `${method} ${stripUrlQueryAndFragment(stringUrl)}`,
126128
data,
127129
});
128130
request.__sentry__ = span;

packages/node/test/integrations/http.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('tracing', () => {
208208
// our span is at index 1 because the transaction itself is at index 0
209209
expect(spans[1].description).toEqual('GET http://dogs.are.great/spaniel');
210210
expect(spans[1].op).toEqual('http.client');
211-
expect(spans[1].data.method).toEqual('GET');
211+
expect(spans[1].data['http.method']).toEqual('GET');
212212
expect(spans[1].data.url).toEqual('http://dogs.are.great/spaniel');
213213
expect(spans[1].data['http.query']).toEqual('tail=wag&cute=true');
214214
expect(spans[1].data['http.fragment']).toEqual('learn-more');

packages/node/test/integrations/undici.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ conditionalTest({ min: 16 })('Undici integration', () => {
5656
{
5757
description: 'GET http://localhost:18099/',
5858
op: 'http.client',
59+
data: {
60+
'http.method': 'GET',
61+
},
5962
},
6063
],
6164
[
@@ -66,6 +69,7 @@ conditionalTest({ min: 16 })('Undici integration', () => {
6669
description: 'GET http://localhost:18099/',
6770
op: 'http.client',
6871
data: {
72+
'http.method': 'GET',
6973
'http.query': '?foo=bar',
7074
},
7175
},
@@ -76,6 +80,9 @@ conditionalTest({ min: 16 })('Undici integration', () => {
7680
{ method: 'POST' },
7781
{
7882
description: 'POST http://localhost:18099/',
83+
data: {
84+
'http.method': 'POST',
85+
},
7986
},
8087
],
8188
[
@@ -84,6 +91,9 @@ conditionalTest({ min: 16 })('Undici integration', () => {
8491
{ method: 'POST' },
8592
{
8693
description: 'POST http://localhost:18099/',
94+
data: {
95+
'http.method': 'POST',
96+
},
8797
},
8898
],
8999
[

packages/sveltekit/src/client/load.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
152152

153153
const requestData: SanitizedRequestData = {
154154
url: getSanitizedUrlString(urlObject),
155-
method,
155+
'http.method': method,
156156
...(urlObject.search && { 'http.query': urlObject.search.substring(1) }),
157157
...(urlObject.hash && { 'http.hash': urlObject.hash.substring(1) }),
158158
};
@@ -190,7 +190,7 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
190190
if (createSpan) {
191191
fetchPromise = trace(
192192
{
193-
name: `${requestData.method} ${requestData.url}`, // this will become the description of the span
193+
name: `${method} ${requestData.url}`, // this will become the description of the span
194194
op: 'http.client',
195195
data: requestData,
196196
},

packages/sveltekit/src/server/load.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
115115
source: routeId ? 'route' : 'url',
116116
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
117117
},
118+
data: {
119+
'http.method': event.request.method,
120+
},
118121
...traceparentData,
119122
};
120123

packages/sveltekit/test/client/load.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe('wrapLoadWithSentry', () => {
205205
op: 'http.client',
206206
name: 'GET example.com/api/users/',
207207
data: {
208-
method: 'GET',
208+
'http.method': 'GET',
209209
url: 'example.com/api/users/',
210210
'http.hash': 'testfragment',
211211
'http.query': 'id=123',
@@ -219,7 +219,7 @@ describe('wrapLoadWithSentry', () => {
219219
op: 'http.client',
220220
name: 'POST example.com/api/users/',
221221
data: {
222-
method: 'POST',
222+
'http.method': 'POST',
223223
url: 'example.com/api/users/',
224224
'http.hash': 'testfragment',
225225
'http.query': 'id=123',
@@ -233,7 +233,7 @@ describe('wrapLoadWithSentry', () => {
233233
op: 'http.client',
234234
name: 'POST example.com/api/users/',
235235
data: {
236-
method: 'POST',
236+
'http.method': 'POST',
237237
url: 'example.com/api/users/',
238238
'http.hash': 'testfragment',
239239
'http.query': 'id=123',
@@ -247,7 +247,7 @@ describe('wrapLoadWithSentry', () => {
247247
op: 'http.client',
248248
name: 'GET /api/users',
249249
data: {
250-
method: 'GET',
250+
'http.method': 'GET',
251251
url: '/api/users',
252252
'http.query': 'id=123',
253253
},

packages/sveltekit/test/server/load.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const MOCK_LOAD_NO_ROUTE_ARGS: any = {
6464
const MOCK_SERVER_ONLY_LOAD_ARGS: any = {
6565
...MOCK_LOAD_ARGS,
6666
request: {
67+
method: 'GET',
6768
headers: {
6869
get: (key: string) => {
6970
if (key === 'sentry-trace') {
@@ -87,6 +88,7 @@ const MOCK_SERVER_ONLY_LOAD_ARGS: any = {
8788
const MOCK_SERVER_ONLY_NO_TRACE_LOAD_ARGS: any = {
8889
...MOCK_LOAD_ARGS,
8990
request: {
91+
method: 'GET',
9092
headers: {
9193
get: (_: string) => {
9294
return null;
@@ -98,6 +100,7 @@ const MOCK_SERVER_ONLY_NO_TRACE_LOAD_ARGS: any = {
98100
const MOCK_SERVER_ONLY_NO_BAGGAGE_LOAD_ARGS: any = {
99101
...MOCK_LOAD_ARGS,
100102
request: {
103+
method: 'GET',
101104
headers: {
102105
get: (key: string) => {
103106
if (key === 'sentry-trace') {
@@ -268,6 +271,9 @@ describe('wrapServerLoadWithSentry calls trace', () => {
268271
parentSpanId: '1234567890abcdef',
269272
status: 'ok',
270273
traceId: '1234567890abcdef1234567890abcdef',
274+
data: {
275+
'http.method': 'GET',
276+
},
271277
metadata: {
272278
dynamicSamplingContext: {
273279
environment: 'production',
@@ -296,6 +302,9 @@ describe('wrapServerLoadWithSentry calls trace', () => {
296302
op: 'function.sveltekit.server.load',
297303
name: '/users/[id]',
298304
status: 'ok',
305+
data: {
306+
'http.method': 'GET',
307+
},
299308
metadata: {
300309
source: 'route',
301310
},
@@ -318,6 +327,9 @@ describe('wrapServerLoadWithSentry calls trace', () => {
318327
parentSpanId: '1234567890abcdef',
319328
status: 'ok',
320329
traceId: '1234567890abcdef1234567890abcdef',
330+
data: {
331+
'http.method': 'GET',
332+
},
321333
metadata: {
322334
dynamicSamplingContext: {},
323335
source: 'route',
@@ -343,6 +355,9 @@ describe('wrapServerLoadWithSentry calls trace', () => {
343355
parentSpanId: '1234567890abcdef',
344356
status: 'ok',
345357
traceId: '1234567890abcdef1234567890abcdef',
358+
data: {
359+
'http.method': 'GET',
360+
},
346361
metadata: {
347362
dynamicSamplingContext: {
348363
environment: 'production',

packages/types/src/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type QueryParams = string | { [key: string]: string } | Array<[string, st
1818
*/
1919
export type SanitizedRequestData = {
2020
url: string;
21-
method: string;
21+
'http.method': string;
2222
'http.fragment'?: string;
2323
'http.query'?: string;
2424
};

0 commit comments

Comments
 (0)