Skip to content

Commit 574d260

Browse files
committed
[#231] Support ChildTrace
1 parent 46e862e commit 574d260

File tree

9 files changed

+42
-13
lines changed

9 files changed

+42
-13
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class RemoteTraceRoot {
2222
isSampled() {
2323
return true
2424
}
25+
26+
getTraceId() {
27+
return this.traceId
28+
}
2529
}
2630

2731
class RemoteTraceRootBuilder {

lib/context/span-builder.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class SpanBuilder {
6464
return this
6565
}
6666

67+
getTraceRoot() {
68+
return this.traceRoot
69+
}
70+
6771
build() {
6872
const span = new Span(this.traceRoot)
6973
this.startTime = this.startTime || Date.now()

lib/context/trace-context.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class TraceContext {
3535
this.traceSampler = new TraceSampler(agentInfo, config)
3636
}
3737

38+
getAgentInfo() {
39+
return this.agentInfo
40+
}
41+
3842
continueTraceObject(requestData) {
3943
const traceId = new TraceId(
4044
requestData.transactionId,
@@ -127,7 +131,7 @@ class TraceContext {
127131
const spanBuilder = new SpanBuilder(traceRoot)
128132
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
129133
const repository = new SpanRepository(spanChunkBuilder, this.dataSender)
130-
return new Trace2(spanBuilder, repository, this.agentInfo.getServiceType())
134+
return new Trace2(spanBuilder, repository)
131135
}
132136

133137
// DefaultAsyncContext.java: newAsyncContextTrace

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const SpanEventRecorder = require('./span-event-recorder2')
1010
const Trace = require('./trace2')
1111
const TraceRootSpanRecorder = require('./trace-root-span-recorder')
1212
const StackId = require('./stack-id')
13+
const CallStack = require('./call-stack')
1314

1415
class ChildTrace extends Trace {
1516
constructor(spanBuilder, repository, localAsyncId) {
@@ -25,7 +26,7 @@ class ChildTraceBuilder {
2526
this.spanRecorder = new TraceRootSpanRecorder(traceRoot)
2627
this.localAsyncId = localAsyncId
2728

28-
this.callStack = []
29+
this.callStack = new CallStack()
2930
this.closed = false
3031

3132
this.traceBlockBegin(StackId.asyncBeginStackId)
@@ -48,6 +49,14 @@ class ChildTraceBuilder {
4849
spanEventBuilder.markAfterTime()
4950
this.repository.storeSpanEvent(spanEventBuilder)
5051
}
52+
53+
getTraceRoot() {
54+
return this.traceRoot
55+
}
56+
57+
getTraceId() {
58+
return this.traceRoot.getTraceId()
59+
}
5160
}
5261

5362
module.exports = ChildTraceBuilder

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class TraceId {
4040
return this.spanId
4141
}
4242

43+
getFlags() {
44+
return this.flags
45+
}
46+
4347
isRoot() {
4448
return this.parentSpanId == spanId.nullSpanId()
4549
}

lib/context/trace/trace2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const StackId = require('./stack-id')
1414
class Trace {
1515
/**
1616
* Creates an instance of the Trace class.
17-
*
17+
*
1818
* @constructor
1919
* @param {SpanBuilder} spanBuilder - The builder for creating spans.
2020
* @param {Repository} repository - The repository for storing trace data.
@@ -43,7 +43,7 @@ class Trace {
4343
return
4444
}
4545

46-
const spanEventBuilder = this.callStack.pop()
46+
const spanEventBuilder = this.callStack.pop()
4747
spanEventBuilder.markAfterTime()
4848
this.repository.storeSpanEvent(spanEventBuilder)
4949
}

lib/data/dto/agent-info.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ class AgentInfo {
4646
getAgentStartTime() {
4747
return this.agentStartTime
4848
}
49-
49+
5050
getServiceType() {
5151
return this.serviceType
5252
}
53+
54+
getApplicationName() {
55+
return this.applicationName
56+
}
5357
}
5458

5559
module.exports = AgentInfo

lib/instrumentation/http-shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ exports.traceOutgoingRequest2 = function (agent, moduleName) {
128128
trace.traceBlockEnd(spanEventRecorder)
129129

130130
// DefaultAsyncContext.java: newAsyncContextTrace
131-
const childTraceBuilder = this.traceContext.continueAsyncContextTraceObject(trace.getTraceRoot(), asyncId.nextLocalAsyncId2())
131+
const childTraceBuilder = agent.traceContext.continueAsyncContextTraceObject(trace.getTraceRoot(), asyncId.nextLocalAsyncId2())
132132
const recorder = childTraceBuilder.traceBlockBegin()
133133
recorder.recordServiceType(ServiceType.asyncHttpClient)
134134
recorder.recordApiDesc(req.method)

lib/instrumentation/http/http-outgoing-request-header.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class HttpClientRequest {
2323

2424
// write(T header, final TraceId traceId, final String host)
2525
write(traceId, agentInfo) {
26-
this.setHeader(Header.traceId, traceId.toStringFormatted())
26+
this.setHeader(Header.traceId, traceId.toStringDelimiterFormatted())
2727
this.setHeader(Header.spanId, traceId.getSpanId())
2828
this.setHeader(Header.parentSpanId, traceId.getParentSpanId())
2929
this.setHeader(Header.flags, traceId.getFlags())
3030
this.setHeader(Header.parentApplicationName, agentInfo.getApplicationName())
31-
this.setHeader(Header.parentApplicationType, agentInfo.getApplicationType())
31+
this.setHeader(Header.parentApplicationType, agentInfo.getServiceType())
3232

3333
if (this.host) {
3434
this.setHeader(Header.host, this.getHost())
@@ -39,17 +39,17 @@ class HttpClientRequest {
3939
this.request.setHeader?.(name, value)
4040
}
4141

42-
getUrl() {
43-
return this.request.url
44-
}
45-
4642
getHeader(name) {
4743
return this.request.getHeader?.(name)
4844
}
4945

5046
getHost() {
5147
return this.host
5248
}
49+
50+
getUrlPathname() {
51+
return new URL(`${this.request.protocol}//${this.getHost()}${this.request.path}`).pathname
52+
}
5353
}
5454

5555
class HttpOutgoingRequestHeader {
@@ -59,7 +59,7 @@ class HttpOutgoingRequestHeader {
5959
}
6060

6161
getUrlPathname() {
62-
return new URL(`${this.getHost()}${this.request.getUrl()}`).pathname
62+
return this.request.getUrlPathname()
6363
}
6464

6565
getHost() {

0 commit comments

Comments
 (0)