-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureSpan.ts
More file actions
150 lines (133 loc) · 5.53 KB
/
captureSpan.ts
File metadata and controls
150 lines (133 loc) · 5.53 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
import type { RawAttributes } from '../../attributes';
import type { Client } from '../../client';
import type { ScopeData } from '../../scope';
import {
SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT,
SEMANTIC_ATTRIBUTE_SENTRY_RELEASE,
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 { isStreamedBeforeSendSpanCallback } from '../../utils/beforeSendSpan';
import { getCombinedScopeData } from '../../utils/scopeData';
import {
INTERNAL_getSegmentSpan,
showSpanDropWarning,
spanToStreamedSpanJSON,
streamedSpanJsonToSerializedSpan,
} from '../../utils/spanUtils';
import { getCapturedScopesOnSpan } from '../utils';
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);
if (span === segmentSpan) {
applyScopeToSegmentSpan(spanJSON, finalScopeData);
// Allow hook subscribers to mutate the segment span JSON
client.emit('processSegmentSpan', spanJSON);
}
// Allow hook subscribers to mutate the span JSON
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 all scope and request data from auto instrumentation (contexts, request) to segment span
// This will follow in a separate PR
}
function applyCommonSpanAttributes(
spanJSON: StreamedSpanJSON,
serializedSegmentSpan: StreamedSpanJSON,
client: Client,
scopeData: ScopeData,
): void {
const sdk = client.getSdkMetadata();
const { release, environment, sendDefaultPii } = 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,
...(sendDefaultPii
? {
[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;
}
/**
* 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;
}
});
}