Skip to content

Commit fd48621

Browse files
committed
refactor
1 parent 862e0b2 commit fd48621

2 files changed

Lines changed: 97 additions & 89 deletions

File tree

packages/core/src/tracing/spans/captureSpan.ts

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ import {
1818
} from '../../semanticAttributes';
1919
import type { RequestDataIncludeOptions } from '../../integrations/requestdata';
2020
import type { Integration } from '../../types-hoist/integration';
21-
import type { QueryParams, RequestEventData } from '../../types-hoist/request';
2221
import type { SerializedStreamedSpan, Span, StreamedSpanJSON } from '../../types-hoist/span';
23-
import { httpHeadersToSpanAttributes } from '../../utils/request';
2422
import { getCombinedScopeData } from '../../utils/scopeData';
2523
import { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from '../../utils/url';
26-
import { getClientIPAddress, ipHeaderNames } from '../../vendor/getIpAddress';
2724
import {
2825
INTERNAL_getSegmentSpan,
2926
showSpanDropWarning,
@@ -32,6 +29,7 @@ import {
3229
} from '../../utils/spanUtils';
3330
import { getCapturedScopesOnSpan } from '../utils';
3431
import { isStreamedBeforeSendSpanCallback } from './beforeSendSpan';
32+
import { applyRequestDataToSegmentSpan } from './requestData';
3533

3634
export type SerializedStreamedSpanWithSegmentSpan = SerializedStreamedSpan & {
3735
_segmentSpan: Span;
@@ -115,92 +113,6 @@ function applyScopeToSegmentSpan(segmentSpanJSON: StreamedSpanJSON, scopeData: S
115113
}
116114
}
117115

118-
// Span-streaming counterpart of requestDataIntegration's processEvent.
119-
function applyRequestDataToSegmentSpan(
120-
segmentSpanJSON: StreamedSpanJSON,
121-
normalizedRequest: RequestEventData,
122-
ipAddress: string | undefined,
123-
include: RequestDataIncludeOptions,
124-
sendDefaultPii: boolean | undefined,
125-
): void {
126-
const attributes: Record<string, unknown> = {};
127-
128-
if (include.url && normalizedRequest.url) {
129-
attributes['url.full'] = normalizedRequest.url;
130-
}
131-
132-
if (normalizedRequest.method) {
133-
attributes['http.request.method'] = normalizedRequest.method;
134-
}
135-
136-
if (include.query_string && normalizedRequest.query_string) {
137-
attributes['url.query'] = normalizeQueryString(normalizedRequest.query_string);
138-
}
139-
140-
safeSetSpanJSONAttributes(segmentSpanJSON, attributes);
141-
142-
if (include.headers && normalizedRequest.headers) {
143-
const headers = { ...normalizedRequest.headers };
144-
145-
if (!include.cookies) {
146-
delete headers.cookie;
147-
}
148-
149-
if (!include.ip) {
150-
const ipHeaderNamesLower = new Set(ipHeaderNames.map(name => name.toLowerCase()));
151-
for (const key of Object.keys(headers)) {
152-
if (ipHeaderNamesLower.has(key.toLowerCase())) {
153-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
154-
delete headers[key];
155-
}
156-
}
157-
}
158-
159-
const headerAttributes = httpHeadersToSpanAttributes(headers, sendDefaultPii ?? false, 'request');
160-
safeSetSpanJSONAttributes(segmentSpanJSON, headerAttributes);
161-
}
162-
163-
if (include.cookies && normalizedRequest.cookies) {
164-
const cookieString = Object.entries(normalizedRequest.cookies)
165-
.map(([name, value]) => `${name}=${value}`)
166-
.join('; ');
167-
if (cookieString) {
168-
const cookieAttributes = httpHeadersToSpanAttributes(
169-
{ cookie: cookieString },
170-
sendDefaultPii ?? false,
171-
'request',
172-
);
173-
safeSetSpanJSONAttributes(segmentSpanJSON, cookieAttributes);
174-
}
175-
}
176-
177-
if (include.data && normalizedRequest.data != null) {
178-
const serialized =
179-
typeof normalizedRequest.data === 'string' ? normalizedRequest.data : JSON.stringify(normalizedRequest.data);
180-
if (serialized) {
181-
safeSetSpanJSONAttributes(segmentSpanJSON, { 'http.request.body.data': serialized });
182-
}
183-
}
184-
185-
if (include.ip) {
186-
const ip = (normalizedRequest.headers && getClientIPAddress(normalizedRequest.headers)) || ipAddress || undefined;
187-
if (ip) {
188-
safeSetSpanJSONAttributes(segmentSpanJSON, { [SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: ip });
189-
}
190-
}
191-
}
192-
193-
function normalizeQueryString(queryString: QueryParams): string | undefined {
194-
if (typeof queryString === 'string') {
195-
return queryString || undefined;
196-
}
197-
198-
const pairs = Array.isArray(queryString) ? queryString : Object.entries(queryString);
199-
const result = pairs.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');
200-
201-
return result || undefined;
202-
}
203-
204116
function applyCommonSpanAttributes(
205117
spanJSON: StreamedSpanJSON,
206118
serializedSegmentSpan: StreamedSpanJSON,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import type { RequestDataIncludeOptions } from '../../integrations/requestdata';
2+
import { SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS } from '../../semanticAttributes';
3+
import type { QueryParams, RequestEventData } from '../../types-hoist/request';
4+
import type { StreamedSpanJSON } from '../../types-hoist/span';
5+
import { httpHeadersToSpanAttributes } from '../../utils/request';
6+
import { getClientIPAddress, ipHeaderNames } from '../../vendor/getIpAddress';
7+
import { safeSetSpanJSONAttributes } from './captureSpan';
8+
9+
// Span-streaming counterpart of requestDataIntegration's processEvent.
10+
export function applyRequestDataToSegmentSpan(
11+
segmentSpanJSON: StreamedSpanJSON,
12+
normalizedRequest: RequestEventData,
13+
ipAddress: string | undefined,
14+
include: RequestDataIncludeOptions,
15+
sendDefaultPii: boolean | undefined,
16+
): void {
17+
const attributes: Record<string, unknown> = {};
18+
19+
if (include.url && normalizedRequest.url) {
20+
attributes['url.full'] = normalizedRequest.url;
21+
}
22+
23+
if (normalizedRequest.method) {
24+
attributes['http.request.method'] = normalizedRequest.method;
25+
}
26+
27+
if (include.query_string && normalizedRequest.query_string) {
28+
attributes['url.query'] = normalizeQueryString(normalizedRequest.query_string);
29+
}
30+
31+
safeSetSpanJSONAttributes(segmentSpanJSON, attributes);
32+
33+
if (include.headers && normalizedRequest.headers) {
34+
const headers = { ...normalizedRequest.headers };
35+
36+
if (!include.cookies) {
37+
delete headers.cookie;
38+
}
39+
40+
if (!include.ip) {
41+
const ipHeaderNamesLower = new Set(ipHeaderNames.map(name => name.toLowerCase()));
42+
for (const key of Object.keys(headers)) {
43+
if (ipHeaderNamesLower.has(key.toLowerCase())) {
44+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
45+
delete headers[key];
46+
}
47+
}
48+
}
49+
50+
const headerAttributes = httpHeadersToSpanAttributes(headers, sendDefaultPii ?? false, 'request');
51+
safeSetSpanJSONAttributes(segmentSpanJSON, headerAttributes);
52+
}
53+
54+
if (include.cookies) {
55+
const cookieString = normalizedRequest.cookies
56+
? Object.entries(normalizedRequest.cookies)
57+
.map(([name, value]) => `${name}=${value}`)
58+
.join('; ')
59+
: normalizedRequest.headers?.cookie;
60+
61+
if (cookieString) {
62+
const cookieAttributes = httpHeadersToSpanAttributes(
63+
{ cookie: cookieString },
64+
sendDefaultPii ?? false,
65+
'request',
66+
);
67+
safeSetSpanJSONAttributes(segmentSpanJSON, cookieAttributes);
68+
}
69+
}
70+
71+
if (include.data && normalizedRequest.data != null) {
72+
const serialized =
73+
typeof normalizedRequest.data === 'string' ? normalizedRequest.data : JSON.stringify(normalizedRequest.data);
74+
if (serialized) {
75+
safeSetSpanJSONAttributes(segmentSpanJSON, { 'http.request.body.data': serialized });
76+
}
77+
}
78+
79+
if (include.ip) {
80+
const ip = (normalizedRequest.headers && getClientIPAddress(normalizedRequest.headers)) || ipAddress || undefined;
81+
if (ip) {
82+
safeSetSpanJSONAttributes(segmentSpanJSON, { [SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: ip });
83+
}
84+
}
85+
}
86+
87+
function normalizeQueryString(queryString: QueryParams): string | undefined {
88+
if (typeof queryString === 'string') {
89+
return queryString || undefined;
90+
}
91+
92+
const pairs = Array.isArray(queryString) ? queryString : Object.entries(queryString);
93+
const result = pairs.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');
94+
95+
return result || undefined;
96+
}

0 commit comments

Comments
 (0)