Skip to content

Commit 6ae03e3

Browse files
committed
[#231] Support ChildTrace
1 parent f45f6ae commit 6ae03e3

File tree

7 files changed

+94
-11
lines changed

7 files changed

+94
-11
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ class RemoteTraceRootBuilder {
3535
}
3636

3737
build(transactionId) {
38-
if (this.traceId) {
39-
return new RemoteTraceRoot(this.traceId, new TraceRootBuilder(this.agentInfo.agentId).build(transactionId))
38+
const agentId = this.agentInfo.getAgentId()
39+
if (this.isNewTraceRoot()) {
40+
const traceId = new TraceIdBuilder(this.agentInfo, transactionId).build()
41+
return new RemoteTraceRoot(traceId, new TraceRootBuilder(agentId, transactionId).build())
4042
}
4143

42-
const traceId = new TraceIdBuilder(this.agentInfo, transactionId).build()
43-
return new RemoteTraceRoot(traceId, new TraceRootBuilder(this.agentInfo.agentId).build(transactionId))
44+
return new RemoteTraceRoot(this.traceId, new TraceRootBuilder(agentId, transactionId).build())
45+
}
46+
47+
isNewTraceRoot() {
48+
return !this.traceId
4449
}
4550
}
4651

lib/context/span-id.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// SpanId.java in Java agent
1010
class SpanId {
11-
static nullSpanId = -1
11+
static nullSpanId = '-1'
1212

1313
constructor () {
1414
this.MAX_NUM = Number.MAX_SAFE_INTEGER

lib/context/trace-root-builder.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ class TraceRoot {
2323
}
2424

2525
class TraceRootBuilder {
26-
constructor(agentId) {
26+
constructor(agentId, localTransactionId) {
2727
this.agentId = agentId
28+
this.localTransactionId = localTransactionId
29+
this.traceStartTime = Date.now()
2830
}
2931

30-
build(transactionId) {
31-
return new TraceRoot(this.agentId, Date.now(), transactionId)
32+
// DefaultTraceRootFactory.java: newDisableTraceRoot
33+
build() {
34+
return new TraceRoot(this.agentId, this.traceStartTime, this.localTransactionId)
3235
}
3336
}
3437

lib/context/trace/child-trace-builder.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const SpanEventRecorder = require('./span-event-recorder')
1010
const Trace = require('./trace2')
11+
const TraceRootSpanRecorder = require('./trace-root-span-recorder')
1112

1213
class ChildTrace extends Trace {
1314
constructor(spanBuilder, repository, localAsyncId) {
@@ -17,8 +18,10 @@ class ChildTrace extends Trace {
1718
}
1819

1920
class ChildTraceBuilder {
20-
constructor(traceRoot, localAsyncId) {
21+
constructor(traceRoot, repository, localAsyncId) {
2122
this.traceRoot = traceRoot
23+
this.repository = repository
24+
this.spanRecorder = new TraceRootSpanRecorder(traceRoot)
2225
this.localAsyncId = localAsyncId
2326

2427
this.callStack = []
@@ -34,6 +37,7 @@ class ChildTraceBuilder {
3437
}
3538

3639
const spanEventRecorder = this.spanEventRecorder.makeSequenceAndDepthGrowth()
40+
this.spanEventRecorder = spanEventRecorder
3741
this.callStack.push(spanEventRecorder.getSpanEventBuilder())
3842

3943
return spanEventRecorder

lib/context/trace/trace-id-builder.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,69 @@
66

77
'use strict'
88

9+
const spanId = require('../span-id')
10+
11+
// DefaultTraceId.java
912
class TraceId {
10-
constructor(agentId, agentStartTime, transactionId) {
13+
constructor(agentId, agentStartTime, transactionId, parentSpanId, spanId, flags) {
1114
this.agentId = agentId
1215
this.agentStartTime = agentStartTime
1316
this.transactionId = transactionId
17+
this.parentSpanId = parentSpanId
18+
this.spanId = spanId
19+
this.flags = flags
20+
}
21+
22+
getAgentId() {
23+
return this.agentId
24+
}
25+
26+
getAgentStartTime() {
27+
return this.agentStartTime
28+
}
29+
30+
getTransactionId() {
31+
return this.transactionId
32+
}
33+
34+
getParentSpanId() {
35+
return this.parentSpanId
36+
}
37+
38+
getSpanId() {
39+
return this.spanId
40+
}
41+
42+
isRoot() {
43+
return this.parentSpanId === spanId.nullSpanId
44+
}
45+
46+
toString() {
47+
return `TraceId(agentId=${this.agentId}, agentStartTime=${this.agentStartTime}, transactionId=${this.transactionId}, parentSpanId=${this.parentSpanId}
48+
, spanId=${this.spanId}, flags=${this.flags})`
1449
}
1550
}
1651

1752
class TraceIdBuilder {
1853
constructor(agentInfo, transactionId) {
1954
this.agentInfo = agentInfo
2055
this.transactionId = transactionId
56+
this.parentSpanId = spanId.nullSpanId
57+
}
58+
59+
setSpanId(spanId) {
60+
this.spanId = spanId
61+
return this
62+
}
63+
64+
setParentSpanId(parentSpanId) {
65+
this.parentSpanId = parentSpanId
66+
return this
2167
}
2268

2369
build() {
24-
return new TraceId(this.agentInfo.agentId, this.agentInfo.agentStartTime, this.transactionId)
70+
const spanId = this.spanId ?? spanId.newSpanId()
71+
return new TraceId(this.agentInfo.agentId, this.agentInfo.agentStartTime, this.transactionId, this.parentSpanId, spanId, 0)
2572
}
2673
}
2774

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Pinpoint Node.js Agent
3+
* Copyright 2020-present NAVER Corp.
4+
* Apache License v2.0
5+
*/
6+
7+
'use strict'
8+
9+
class TraceRootSpanRecorder {
10+
constructor(traceRoot) {
11+
this.traceRoot = traceRoot
12+
}
13+
14+
canSampled() {
15+
return true
16+
}
17+
18+
isRoot() {
19+
return this.traceRoot.getTraceId().isRoot()
20+
}
21+
}
22+
23+
module.exports = TraceRootSpanRecorder

lib/context/trace/trace2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Trace {
3737

3838
// GrpcSpanProcessorV2: postProcess
3939
const spanEventRecorder = this.spanEventRecorder.makeSequenceAndDepthGrowth()
40+
this.spanEventRecorder = spanEventRecorder
4041
this.callStack.push(spanEventRecorder.getSpanEventBuilder())
4142

4243
return spanEventRecorder

0 commit comments

Comments
 (0)