-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureSpan.ts
More file actions
320 lines (279 loc) · 11.8 KB
/
captureSpan.ts
File metadata and controls
320 lines (279 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import type { RawAttributes } from '../../attributes';
import type { Client } from '../../client';
import type { ScopeData } from '../../scope';
import {
SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_RELEASE,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID,
SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_USER_EMAIL,
SEMANTIC_ATTRIBUTE_USER_ID,
SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS,
SEMANTIC_ATTRIBUTE_USER_USERNAME,
} from '../../semanticAttributes';
import type { SerializedStreamedSpan, Span, StreamedSpanJSON } from '../../types-hoist/span';
import { getCombinedScopeData } from '../../utils/scopeData';
import { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from '../../utils/url';
import {
INTERNAL_getSegmentSpan,
showSpanDropWarning,
spanToStreamedSpanJSON,
streamedSpanJsonToSerializedSpan,
} from '../../utils/spanUtils';
import { getCapturedScopesOnSpan } from '../utils';
import { isStreamedBeforeSendSpanCallback } from './beforeSendSpan';
export type SerializedStreamedSpanWithSegmentSpan = SerializedStreamedSpan & {
_segmentSpan: Span;
};
/**
* Captures a span and returns a JSON representation to be enqueued for sending.
*
* IMPORTANT: This function converts the span to JSON immediately to avoid writing
* to an already-ended OTel span instance (which is blocked by the OTel Span class).
*
* @returns the final serialized span with a reference to its segment span. This reference
* is needed later on to compute the DSC for the span envelope.
*/
export function captureSpan(span: Span, client: Client): SerializedStreamedSpanWithSegmentSpan {
// Convert to JSON FIRST - we cannot write to an already-ended span
const spanJSON = spanToStreamedSpanJSON(span);
const segmentSpan = INTERNAL_getSegmentSpan(span);
const serializedSegmentSpan = spanToStreamedSpanJSON(segmentSpan);
const { isolationScope: spanIsolationScope, scope: spanScope } = getCapturedScopesOnSpan(span);
const finalScopeData = getCombinedScopeData(spanIsolationScope, spanScope);
applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, finalScopeData);
// Backfill span data from OTel semantic conventions when not explicitly set.
// OTel-originated spans don't have sentry.op, description, etc. — the non-streamed path
// infers these in the SentrySpanExporter, but streamed spans skip the exporter entirely.
// Access `kind` via duck-typing — OTel span objects have this property but it's not on Sentry's Span type.
// This must run before all hooks and beforeSendSpan so that user callbacks can see and override inferred values.
const spanKind = (span as { kind?: number }).kind;
inferSpanDataFromOtelAttributes(spanJSON, spanKind);
if (spanJSON.is_segment) {
applyScopeToSegmentSpan(spanJSON, finalScopeData);
applySdkMetadataToSegmentSpan(spanJSON, client);
// Allow hook subscribers to mutate the segment span JSON
// This also invokes the `processSegmentSpan` hook of all integrations
client.emit('processSegmentSpan', spanJSON);
}
// This allows hook subscribers to mutate the span JSON
// This also invokes the `processSpan` hook of all integrations
client.emit('processSpan', spanJSON);
const { beforeSendSpan } = client.getOptions();
const processedSpan =
beforeSendSpan && isStreamedBeforeSendSpanCallback(beforeSendSpan)
? applyBeforeSendSpanCallback(spanJSON, beforeSendSpan)
: spanJSON;
// Backfill sentry.span.source from sentry.source. Only `sentry.span.source` is respected by Sentry.
// TODO(v11): Remove this backfill once we renamed SEMANTIC_ATTRIBUTE_SENTRY_SOURCE to sentry.span.source
const spanNameSource = processedSpan.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
if (spanNameSource) {
safeSetSpanJSONAttributes(processedSpan, {
// Purposefully not using a constant defined here like in other attributes:
// This will be the name for SEMANTIC_ATTRIBUTE_SENTRY_SOURCE in v11
'sentry.span.source': spanNameSource,
});
}
return {
...streamedSpanJsonToSerializedSpan(processedSpan),
_segmentSpan: segmentSpan,
};
}
function applyScopeToSegmentSpan(_segmentSpanJSON: StreamedSpanJSON, _scopeData: ScopeData): void {
// TODO: Apply contexts data from auto instrumentation to segment span
// This will follow in a separate PR
}
/**
* Safely set attributes on a span JSON.
* If an attribute already exists, it will not be overwritten.
*/
export function safeSetSpanJSONAttributes(
spanJSON: StreamedSpanJSON,
newAttributes: RawAttributes<Record<string, unknown>>,
): void {
const originalAttributes = spanJSON.attributes ?? (spanJSON.attributes = {});
Object.entries(newAttributes).forEach(([key, value]) => {
if (value != null && !(key in originalAttributes)) {
originalAttributes[key] = value;
}
});
}
function applySdkMetadataToSegmentSpan(segmentSpanJSON: StreamedSpanJSON, client: Client): void {
const integrationNames = client.getOptions().integrations.map(i => i.name);
if (!integrationNames.length) return;
safeSetSpanJSONAttributes(segmentSpanJSON, {
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS]: integrationNames,
});
}
function applyCommonSpanAttributes(
spanJSON: StreamedSpanJSON,
serializedSegmentSpan: StreamedSpanJSON,
client: Client,
scopeData: ScopeData,
): void {
const sdk = client.getSdkMetadata();
const { release, environment } = client.getOptions();
// avoid overwriting any previously set attributes (from users or potentially our SDK instrumentation)
safeSetSpanJSONAttributes(spanJSON, {
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: release,
[SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT]: environment,
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: serializedSegmentSpan.name,
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: serializedSegmentSpan.span_id,
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME]: sdk?.sdk?.name,
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION]: sdk?.sdk?.version,
[SEMANTIC_ATTRIBUTE_USER_ID]: scopeData.user?.id,
[SEMANTIC_ATTRIBUTE_USER_EMAIL]: scopeData.user?.email,
[SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: scopeData.user?.ip_address,
[SEMANTIC_ATTRIBUTE_USER_USERNAME]: scopeData.user?.username,
...scopeData.attributes,
});
}
/**
* Apply a user-provided beforeSendSpan callback to a span JSON.
*/
export function applyBeforeSendSpanCallback(
span: StreamedSpanJSON,
beforeSendSpan: (span: StreamedSpanJSON) => StreamedSpanJSON,
): StreamedSpanJSON {
const modifedSpan = beforeSendSpan(span);
if (!modifedSpan) {
showSpanDropWarning();
return span;
}
return modifedSpan;
}
// OTel SpanKind values (numeric to avoid importing from @opentelemetry/api)
const SPAN_KIND_SERVER = 1;
const SPAN_KIND_CLIENT = 2;
/**
* Infer and backfill span data from OTel semantic conventions.
* This mirrors what the `SentrySpanExporter` does for non-streamed spans via `getSpanData`/`inferSpanData`.
* Streamed spans skip the exporter, so we do the inference here during capture.
*
* Backfills: `sentry.op`, `sentry.source`, and `name` (description).
* Uses `safeSetSpanJSONAttributes` so explicitly set attributes are never overwritten.
*/
/** Exported only for tests. */
export function inferSpanDataFromOtelAttributes(spanJSON: StreamedSpanJSON, spanKind?: number): void {
const attributes = spanJSON.attributes;
if (!attributes) {
return;
}
const httpMethod = attributes['http.request.method'] || attributes['http.method'];
if (httpMethod) {
inferHttpSpanData(spanJSON, attributes, spanKind, httpMethod);
return;
}
const dbSystem = attributes['db.system.name'] || attributes['db.system'];
const opIsCache =
typeof attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'string' &&
`${attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]}`.startsWith('cache.');
if (dbSystem && !opIsCache) {
inferDbSpanData(spanJSON, attributes);
return;
}
if (attributes['rpc.service']) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'rpc' });
return;
}
if (attributes['messaging.system']) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'message' });
return;
}
const faasTrigger = attributes['faas.trigger'];
if (faasTrigger) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${faasTrigger}` });
}
}
function inferHttpSpanData(
spanJSON: StreamedSpanJSON,
attributes: RawAttributes<Record<string, unknown>>,
spanKind: number | undefined,
httpMethod: unknown,
): void {
// Infer op: http.client, http.server, or just http
const opParts = ['http'];
if (spanKind === SPAN_KIND_CLIENT) {
opParts.push('client');
} else if (spanKind === SPAN_KIND_SERVER) {
opParts.push('server');
}
if (attributes['sentry.http.prefetch']) {
opParts.push('prefetch');
}
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: opParts.join('.') });
// If the user set a custom span name via updateSpanName(), apply it — OTel instrumentation
// may have overwritten span.name after the user set it, so we restore from the attribute.
const customName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
if (typeof customName === 'string') {
spanJSON.name = customName;
return;
}
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
return;
}
const httpRoute = attributes['http.route'];
if (typeof httpRoute === 'string') {
spanJSON.name = `${httpMethod} ${httpRoute}`;
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' });
} else {
// Infer span name from URL attributes, matching the non-streamed exporter's behavior.
// Only overwrite the name for OTel spans (known spanKind)
if (spanKind === SPAN_KIND_CLIENT || spanKind === SPAN_KIND_SERVER) {
const urlPath = getUrlPath(attributes, spanKind);
if (urlPath) {
spanJSON.name = `${httpMethod} ${urlPath}`;
}
}
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' });
}
}
/**
* Extract a URL path from span attributes for use in the span name.
* Mirrors the logic in the non-streamed exporter's `getSanitizedUrl`.
*/
function getUrlPath(
attributes: RawAttributes<Record<string, unknown>>,
spanKind: number | undefined,
): string | undefined {
const httpUrl = attributes['http.url'] || attributes['url.full'];
const httpTarget = attributes['http.target'];
const parsedUrl = typeof httpUrl === 'string' ? parseUrl(httpUrl) : undefined;
const sanitizedUrl = parsedUrl ? getSanitizedUrlString(parsedUrl) : undefined;
// For server spans, prefer the relative target path
if (spanKind === SPAN_KIND_SERVER && typeof httpTarget === 'string') {
return stripUrlQueryAndFragment(httpTarget);
}
// For client spans (and others), use the full sanitized URL
if (sanitizedUrl) {
return sanitizedUrl;
}
// Fall back to target if no full URL is available
if (typeof httpTarget === 'string') {
return stripUrlQueryAndFragment(httpTarget);
}
return undefined;
}
function inferDbSpanData(spanJSON: StreamedSpanJSON, attributes: RawAttributes<Record<string, unknown>>): void {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db' });
// If the user set a custom span name via updateSpanName(), apply it.
const customName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
if (typeof customName === 'string') {
spanJSON.name = customName;
return;
}
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
return;
}
const statement = attributes['db.statement'];
if (statement) {
spanJSON.name = `${statement}`;
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task' });
}
}