forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.js
91 lines (79 loc) · 2.88 KB
/
span.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const { convertTransactionId, convertAcceptEvent, convertIntStringValue, addAnnotations } = require('../data/grpc-data-convertor')
const spanMessages = require('../data/v1/Span_pb')
const SpanEvents = require('./span-events')
class Span {
constructor(traceId, agentInfo, requestData = {}) {
if (!traceId || !agentInfo) {
throw new Error('Can not initialize Span', traceId, agentInfo)
}
this.traceId = traceId
this.agentId = agentInfo.agentId // required, from config
this.applicationName = agentInfo.applicationName // required, from config
this.agentStartTime = agentInfo.agentStartTime // required, from config
this.serviceType = agentInfo.serviceType // required
this.spanId = traceId.spanId // required
this.parentSpanId = traceId.parentSpanId
this.startTime = Date.now() // required
this.elapsedTime = 0
this.rpc = null // uri
this.endPoint = null // host, domain
this.remoteAddr = null // ip
this.annotations = []
this.flag = traceId.flag
this.err = null
this.spanEventList = []
this.apiId = null
this.exceptionInfo = null
this.applicationServiceType = agentInfo.serviceType // from config
this.loggingTransactionInfo = null // ?
this.version = 1
this.acceptorHost = requestData.host
this.parentApplicationName = requestData.parentApplicationName
this.parentApplicationType = requestData.parentApplicationType
}
markElapsedTime() {
if (this.startTime) {
this.elapsedTime = Date.now() - this.startTime
}
}
get elapsed() {
return this.elapsedTime
}
get spanMessage() {
const pAcceptEvent = convertAcceptEvent(this)
const pSpanMessage = new spanMessages.PSpanMessage()
const pSpan = new spanMessages.PSpan()
pSpan.setVersion(1)
if (this.traceId) {
const pTransactionId = convertTransactionId(this.traceId.transactionId)
pSpan.setTransactionid(pTransactionId)
}
pSpan.setSpanid(this.spanId)
pSpan.setParentspanid(this.parentSpanId)
pSpan.setStarttime(this.startTime)
pSpan.setElapsed(this.elapsedTime)
pSpan.setApiid(this.apiId)
pSpan.setServicetype(this.serviceType)
pSpan.setAcceptevent(pAcceptEvent)
pSpan.setFlag(this.flag)
pSpan.setErr(this.err)
pSpan.setExceptioninfo(convertIntStringValue(this.exceptionInfo))
pSpan.setApplicationservicetype(this.applicationServiceType)
pSpan.setLoggingtransactioninfo(this.loggingTransactionInfo)
const spanEvents = new SpanEvents(this.spanEventList)
const pSpanEvents = spanEvents.getpSpanEvents(this.startTime)
if (pSpanEvents.length > 0) {
pSpanEvents.forEach(pSpanEvent => pSpan.addSpanevent(pSpanEvent))
}
addAnnotations(pSpan, this.annotations)
pSpanMessage.setSpan(pSpan)
return pSpanMessage
}
}
module.exports = Span