(scope: Scope | undefined, forceTransaction: boolean | undefined)
| 229 | } |
| 230 | |
| 231 | function getContext(scope: Scope | undefined, forceTransaction: boolean | undefined): Context { |
| 232 | const ctx = getContextForScope(scope); |
| 233 | const parentSpan = trace.getSpan(ctx); |
| 234 | |
| 235 | // In the case that we have no parent span, we start a new trace |
| 236 | // Note that if we continue a trace, we'll always have a remote parent span here anyhow |
| 237 | if (!parentSpan) { |
| 238 | return ctx; |
| 239 | } |
| 240 | |
| 241 | // If we don't want to force a transaction, and we have a parent span, all good, we just return as-is! |
| 242 | if (!forceTransaction) { |
| 243 | return ctx; |
| 244 | } |
| 245 | |
| 246 | // Else, if we do have a parent span but want to force a transaction, we have to simulate a "root" context |
| 247 | |
| 248 | // Else, we need to do two things: |
| 249 | // 1. Unset the parent span from the context, so we'll create a new root span |
| 250 | // 2. Ensure the propagation context is correct, so we'll continue from the parent span |
| 251 | const ctxWithoutSpan = trace.deleteSpan(ctx); |
| 252 | |
| 253 | const { spanId, traceId } = parentSpan.spanContext(); |
| 254 | const sampled = getSamplingDecision(parentSpan.spanContext()); |
| 255 | |
| 256 | // In this case, when we are forcing a transaction, we want to treat this like continuing an incoming trace |
| 257 | // so we set the traceState according to the root span |
| 258 | const rootSpan = getRootSpan(parentSpan); |
| 259 | const dsc = getDynamicSamplingContextFromSpan(rootSpan); |
| 260 | |
| 261 | const traceState = makeTraceState({ |
| 262 | dsc, |
| 263 | sampled, |
| 264 | }); |
| 265 | |
| 266 | const spanOptions: SpanContext = { |
| 267 | traceId, |
| 268 | spanId, |
| 269 | isRemote: true, |
| 270 | traceFlags: sampled ? TraceFlags.SAMPLED : TraceFlags.NONE, |
| 271 | traceState, |
| 272 | }; |
| 273 | |
| 274 | const ctxWithSpanContext = trace.setSpanContext(ctxWithoutSpan, spanOptions); |
| 275 | |
| 276 | return ctxWithSpanContext; |
| 277 | } |
| 278 | |
| 279 | function getContextForScope(scope?: Scope): Context { |
| 280 | if (scope) { |
no test coverage detected