Skip to content

Commit

Permalink
[#233] Support SpanChunk and AsyncSpanChunk
Browse files Browse the repository at this point in the history
* Implement TraceRoot class for tracing functionality
* Support SpanChunk and AsyncSpanChunk
* Support ChildTrace
* Fix node@16 test failure
  • Loading branch information
feelform committed Dec 8, 2024
1 parent 63736d7 commit ccdc0ec
Show file tree
Hide file tree
Showing 83 changed files with 1,866 additions and 1,590 deletions.
17 changes: 15 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Agent {
}

createTraceObject(requestData) {
return this.traceContext.makeTrace(requestData)
return this.traceContext.newTraceObject2()
}

currentTraceObject() {
Expand All @@ -79,7 +79,15 @@ class Agent {
}

createAgentInfo(config, agentStartTime) {
return AgentInfo.create(config, agentStartTime)
return AgentInfo.create(config, '' + agentStartTime)
}

getAgentInfo() {
return this.agentInfo
}

getTraceContext() {
return this.traceContext
}

startSchedule(agentId, agentStartTime) {
Expand All @@ -91,6 +99,11 @@ class Agent {
}
this.pingScheduler = new PinScheduler(this.dataSender)
}

shutdown() {
this.mainScheduler?.stop()
this.pingScheduler?.stop()
}
}

module.exports = Agent
8 changes: 1 addition & 7 deletions lib/client/data-sender-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
const GrpcDataSender = require('./grpc-data-sender')
const DataSender = require('./data-sender')

let dataSender
const create = (config, agentInfo) => {
dataSender = new DataSender(config, new GrpcDataSender(
const dataSender = new DataSender(config, new GrpcDataSender(
config.collectorIp,
config.collectorTcpPort,
config.collectorStatPort,
Expand All @@ -22,11 +21,6 @@ const create = (config, agentInfo) => {
return dataSender
}

const setDataSender = (sender) => {
dataSender = sender
}

module.exports = {
create,
setDataSender
}
7 changes: 6 additions & 1 deletion lib/client/data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const SqlUidMetaData = require('./sql-uid-meta-data')

class DataSender {
constructor(config, dataSender) {
this.config = config
this.enabledDataSending = config.enabledDataSending
this.dataSender = dataSender
}
Expand Down Expand Up @@ -46,8 +47,12 @@ class DataSender {
this.dataSender.sendStringMetaInfo(data)
} else if (data instanceof Span) {
this.dataSender.sendSpan(data)
} else if (data?.isSpan?.()) {
this.dataSender.sendSpan(data)
} else if (data instanceof SpanChunk) {
this.dataSender.sendSpanChunk(data)
} else if (data?.isAsyncSpanChunk?.()) {
this.dataSender.sendSpanChunk(data)
} else if (data instanceof SqlMetaData) {
this.dataSender.sendSqlMetaInfo(data)
} else if (data instanceof SqlUidMetaData) {
Expand Down Expand Up @@ -75,7 +80,7 @@ class DataSender {

// TODO Remove it after implementing grpc
sendActiveThreadCountRes() {

}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/client/grpc-data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class GrpcDataSender {

sendSpan(span) {
try {
const pSpan = span.spanMessage
const pSpan = span.toProtocolBuffer()
if (log.isDebug()) {
log.debug(`sendSpan pSpan: ${pSpan}`)
}
Expand All @@ -330,7 +330,7 @@ class GrpcDataSender {

sendSpanChunk(spanChunk) {
try {
const pSpanChunk = spanChunk.spanMessage
const pSpanChunk = spanChunk.toProtocolBuffer()
if (log.isDebug()) {
log.debug(`sendSpanChunk spanChunk: ${JSON.stringify(spanChunk)}`)
}
Expand Down
14 changes: 13 additions & 1 deletion lib/context/async-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'use strict'

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

// ref: DefaultAsyncId.java
class AsyncId {
Expand Down Expand Up @@ -43,11 +44,22 @@ class AsyncId {

sequenceNextLocalAsyncId() {
if (this === AsyncId.nonAsyncId) {
return this.nextLocalAsyncId()
return this.nextLocalAsyncId()
}

return new AsyncId(this.asyncId, this.sequence + 1)
}

toProtocolBuffer() {
const pLocalAsyncId = new spanMessages.PLocalAsyncId()
pLocalAsyncId.setAsyncid(this.getAsyncId())
pLocalAsyncId.setSequence(this.getSequence())
return pLocalAsyncId
}

tosString() {
return `AsyncId(asyncId=${this.asyncId}, sequence=${this.sequence})`
}
}

module.exports = AsyncId
5 changes: 4 additions & 1 deletion lib/context/disable-span-event-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

'use strict'

const disableAsyncId = require('./trace/disable-async-id')

class DisableSpanEventRecorder {
constructor() {
this.nextAsyncId = null
Expand Down Expand Up @@ -59,7 +61,8 @@ class DisableSpanEventRecorder {
this.ended = true
}

getLocalAsyncId() {
getNextAsyncId() {
return disableAsyncId
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/context/disable-span-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ class DisableSpanRecorder {

recordEndPoint() {}

recordRemoteAddr() {}
recordRemoteAddress() {}

recordException() {}

recordAcceptorHost() {}

recordParentApplication() {}
}

module.exports = DisableSpanRecorder
14 changes: 6 additions & 8 deletions lib/context/disable-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ class DisableTrace {
this.spanRecorder = new DisableSpanRecorder()
}

getTraceRoot() {
return this.traceRoot
}

traceBlockBegin() {
return new DisableSpanEventRecorder()
}

traceBlockEnd() {}

completeSpanEvent() {
getSpanRecorder() {
return this.spanRecorder
}

canSampled() {
return false
getTraceRoot() {
return this.traceRoot
}

getStartTime() {
return 0
canSampled() {
return false
}

close() {
Expand Down
22 changes: 11 additions & 11 deletions lib/context/remote-trace-root-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

'use strict'

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

class RemoteTraceRoot {
constructor(traceId, traceRoot) {
class RemoteTraceRoot extends TraceRoot {
constructor(traceId, agentId, transactionId) {
super(agentId, Date.now(), transactionId)
this.traceId = traceId
this.traceRoot = traceRoot
}

getTraceStartTime() {
return this.traceRoot.getTraceStartTime()
}

isSampled() {
Expand All @@ -26,6 +22,10 @@ class RemoteTraceRoot {
getTraceId() {
return this.traceId
}

toString() {
return `RemoteTraceRoot{traceId=${this.traceId.toString()}, ${super.toString()}}`
}
}

class RemoteTraceRootBuilder {
Expand All @@ -46,11 +46,11 @@ class RemoteTraceRootBuilder {
build() {
const agentId = this.agentInfo.getAgentId()
if (this.isNewTraceRoot()) {
const traceId = new TraceIdBuilder(this.agentInfo, this.transactionId).build()
return new RemoteTraceRoot(traceId, new TraceRootBuilder(agentId, this.transactionId).build())
const traceId = new TraceIdBuilder(this.agentInfo.getAgentId(), this.agentInfo.getAgentStartTime(), this.transactionId).build()
return new RemoteTraceRoot(traceId, agentId, this.transactionId)
}

return new RemoteTraceRoot(this.traceId, new TraceRootBuilder(agentId, this.transactionId).build())
return new RemoteTraceRoot(this.traceId, agentId, this.transactionId)
}

isNewTraceRoot() {
Expand Down
82 changes: 81 additions & 1 deletion lib/context/span-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

'use strict'

const spanMessages = require('../data/v1/Span_pb')
class Span {
constructor(traceRoot) {
this.traceRoot = traceRoot
Expand All @@ -14,6 +15,57 @@ class Span {
isSpan() {
return true
}

toProtocolBuffer() {
const pSpanMessage = new spanMessages.PSpanMessage()
const pSpan = new spanMessages.PSpan()
pSpan.setVersion(1)

const pTransactionId = this.traceRoot.getTraceId().toProtocolBuffer()
pSpan.setTransactionid(pTransactionId)

pSpan.setSpanid(this.traceRoot.getTraceId().getSpanId())
pSpan.setParentspanid(this.traceRoot.getTraceId().getParentSpanId())
pSpan.setStarttime(this.startTime)
pSpan.setElapsed(this.elapsedTime)
pSpan.setApiid(this.apiId)
pSpan.setServicetype(this.serviceType)

const pAcceptEvent = new spanMessages.PAcceptEvent()
pAcceptEvent.setRpc(this.rpc)
pAcceptEvent.setEndpoint(this.endPoint ?? 'UNKNOWN')
pAcceptEvent.setRemoteaddr(this.remoteAddress ?? 'UNKNOWN')

const pParentInfo = new spanMessages.PParentInfo()
if (this.parentApplicationType) {
pParentInfo.setParentapplicationtype(this.parentApplicationType)
}
if (this.parentApplicationName) {
pParentInfo.setParentapplicationname(this.parentApplicationName)
}
if (this.acceptorHost) {
pParentInfo.setAcceptorhost(this.acceptorHost)
}
pAcceptEvent.setParentinfo(pParentInfo)
pSpan.setAcceptevent(pAcceptEvent)

pSpan.setFlag(this.traceRoot.getTraceId().getFlags())

if (this.traceRoot.hasErrorCode()) {
pSpan.setErr(this.traceRoot.getShared().getErrorCode())
}

if (this.applicationServiceType) {
pSpan.setApplicationservicetype(this.applicationServiceType)
}
// TODO: SpanMessageMapperImpl.java: spanTraceRootSharedLoggingInfo loggingTransactionInfo

this.spanEventList.forEach(spanEvent => pSpan.addSpanevent(spanEvent.toProtocolBuffer()))
this.annotations.forEach(annotation => pSpan.addAnnotation(annotation.pAnnotation()))

pSpanMessage.setSpan(pSpan)
return pSpanMessage
}
}

class SpanBuilder {
Expand All @@ -23,6 +75,15 @@ class SpanBuilder {
this.startTime = traceRoot.getTraceStartTime()
}

setServiceType(serviceType) {
this.serviceType = serviceType
return this
}

getStartTime() {
return this.startTime
}

setApiId(apiId) {
this.apiId = apiId
return this
Expand Down Expand Up @@ -72,9 +133,26 @@ class SpanBuilder {
return this.traceRoot
}

setSpanEventList(spanEventList) {
this.spanEventList = spanEventList
return this
}

setApplicationServiceType(applicationServiceType) {
this.applicationServiceType = applicationServiceType
return this
}

markAfterTime() {
this.elapsedTime = Date.now() - this.startTime
return this
}

build() {
const span = new Span(this.traceRoot)
this.startTime = this.startTime || Date.now()
span.serviceType = this.serviceType
span.startTime = this.startTime || Date.now()
span.elapsedTime = this.elapsedTime
span.apiId = this.apiId
span.rpc = this.rpc
span.endPoint = this.endPoint
Expand All @@ -84,6 +162,8 @@ class SpanBuilder {
span.acceptorHost = this.acceptorHost
span.parentApplicationName = this.parentApplicationName
span.parentApplicationType = this.parentApplicationType
span.spanEventList = this.spanEventList ?? []
span.applicationServiceType = this.applicationServiceType
return span
}
}
Expand Down
Loading

0 comments on commit ccdc0ec

Please sign in to comment.