Unverified Commit 49c5fe6f authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

test: record span parents as exporters see them at span end

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 4dd01b89
...@@ -25,7 +25,9 @@ const PRINTED_ATTRIBUTES = [ ...@@ -25,7 +25,9 @@ const PRINTED_ATTRIBUTES = [
const MAX_REMEMBERED_SPANS = 10_000 const MAX_REMEMBERED_SPANS = 10_000
interface Store { interface Store {
// The parent of each span started here, from `onStart`'s parent context. // The parent of each span started here, from `onStart`'s parent context:
// only a fallback for spans without an SDK parent field (see
// `getParentAtEnd`).
parents: Map<string, string | undefined> parents: Map<string, string | undefined>
// The traces of the `instrumentation.ts` lifecycle spans ended here. // The traces of the `instrumentation.ts` lifecycle spans ended here.
lifecycleTraceIds: Set<string> lifecycleTraceIds: Set<string>
...@@ -57,6 +59,26 @@ function getType(span: { attributes: ReadableSpan['attributes'] }) { ...@@ -57,6 +59,26 @@ function getType(span: { attributes: ReadableSpan['attributes'] }) {
return typeof type === 'string' ? type : undefined return typeof type === 'string' ? type : undefined
} }
// The parent a span has when it ends, as span processors' `onEnd` and
// exporters see it: the adapter can make a started span a root before it ends
// (see `clearSpanParent` in `deferred-request-span.ts`). The field's presence
// decides, as `undefined` is a root: `parentSpanContext` in
// `@opentelemetry/sdk-trace` 2.x, `parentSpanId` in `sdk-trace-base` 1.x.
// Falls back to the parent at start for a span with neither.
function getParentAtEnd(
span: ReadableSpan,
parentAtStart: () => string | undefined
): string | undefined {
const sdkSpan = span as ReadableSpan & { parentSpanId?: string }
if ('parentSpanContext' in sdkSpan) {
return sdkSpan.parentSpanContext?.spanId
}
if ('parentSpanId' in sdkSpan) {
return sdkSpan.parentSpanId
}
return parentAtStart()
}
function write(event: Record<string, unknown>) { function write(event: Record<string, unknown>) {
const store = getStore() const store = getStore()
// Unique per event written, so the test can tell redeliveries of a log line // Unique per event written, so the test can tell redeliveries of a log line
...@@ -88,9 +110,7 @@ export const spanRecorder: SpanProcessor = { ...@@ -88,9 +110,7 @@ export const spanRecorder: SpanProcessor = {
onEnd(span: ReadableSpan) { onEnd(span: ReadableSpan) {
const { traceId, spanId } = span.spanContext() const { traceId, spanId } = span.spanContext()
const store = getStore() const store = getStore()
const parentId = store.parents.has(spanId) const parentId = getParentAtEnd(span, () => store.parents.get(spanId))
? store.parents.get(spanId)
: span.parentSpanContext?.spanId
store.parents.delete(spanId) store.parents.delete(spanId)
const type = getType(span) const type = getType(span)
if (type?.startsWith('Instrumentation.')) { if (type?.startsWith('Instrumentation.')) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment