Skip to content

Commit 35ecf54

Browse files
committed
[#229] Refactoring Trace, AsyncTrace, DisableTrace and AsyncDisableTrace With TraceRoot
* continueTrace parent Information header parsing
1 parent 10f5e38 commit 35ecf54

33 files changed

+734
-179
lines changed

lib/config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ const path = require('path')
1010
const fs = require('fs')
1111
const defaultConfig = require('./pinpoint-config-default')
1212
const log = require('./utils/logger')
13-
const { setLog } = require('./supports')
14-
const { makeLogLevelLog } = require('./utils/log/log-level-logger')
1513
const ServiceConfigBuilder = require('./client/retry/service-config-builder')
1614

1715
const valueOfString = (envName) => {
@@ -147,7 +145,6 @@ const init = (initOptions = {}) => {
147145
readConfigJson(initOptions))
148146

149147
log.init(agentConfig.logLevel)
150-
setLog(makeLogLevelLog(agentConfig.logLevel))
151148

152149
Object.entries(REQUIRE_CONFIG).forEach(([propertyName, description]) => {
153150
if (agentConfig.enable && !agentConfig[propertyName]) {

lib/context/disable-span-recorder.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class DisableSpanRecorder {
2424
recordRemoteAddr() {}
2525

2626
recordException() {}
27-
28-
recordSpanEvent() {}
2927
}
3028

3129
module.exports = DisableSpanRecorder

lib/context/disable-trace.js

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

19+
getTraceRoot() {
20+
return this.traceRoot
21+
}
22+
1923
traceBlockBegin() {
2024
return new DisableSpanEventRecorder()
2125
}

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

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

77
'use strict'
88

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

1212
class RemoteTraceRoot {
1313
constructor(traceId, traceRoot) {
@@ -18,6 +18,10 @@ class RemoteTraceRoot {
1818
getTraceStartTime() {
1919
return this.traceRoot.getTraceStartTime()
2020
}
21+
22+
isSampled() {
23+
return true
24+
}
2125
}
2226

2327
class RemoteTraceRootBuilder {

lib/context/sequence-generator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class SequenceGenerator {
2424
return this.next.toString()
2525
}
2626

27+
getAndIncrement() {
28+
if (this.sequence > this.maxValue) {
29+
this.sequence = this.initValue
30+
}
31+
return this.sequence++
32+
}
33+
2734
// for test
2835
reset() {
2936
this.sequence = this.initValue

lib/context/span-builder.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,74 @@
99
class Span {
1010
constructor(traceRoot) {
1111
this.traceRoot = traceRoot
12-
this.startTime = traceRoot.getTraceStartTime()
1312
}
1413
}
1514

1615
class SpanBuilder {
1716
constructor(traceRoot) {
1817
this.traceRoot = traceRoot
18+
this.annotations = []
19+
this.startTime = traceRoot.getTraceStartTime()
20+
}
21+
22+
setApiId(apiId) {
23+
this.apiId = apiId
24+
return this
25+
}
26+
27+
setRpc(rpc) {
28+
this.rpc = rpc
29+
return this
30+
}
31+
32+
setEndPoint(endPoint) {
33+
this.endPoint = endPoint
34+
return this
35+
}
36+
37+
setRemoteAddress(remoteAddress) {
38+
this.remoteAddress = remoteAddress
39+
return this
40+
}
41+
42+
addAnnotation(annotation) {
43+
this.annotations.push(annotation)
44+
return this
45+
}
46+
47+
setExceptionInfo(id, message) {
48+
this.exceptionInfo = { id, message }
49+
return this
50+
}
51+
52+
setAcceptorHost(acceptorHost) {
53+
this.acceptorHost = acceptorHost
54+
return this
55+
}
56+
57+
setParentApplicationName(parentApplicationName) {
58+
this.parentApplicationName = parentApplicationName
59+
return this
60+
}
61+
62+
setParentApplicationType(parentApplicationType) {
63+
this.parentApplicationType = parentApplicationType
64+
return this
1965
}
2066

2167
build() {
22-
return new Span(this.traceRoot)
68+
const span = new Span(this.traceRoot)
69+
this.startTime = this.startTime || Date.now()
70+
span.apiId = this.apiId
71+
span.rpc = this.rpc
72+
span.endPoint = this.endPoint
73+
span.remoteAddress = this.remoteAddress
74+
span.annotations = this.annotations
75+
span.exceptionInfo = this.exceptionInfo
76+
span.acceptorHost = this.acceptorHost
77+
span.parentApplicationName = this.parentApplicationName
78+
span.parentApplicationType = this.parentApplicationType
79+
return span
2380
}
2481
}
2582

lib/context/span-id.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
// SpanId.java in Java agent
1010
class SpanId {
11+
static nullSpanId = -1
12+
1113
constructor () {
1214
this.MAX_NUM = Number.MAX_SAFE_INTEGER
1315
}

lib/context/span-recorder.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ class SpanRecorder {
7777
this.span.err = 1
7878
}
7979
}
80-
81-
recordSpanEvent(spanEvent) {
82-
if (this.span && spanEvent) {
83-
this.span.spanEventList.push(spanEvent)
84-
}
85-
}
8680
}
8781

8882
module.exports = SpanRecorder

lib/context/trace-builder.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/context/trace-context.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ const sampler = require('../sampler/sampler')
1515
const DisableTrace = require('./disable-trace')
1616
const localStorage = require('../instrumentation/context/local-storage')
1717
const TraceRootBuilder = require('./trace-root-builder')
18-
const RemoteTraceRootBuilder = require('./remote-trace-root-builder')
1918
const activeRequestRepository = require('./trace/active-request-repository')
2019
const TraceSampler = require('./trace/trace-sampler')
2120
const SpanBuilder = require('./span-builder')
2221
const SpanChunkBuilder = require('./span-chunk-builder')
2322
const SpanRepository = require('./trace/span-repository')
24-
const SpanRecorder = require('./span-recorder')
25-
const SpanEventRecorderBuilder = require('./trace/span-event-recorder-builder')
26-
const TraceBuilder = require('./trace-builder')
23+
const Trace2 = require('./trace/trace2')
2724

2825
class TraceContext {
2926
constructor(agentInfo, dataSender, config) {
@@ -34,7 +31,7 @@ class TraceContext {
3431
this.isSampling = sampler.getIsSampling(config.sampling, config.sampleRate)
3532
this.enableSampling = config.sampling
3633
}
37-
this.traceSampler = new TraceSampler(config)
34+
this.traceSampler = new TraceSampler(agentInfo, config)
3835
this.localTraceRootBuilder = new TraceRootBuilder(agentInfo.agentId)
3936
}
4037

@@ -111,31 +108,26 @@ class TraceContext {
111108

112109
// disableSampling() method in DefaultBaseTraceFactory.java
113110
disableSampling() {
114-
const state = this.traceSampler.getContinueDisabledState()
115-
return this.newLocalTrace(state.nextId())
111+
const traceRoot = this.traceSampler.makeContinueDisableTraceRoot()
112+
return this.newLocalTrace(traceRoot)
116113
}
117114

118-
newLocalTrace(nextDisabledId) {
119-
const traceRoot = this.localTraceRootBuilder.build(nextDisabledId)
115+
newLocalTrace(traceRoot) {
120116
activeRequestRepository.registry(traceRoot)
121117
return new DisableTrace(traceRoot)
122118
}
123119

124120
// newTraceObject method in DefaultBaseTraceFactory.java
125121
newTraceObject2(urlPath) {
126-
const state = this.traceSampler.newState(urlPath)
127-
if (!state.isSampled()) {
128-
return this.newLocalTrace(state.nextId())
122+
const traceRoot = this.traceSampler.makeNewTraceRoot(urlPath)
123+
if (!traceRoot.isSampled()) {
124+
return this.newLocalTrace(traceRoot)
129125
}
130126

131-
const traceRoot = new RemoteTraceRootBuilder(this.agentInfo).build(state.nextId())
132-
const span = new SpanBuilder(traceRoot).build()
127+
const spanBuilder = new SpanBuilder(traceRoot)
133128
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
134129
const repository = new SpanRepository(spanChunkBuilder, this.dataSender)
135-
136-
const spanRecorder = new SpanRecorder(span)
137-
const spanEventRecorder = new SpanEventRecorderBuilder(traceRoot).build()
138-
return new TraceBuilder(span, repository, spanRecorder, spanEventRecorder).build()
130+
return new Trace2(spanBuilder, repository, this.agentInfo.getServiceType())
139131
}
140132
}
141133

0 commit comments

Comments
 (0)