/
autoparent.ts
72 lines (64 loc) · 1.91 KB
/
autoparent.ts
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
import { trace, Span } from './trace'
import { debugLog } from './shared'
const stacks = new WeakMap<any, Array<Span>>()
const stoppedSpansSets = new WeakMap<any, Set<Span>>()
export function stackPush(keyObj: any, spanName: string, attrs?: any): Span {
let stack = stacks.get(keyObj)
let span
if (!stack) {
stack = []
stacks.set(keyObj, stack)
span = trace(spanName, undefined, attrs ? attrs() : undefined)
} else {
const parent = stack[stack.length - 1]
if (parent) {
span = trace(spanName, parent.id, attrs ? attrs() : undefined)
} else {
span = trace(spanName, undefined, attrs ? attrs() : undefined)
}
}
stack.push(span)
return span
}
export function stackPop(keyObj: any, span: any): void {
let stack = stacks.get(keyObj)
if (!stack) {
debugLog(
'Attempted to pop from non-existent stack. Key reference must be bad.'
)
return
}
let stoppedSpans = stoppedSpansSets.get(keyObj)
if (!stoppedSpans) {
stoppedSpans = new Set()
stoppedSpansSets.set(keyObj, stoppedSpans)
}
if (stoppedSpans.has(span)) {
debugLog(
`Attempted to terminate tracing span that was already stopped for ${span.name}`
)
return
}
while (true) {
let poppedSpan = stack.pop()
if (poppedSpan && poppedSpan === span) {
stoppedSpans.add(poppedSpan)
span.stop()
stoppedSpans.add(span)
break
} else if (poppedSpan === undefined || stack.indexOf(span) === -1) {
// We've either reached the top of the stack or the stack doesn't contain
// the span for another reason.
debugLog(`Tracing span was not found in stack for: ${span.name}`)
stoppedSpans.add(span)
span.stop()
break
} else if (stack.indexOf(span) !== -1) {
debugLog(
`Attempted to pop span that was not at top of stack for: ${span.name}`
)
stoppedSpans.add(poppedSpan)
poppedSpan.stop()
}
}
}