forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.js
127 lines (102 loc) · 3.62 KB
/
trace.js
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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const SpanRecorder = require('./span-recorder')
const SpanEventRecorder = require('./span-event-recorder')
const Span = require('./span')
const SpanEvent = require('./span-event')
const AsyncTrace = require('./async-trace')
const BufferedStorage = require('./buffered-storage')
const SpanChunk = require('./span-chunk')
const log = require('../utils/logger')
class Trace {
constructor(traceId, agentInfo, dataSender, sampling, requestData) {
this.traceId = traceId
this.agentInfo = agentInfo
this.dataSender = dataSender
this.span = new Span(traceId, agentInfo, requestData)
this.spanRecorder = new SpanRecorder(this.span)
this.spanEventRecorder = null
const createSpanChunk = SpanChunk.getFactoryMethod(traceId, agentInfo)
this.storage = new BufferedStorage(dataSender, createSpanChunk)
this.callStack = []
this.sequence = 0
this.sampling = sampling
}
traceBlockBegin() {
const spanEvent = new SpanEvent(this.span, this.sequence)
spanEvent.depth = this.callStack.length + 1
spanEvent.startTime = Date.now()
spanEvent.startElapsed = spanEvent.startTime - this.span.startTime
this.callStack.push(spanEvent)
this.sequence++
this.spanEventRecorder = new SpanEventRecorder(spanEvent, this.span)
return this.spanEventRecorder
}
traceBlockEnd(spanEventRecorder) {
if (!spanEventRecorder || this.callStack.length < 1) {
return null
}
const index = this.callStack.findIndex(item => item === spanEventRecorder.spanEvent)
if (index < 0) {
log.error('SpanEvent does not exists in call-stack', spanEventRecorder.spanEvent)
return null
}
spanEventRecorder.error && delete spanEventRecorder.error._pinpointCheck
if (index === this.callStack.length - 1) {
this.completeSpanEvent(this.callStack.pop())
} else {
const spanEvent = this.callStack.splice(index, 1)
if (spanEvent && spanEvent.length > 0) {
log.warn('Remove spanEvent from middle of call-stack', spanEvent[0])
this.completeSpanEvent(spanEvent[0])
}
}
}
completeSpanEvent(spanEvent) {
if (spanEvent && spanEvent.isValid()) {
spanEvent.markElapsedTime()
this.storage.storeSpanEvent(spanEvent)
// this.spanRecorder.recordSpanEvent(spanEvent)
}
}
newAsyncTrace(spanEventRecorder) {
if (spanEventRecorder) {
const asyncId = spanEventRecorder.recordNextAsyncId()
return new AsyncTrace(this.span, asyncId, this.traceId, this.agentInfo, this.dataSender, this.sampling)
}
}
newAsyncTraceWithId(asyncId) {
if (asyncId) {
return new AsyncTrace(this.span, asyncId, this.traceId, this.agentInfo, this.dataSender, this.sampling)
}
}
traceCallbackBegin(dummyId) {
const spanEvent = new SpanEvent(this.span, 0)
spanEvent.dummyId = dummyId
spanEvent.startTime = Date.now()
spanEvent.startElapsed = spanEvent.startTime - this.span.startTime
this.spanEventRecorder = new SpanEventRecorder(spanEvent, this.span)
return this.spanEventRecorder
}
traceCallbackEnd(spanEventRecorder) {
if (!spanEventRecorder || !spanEventRecorder.spanEvent) return
// this.spanRecorder.recordSpanEvent(spanEventRecorder.spanEvent)
this.storage.storeSpanEvent(spanEventRecorder.spanEvent)
spanEventRecorder.spanEvent.markElapsedTime()
spanEventRecorder.recordEnd()
}
canSampled() {
return this.sampling
}
getStartTime() {
return (this.span && this.span.startTime) || 0
}
close() {
this.storage.storeSpan(this.span)
}
}
module.exports = Trace