-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathasync-trace.js
71 lines (56 loc) · 2.1 KB
/
async-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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const SpanEventRecorder = require('./span-event-recorder')
const SpanEvent = require('./span-event')
const SpanChunk = require('./span-chunk')
const BufferedStorage = require('./buffered-storage')
const {ServiceTypeCode} = require('../constant/service-type')
const {GeneralMethodDescriptor} = require('../constant/method-descriptor')
class AsyncTrace {
constructor (span, asyncId, traceId, agentInfo, dataSender, sampling) {
this.span = span
this.traceId = traceId
this.agentInfo = agentInfo
this.asyncId = asyncId
this.spanEventRecorder = null
const createAsyncSpanChunk = SpanChunk.getAsyncFactoryMethod(traceId, agentInfo, asyncId)
this.storage = new BufferedStorage(dataSender, createAsyncSpanChunk)
this.callStack = []
this.sequence = 0
this.sampling = sampling
const spanEventRecorder = this.traceAsyncBegin()
spanEventRecorder.recordServiceType(ServiceTypeCode.async)
spanEventRecorder.recordApiId(GeneralMethodDescriptor.AsyncInvocation.apiId)
spanEventRecorder.spanEvent.endPoint = null
spanEventRecorder.spanEvent.destinationId = null
this.storage.storeSpanEvent(spanEventRecorder.spanEvent)
}
traceAsyncBegin () {
const spanEvent = new SpanEvent(this.span, this.sequence)
spanEvent.startTime = Date.now()
spanEvent.startElapsed = spanEvent.startTime - this.span.startTime
spanEvent.depth = this.callStack.length + 1
this.callStack.push(spanEvent)
this.sequence++
return new SpanEventRecorder(spanEvent, this.span)
}
traceAsyncEnd (spanEventRecorder) {
if (!spanEventRecorder || !spanEventRecorder.spanEvent) return
this.storage.storeSpanEvent(spanEventRecorder.spanEvent)
this.storage.flush()
spanEventRecorder.spanEvent.markElapsedTime()
this.callStack = this.callStack.filter(spanEvent => spanEvent.sequence != spanEventRecorder.spanEvent.sequence)
}
canSampled () {
return this.sampling
}
close () {
this.storage.flush()
this.callStack = []
}
}
module.exports = AsyncTrace