forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspan-chunk.js
75 lines (61 loc) · 2.59 KB
/
span-chunk.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const spanMessages = require('../data/grpc/Span_pb')
const {convertTransactionId, convertLocalAsyncId} = require('../data/grpc-data-convertor')
const SpanEvents = require('./span-events')
class SpanChunk {
constructor (traceId, agentInfo, asyncId, spanEventList) {
if (!traceId || !agentInfo) {
throw new Error('Can not initialize SpanChunk', traceId, agentInfo)
}
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.transactionIdObject = traceId.transactionId
this.spanEventList = spanEventList
this.endPoint = null // host, domain
this.applicationServiceType = agentInfo.serviceType // from config
this.localAsyncId = asyncId
}
static getFactoryMethod (agentInfo, traceId) {
return (spanEventList) => new SpanChunk(agentInfo, traceId, null, spanEventList)
}
static getAsyncFactoryMethod (agentInfo, traceId, asyncId) {
return (spanEventList) => new SpanChunk(agentInfo, traceId, asyncId, spanEventList)
}
get spanMessage() {
const pSpanMessage = new spanMessages.PSpanMessage()
const pSpanChunk = new spanMessages.PSpanChunk()
pSpanChunk.setVersion(1);
const pTransactionId = convertTransactionId(this.transactionIdObject)
pSpanChunk.setTransactionid(pTransactionId);
pSpanChunk.setSpanid(this.spanId)
pSpanChunk.setEndpoint(this.endPoint)
pSpanChunk.setApplicationservicetype(this.applicationServiceType)
pSpanChunk.setLocalasyncid(convertLocalAsyncId(this.localAsyncId))
const keyTime = this.keyTime
pSpanChunk.setKeytime(keyTime)
const spanEvents = new SpanEvents(this.spanEventList)
const pSpanEvents = spanEvents.getpSpanEvents(keyTime)
if (pSpanEvents.length > 0) {
pSpanEvents.forEach(pSpanEvent => pSpanChunk.addSpanevent(pSpanEvent))
}
pSpanMessage.setSpanchunk(pSpanChunk)
return pSpanMessage
}
get keyTime() {
const minStartTimeSpanEvent = this.spanEventList.reduce((minStartTimeSpanEvent, currentSpanEvent) => minStartTimeSpanEvent.startTime < currentSpanEvent.startTime ? minStartTimeSpanEvent : currentSpanEvent)
if (!minStartTimeSpanEvent) {
return 0;
}
return minStartTimeSpanEvent.startTime
}
}
module.exports = SpanChunk