Skip to content

Commit ccdc0ec

Browse files
committed
[#233] Support SpanChunk and AsyncSpanChunk
* Implement TraceRoot class for tracing functionality * Support SpanChunk and AsyncSpanChunk * Support ChildTrace * Fix node@16 test failure
1 parent 63736d7 commit ccdc0ec

File tree

83 files changed

+1866
-1590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1866
-1590
lines changed

lib/agent.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Agent {
6767
}
6868

6969
createTraceObject(requestData) {
70-
return this.traceContext.makeTrace(requestData)
70+
return this.traceContext.newTraceObject2()
7171
}
7272

7373
currentTraceObject() {
@@ -79,7 +79,15 @@ class Agent {
7979
}
8080

8181
createAgentInfo(config, agentStartTime) {
82-
return AgentInfo.create(config, agentStartTime)
82+
return AgentInfo.create(config, '' + agentStartTime)
83+
}
84+
85+
getAgentInfo() {
86+
return this.agentInfo
87+
}
88+
89+
getTraceContext() {
90+
return this.traceContext
8391
}
8492

8593
startSchedule(agentId, agentStartTime) {
@@ -91,6 +99,11 @@ class Agent {
9199
}
92100
this.pingScheduler = new PinScheduler(this.dataSender)
93101
}
102+
103+
shutdown() {
104+
this.mainScheduler?.stop()
105+
this.pingScheduler?.stop()
106+
}
94107
}
95108

96109
module.exports = Agent

lib/client/data-sender-factory.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
const GrpcDataSender = require('./grpc-data-sender')
1010
const DataSender = require('./data-sender')
1111

12-
let dataSender
1312
const create = (config, agentInfo) => {
14-
dataSender = new DataSender(config, new GrpcDataSender(
13+
const dataSender = new DataSender(config, new GrpcDataSender(
1514
config.collectorIp,
1615
config.collectorTcpPort,
1716
config.collectorStatPort,
@@ -22,11 +21,6 @@ const create = (config, agentInfo) => {
2221
return dataSender
2322
}
2423

25-
const setDataSender = (sender) => {
26-
dataSender = sender
27-
}
28-
2924
module.exports = {
3025
create,
31-
setDataSender
3226
}

lib/client/data-sender.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SqlUidMetaData = require('./sql-uid-meta-data')
1717

1818
class DataSender {
1919
constructor(config, dataSender) {
20+
this.config = config
2021
this.enabledDataSending = config.enabledDataSending
2122
this.dataSender = dataSender
2223
}
@@ -46,8 +47,12 @@ class DataSender {
4647
this.dataSender.sendStringMetaInfo(data)
4748
} else if (data instanceof Span) {
4849
this.dataSender.sendSpan(data)
50+
} else if (data?.isSpan?.()) {
51+
this.dataSender.sendSpan(data)
4952
} else if (data instanceof SpanChunk) {
5053
this.dataSender.sendSpanChunk(data)
54+
} else if (data?.isAsyncSpanChunk?.()) {
55+
this.dataSender.sendSpanChunk(data)
5156
} else if (data instanceof SqlMetaData) {
5257
this.dataSender.sendSqlMetaInfo(data)
5358
} else if (data instanceof SqlUidMetaData) {
@@ -75,7 +80,7 @@ class DataSender {
7580

7681
// TODO Remove it after implementing grpc
7782
sendActiveThreadCountRes() {
78-
83+
7984
}
8085
}
8186

lib/client/grpc-data-sender.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class GrpcDataSender {
316316

317317
sendSpan(span) {
318318
try {
319-
const pSpan = span.spanMessage
319+
const pSpan = span.toProtocolBuffer()
320320
if (log.isDebug()) {
321321
log.debug(`sendSpan pSpan: ${pSpan}`)
322322
}
@@ -330,7 +330,7 @@ class GrpcDataSender {
330330

331331
sendSpanChunk(spanChunk) {
332332
try {
333-
const pSpanChunk = spanChunk.spanMessage
333+
const pSpanChunk = spanChunk.toProtocolBuffer()
334334
if (log.isDebug()) {
335335
log.debug(`sendSpanChunk spanChunk: ${JSON.stringify(spanChunk)}`)
336336
}

lib/context/async-id.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'use strict'
88

99
const SequenceGenerator = require('./sequence-generator')
10+
const spanMessages = require('../data/v1/Span_pb')
1011

1112
// ref: DefaultAsyncId.java
1213
class AsyncId {
@@ -43,11 +44,22 @@ class AsyncId {
4344

4445
sequenceNextLocalAsyncId() {
4546
if (this === AsyncId.nonAsyncId) {
46-
return this.nextLocalAsyncId()
47+
return this.nextLocalAsyncId()
4748
}
4849

4950
return new AsyncId(this.asyncId, this.sequence + 1)
5051
}
52+
53+
toProtocolBuffer() {
54+
const pLocalAsyncId = new spanMessages.PLocalAsyncId()
55+
pLocalAsyncId.setAsyncid(this.getAsyncId())
56+
pLocalAsyncId.setSequence(this.getSequence())
57+
return pLocalAsyncId
58+
}
59+
60+
tosString() {
61+
return `AsyncId(asyncId=${this.asyncId}, sequence=${this.sequence})`
62+
}
5163
}
5264

5365
module.exports = AsyncId

lib/context/disable-span-event-recorder.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict'
88

9+
const disableAsyncId = require('./trace/disable-async-id')
10+
911
class DisableSpanEventRecorder {
1012
constructor() {
1113
this.nextAsyncId = null
@@ -59,7 +61,8 @@ class DisableSpanEventRecorder {
5961
this.ended = true
6062
}
6163

62-
getLocalAsyncId() {
64+
getNextAsyncId() {
65+
return disableAsyncId
6366
}
6467
}
6568

lib/context/disable-span-recorder.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class DisableSpanRecorder {
2121

2222
recordEndPoint() {}
2323

24-
recordRemoteAddr() {}
24+
recordRemoteAddress() {}
2525

2626
recordException() {}
27+
28+
recordAcceptorHost() {}
29+
30+
recordParentApplication() {}
2731
}
2832

2933
module.exports = DisableSpanRecorder

lib/context/disable-trace.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@ class DisableTrace {
1616
this.spanRecorder = new DisableSpanRecorder()
1717
}
1818

19-
getTraceRoot() {
20-
return this.traceRoot
21-
}
2219

2320
traceBlockBegin() {
2421
return new DisableSpanEventRecorder()
2522
}
2623

2724
traceBlockEnd() {}
2825

29-
completeSpanEvent() {
26+
getSpanRecorder() {
27+
return this.spanRecorder
3028
}
3129

32-
canSampled() {
33-
return false
30+
getTraceRoot() {
31+
return this.traceRoot
3432
}
3533

36-
getStartTime() {
37-
return 0
34+
canSampled() {
35+
return false
3836
}
3937

4038
close() {

lib/context/remote-trace-root-builder.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66

77
'use strict'
88

9-
const TraceRootBuilder = require('./trace-root-builder')
9+
const TraceRoot = require('./trace/trace-root')
1010
const TraceIdBuilder = require('./trace/trace-id-builder')
1111

12-
class RemoteTraceRoot {
13-
constructor(traceId, traceRoot) {
12+
class RemoteTraceRoot extends TraceRoot {
13+
constructor(traceId, agentId, transactionId) {
14+
super(agentId, Date.now(), transactionId)
1415
this.traceId = traceId
15-
this.traceRoot = traceRoot
16-
}
17-
18-
getTraceStartTime() {
19-
return this.traceRoot.getTraceStartTime()
2016
}
2117

2218
isSampled() {
@@ -26,6 +22,10 @@ class RemoteTraceRoot {
2622
getTraceId() {
2723
return this.traceId
2824
}
25+
26+
toString() {
27+
return `RemoteTraceRoot{traceId=${this.traceId.toString()}, ${super.toString()}}`
28+
}
2929
}
3030

3131
class RemoteTraceRootBuilder {
@@ -46,11 +46,11 @@ class RemoteTraceRootBuilder {
4646
build() {
4747
const agentId = this.agentInfo.getAgentId()
4848
if (this.isNewTraceRoot()) {
49-
const traceId = new TraceIdBuilder(this.agentInfo, this.transactionId).build()
50-
return new RemoteTraceRoot(traceId, new TraceRootBuilder(agentId, this.transactionId).build())
49+
const traceId = new TraceIdBuilder(this.agentInfo.getAgentId(), this.agentInfo.getAgentStartTime(), this.transactionId).build()
50+
return new RemoteTraceRoot(traceId, agentId, this.transactionId)
5151
}
5252

53-
return new RemoteTraceRoot(this.traceId, new TraceRootBuilder(agentId, this.transactionId).build())
53+
return new RemoteTraceRoot(this.traceId, agentId, this.transactionId)
5454
}
5555

5656
isNewTraceRoot() {

lib/context/span-builder.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
'use strict'
88

9+
const spanMessages = require('../data/v1/Span_pb')
910
class Span {
1011
constructor(traceRoot) {
1112
this.traceRoot = traceRoot
@@ -14,6 +15,57 @@ class Span {
1415
isSpan() {
1516
return true
1617
}
18+
19+
toProtocolBuffer() {
20+
const pSpanMessage = new spanMessages.PSpanMessage()
21+
const pSpan = new spanMessages.PSpan()
22+
pSpan.setVersion(1)
23+
24+
const pTransactionId = this.traceRoot.getTraceId().toProtocolBuffer()
25+
pSpan.setTransactionid(pTransactionId)
26+
27+
pSpan.setSpanid(this.traceRoot.getTraceId().getSpanId())
28+
pSpan.setParentspanid(this.traceRoot.getTraceId().getParentSpanId())
29+
pSpan.setStarttime(this.startTime)
30+
pSpan.setElapsed(this.elapsedTime)
31+
pSpan.setApiid(this.apiId)
32+
pSpan.setServicetype(this.serviceType)
33+
34+
const pAcceptEvent = new spanMessages.PAcceptEvent()
35+
pAcceptEvent.setRpc(this.rpc)
36+
pAcceptEvent.setEndpoint(this.endPoint ?? 'UNKNOWN')
37+
pAcceptEvent.setRemoteaddr(this.remoteAddress ?? 'UNKNOWN')
38+
39+
const pParentInfo = new spanMessages.PParentInfo()
40+
if (this.parentApplicationType) {
41+
pParentInfo.setParentapplicationtype(this.parentApplicationType)
42+
}
43+
if (this.parentApplicationName) {
44+
pParentInfo.setParentapplicationname(this.parentApplicationName)
45+
}
46+
if (this.acceptorHost) {
47+
pParentInfo.setAcceptorhost(this.acceptorHost)
48+
}
49+
pAcceptEvent.setParentinfo(pParentInfo)
50+
pSpan.setAcceptevent(pAcceptEvent)
51+
52+
pSpan.setFlag(this.traceRoot.getTraceId().getFlags())
53+
54+
if (this.traceRoot.hasErrorCode()) {
55+
pSpan.setErr(this.traceRoot.getShared().getErrorCode())
56+
}
57+
58+
if (this.applicationServiceType) {
59+
pSpan.setApplicationservicetype(this.applicationServiceType)
60+
}
61+
// TODO: SpanMessageMapperImpl.java: spanTraceRootSharedLoggingInfo loggingTransactionInfo
62+
63+
this.spanEventList.forEach(spanEvent => pSpan.addSpanevent(spanEvent.toProtocolBuffer()))
64+
this.annotations.forEach(annotation => pSpan.addAnnotation(annotation.pAnnotation()))
65+
66+
pSpanMessage.setSpan(pSpan)
67+
return pSpanMessage
68+
}
1769
}
1870

1971
class SpanBuilder {
@@ -23,6 +75,15 @@ class SpanBuilder {
2375
this.startTime = traceRoot.getTraceStartTime()
2476
}
2577

78+
setServiceType(serviceType) {
79+
this.serviceType = serviceType
80+
return this
81+
}
82+
83+
getStartTime() {
84+
return this.startTime
85+
}
86+
2687
setApiId(apiId) {
2788
this.apiId = apiId
2889
return this
@@ -72,9 +133,26 @@ class SpanBuilder {
72133
return this.traceRoot
73134
}
74135

136+
setSpanEventList(spanEventList) {
137+
this.spanEventList = spanEventList
138+
return this
139+
}
140+
141+
setApplicationServiceType(applicationServiceType) {
142+
this.applicationServiceType = applicationServiceType
143+
return this
144+
}
145+
146+
markAfterTime() {
147+
this.elapsedTime = Date.now() - this.startTime
148+
return this
149+
}
150+
75151
build() {
76152
const span = new Span(this.traceRoot)
77-
this.startTime = this.startTime || Date.now()
153+
span.serviceType = this.serviceType
154+
span.startTime = this.startTime || Date.now()
155+
span.elapsedTime = this.elapsedTime
78156
span.apiId = this.apiId
79157
span.rpc = this.rpc
80158
span.endPoint = this.endPoint
@@ -84,6 +162,8 @@ class SpanBuilder {
84162
span.acceptorHost = this.acceptorHost
85163
span.parentApplicationName = this.parentApplicationName
86164
span.parentApplicationType = this.parentApplicationType
165+
span.spanEventList = this.spanEventList ?? []
166+
span.applicationServiceType = this.applicationServiceType
87167
return span
88168
}
89169
}

0 commit comments

Comments
 (0)