|
1 | 1 | /* eslint-disable max-lines */ // TODO: We might want to split this file up |
2 | 2 | import type { ReplayRecordingMode, Span } from '@sentry/core'; |
3 | | -import { getActiveSpan, getClient, getRootSpan, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, spanToJSON } from '@sentry/core'; |
| 3 | +import { |
| 4 | + getActiveSpan, |
| 5 | + getClient, |
| 6 | + getCurrentScope, |
| 7 | + getRootSpan, |
| 8 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 9 | + spanToJSON, |
| 10 | +} from '@sentry/core'; |
4 | 11 | import { EventType, record } from '@sentry-internal/rrweb'; |
5 | 12 | import { |
6 | 13 | BUFFER_CHECKOUT_TIME, |
@@ -192,7 +199,7 @@ export class ReplayContainer implements ReplayContainerInterface { |
192 | 199 | this._hasInitializedCoreListeners = false; |
193 | 200 | this._context = { |
194 | 201 | errorIds: new Set(), |
195 | | - traceIds: new Set(), |
| 202 | + traceIds: [], |
196 | 203 | urls: [], |
197 | 204 | initialTimestamp: Date.now(), |
198 | 205 | initialUrl: '', |
@@ -1097,7 +1104,11 @@ export class ReplayContainer implements ReplayContainerInterface { |
1097 | 1104 | private _clearContext(): void { |
1098 | 1105 | // XXX: `initialTimestamp` and `initialUrl` do not get cleared |
1099 | 1106 | this._context.errorIds.clear(); |
1100 | | - this._context.traceIds.clear(); |
| 1107 | + // We want to preserve the most recent trace id for the next replay segment. |
| 1108 | + // This is so that we can associate replay events w/ the trace. |
| 1109 | + if (this._context.traceIds.length > 1) { |
| 1110 | + this._context.traceIds = this._context.traceIds.slice(-1); |
| 1111 | + } |
1101 | 1112 | this._context.urls = []; |
1102 | 1113 | } |
1103 | 1114 |
|
@@ -1125,11 +1136,17 @@ export class ReplayContainer implements ReplayContainerInterface { |
1125 | 1136 | * Return and clear _context |
1126 | 1137 | */ |
1127 | 1138 | private _popEventContext(): PopEventContext { |
| 1139 | + if (this._context.traceIds.length === 0) { |
| 1140 | + const currentTraceId = getCurrentScope().getPropagationContext().traceId; |
| 1141 | + if (currentTraceId) { |
| 1142 | + this._context.traceIds.push([-1, currentTraceId]); |
| 1143 | + } |
| 1144 | + } |
1128 | 1145 | const _context = { |
1129 | 1146 | initialTimestamp: this._context.initialTimestamp, |
1130 | 1147 | initialUrl: this._context.initialUrl, |
1131 | 1148 | errorIds: Array.from(this._context.errorIds), |
1132 | | - traceIds: Array.from(this._context.traceIds), |
| 1149 | + traceIds: this._context.traceIds, |
1133 | 1150 | urls: this._context.urls, |
1134 | 1151 | }; |
1135 | 1152 |
|
|
0 commit comments