Skip to content

Commit d79f4fa

Browse files
committed
[#231] Support ChildTrace
1 parent 34baa14 commit d79f4fa

File tree

6 files changed

+62
-34
lines changed

6 files changed

+62
-34
lines changed

lib/context/trace-context.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ class TraceContext {
131131
// DefaultAsyncContext.java: newAsyncContextTrace
132132
// DefaultBaseTraceFactory.java: continueAsyncContextTraceObject
133133
// AsyncContextSpanEventEndPointApiAwareInterceptor.java : before
134-
continueAsyncContextTraceObject(traceRoot, asyncId) {
135-
const localAsyncId = asyncId.nextLocalAsyncId2()
136-
134+
continueAsyncContextTraceObject(traceRoot, localAsyncId) {
135+
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
137136
}
138137
}
139138

lib/context/trace/span-event-builder.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'use strict'
88

99
const StackId = require('./stack-id')
10+
const AsyncId = require('../async-id')
1011

1112
class SpanEvent {
1213
static nullObject = new SpanEvent(0, 0)
@@ -44,7 +45,7 @@ class SpanEventBuilder {
4445
if (stackId === StackId.default) {
4546
return SpanEventBuilder.default()
4647
}
47-
48+
4849
return new SpanEventBuilder(stackId)
4950
}
5051

@@ -87,11 +88,19 @@ class SpanEventBuilder {
8788
return this
8889
}
8990

91+
// WrappedSpanEventRecorder.java: getNextAsyncId
92+
getAsyncId() {
93+
if (!this.asyncId) {
94+
this.asyncId = AsyncId.make()
95+
}
96+
return this.asyncId
97+
}
98+
9099
setAsyncId(asyncId) {
91100
this.asyncId = asyncId
92101
return this
93102
}
94-
103+
95104
markAfterTime() {
96105
this.setAfterTime(Date.now())
97106
return this

lib/context/trace/span-event-recorder2.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SpanEventRecorder {
1515
static nullObject() {
1616
return new SpanEventRecorder(SpanEventBuilder.nullObject())
1717
}
18-
18+
1919
constructor(spanEventBuilder) {
2020
this.spanEventBuilder = spanEventBuilder
2121
}
@@ -49,18 +49,7 @@ class SpanEventRecorder {
4949
}
5050

5151
recordNextAsyncId() {
52-
this.asyncId = this.asyncId.sequenceNextLocalAsyncId()
53-
this.spanEventBuilder.setNextAsyncId(this.asyncId.getAsyncId())
54-
}
55-
56-
getNextAsyncId() {
57-
const nextAsyncId = this.spanEventBuilder.getAsyncId()
58-
if (!nextAsyncId) {
59-
const asyncId = AsyncId.make()
60-
this.spanEventBuilder.setAsyncId(asyncId)
61-
return asyncId
62-
}
63-
return nextAsyncId
52+
return this.spanEventBuilder.getAsyncId()
6453
}
6554
}
6655

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict'
88

9-
const spanIdGenerator = require('../span-id')
9+
const spanId = require('../span-id')
1010

1111
// DefaultTraceId.java
1212
class TraceId {
@@ -53,7 +53,7 @@ class TraceIdBuilder {
5353
constructor(agentInfo, transactionId) {
5454
this.agentInfo = agentInfo
5555
this.transactionId = transactionId
56-
this.parentSpanId = spanIdGenerator.nullSpanId()
56+
this.parentSpanId = spanId.nullSpanId()
5757
}
5858

5959
make(transactionId) {
@@ -71,8 +71,8 @@ class TraceIdBuilder {
7171
}
7272

7373
build() {
74-
const spanId = this.spanId ?? spanIdGenerator.newSpanId()
75-
return new TraceId(this.agentInfo.getAgentId(), this.agentInfo.getAgentStartTime(), this.transactionId, this.parentSpanId, spanId, 0)
74+
return new TraceId(this.agentInfo.getAgentId(), this.agentInfo.getAgentStartTime()
75+
, this.transactionId, this.parentSpanId, this.spanId ?? spanId.newSpanId(), 0)
7676
}
7777
}
7878

lib/instrumentation/http-shared.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ exports.traceOutgoingRequest = function (agent, moduleName) {
171171
return req
172172

173173
function onresponse(res) {
174-
log.debug('intercepted http.ClientcRequest response event %o', { id: httpURL })
175174
// Inspired by:
176175
// https://github.com/nodejs/node/blob/9623ce572a02632b7596452e079bba066db3a429/lib/events.js#L258-L274
177176
if (res.prependListener) {
@@ -191,7 +190,6 @@ exports.traceOutgoingRequest = function (agent, moduleName) {
191190
}
192191

193192
function onEnd() {
194-
log.debug('intercepted http.IncomingMessage end event %o', { id: httpURL })
195193
if (asyncTrace) {
196194
asyncEventRecorder.recordAttribute(annotationKey.HTTP_STATUS_CODE, this.statusCode)
197195
asyncTrace.traceAsyncEnd(asyncEventRecorder)

lib/instrumentation/http/http-outgoing-request-trace-builder.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,62 @@
88

99
const samplingFlag = require('../../sampler/sampling-flag')
1010
const Header = require('./pinpoint-header')
11+
const ServiceType = require('../../context/service-type')
1112

1213
class HttpOutgoingRequestTrace {
13-
constructor(trace, spanEventRecorder, request) {
14-
this.trace = trace
15-
this.spanEventRecorder = spanEventRecorder
14+
static nullObject = new HttpOutgoingRequestTrace()
15+
16+
constructor(childTraceBuilder) {
17+
this.childTraceBuilder = childTraceBuilder
18+
}
19+
20+
close() {
21+
if (HttpOutgoingRequestTrace.nullObject === this) {
22+
return
23+
}
24+
}
25+
}
26+
27+
class HttpClientRequestWriter {
28+
constructor(request) {
1629
this.request = request
1730
}
1831

19-
32+
writeSampledHeaderFalse() {
33+
this.setHeader(Header.sampled, samplingFlag.samplingRateFalse())
34+
}
35+
36+
setHeader(name, value) {
37+
this.request.setHeader?.(name, value)
38+
}
2039
}
2140

2241
class HttpOutgoingRequestTraceBuilder {
2342
constructor(context, request) {
24-
this.context = context
25-
this.request = request
43+
this.traceContext = context
44+
this.request = new HttpClientRequestWriter(request)
2645
}
2746

2847
build() {
29-
const trace = this.context.currentTraceObject()
48+
const trace = this.traceContext.currentTraceObject()
49+
if (!trace) {
50+
return HttpOutgoingRequestTrace.nullObject
51+
}
3052

31-
}
32-
writeSampledHeaderFalse() {
33-
this.request.setHeader(Header.sampled, samplingFlag.samplingRateFalse())
53+
if (!trace.canSampled()) {
54+
this.request.writeSampledHeaderFalse()
55+
return HttpOutgoingRequestTrace.nullObject
56+
}
57+
58+
const spanEventRecorder = trace.traceBlockBegin()
59+
spanEventRecorder.recordServiceType(ServiceType.asyncHttpClientInternal)
60+
spanEventRecorder.recordApiDesc('http.request')
61+
const asyncId = spanEventRecorder.recordNextAsyncId()
62+
trace.traceBlockEnd(spanEventRecorder)
63+
64+
// DefaultAsyncContext.java: newAsyncContextTrace
65+
const childTraceBuilder = this.traceContext.continueAsyncContextTraceObject(trace.getTraceRoot(), asyncId.nextLocalAsyncId2())
66+
return new HttpOutgoingRequestTrace(childTraceBuilder)
3467
}
3568
}
3669

0 commit comments

Comments
 (0)