Skip to content

Commit

Permalink
[#229] Refactoring Trace, AsyncTrace, DisableTrace and AsyncDisableTr…
Browse files Browse the repository at this point in the history
…ace With TraceRoot

* continueTrace parent Information header parsing
  • Loading branch information
feelform committed Oct 23, 2024
1 parent 10f5e38 commit 35ecf54
Show file tree
Hide file tree
Showing 33 changed files with 734 additions and 179 deletions.
3 changes: 0 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const path = require('path')
const fs = require('fs')
const defaultConfig = require('./pinpoint-config-default')
const log = require('./utils/logger')
const { setLog } = require('./supports')
const { makeLogLevelLog } = require('./utils/log/log-level-logger')
const ServiceConfigBuilder = require('./client/retry/service-config-builder')

const valueOfString = (envName) => {
Expand Down Expand Up @@ -147,7 +145,6 @@ const init = (initOptions = {}) => {
readConfigJson(initOptions))

log.init(agentConfig.logLevel)
setLog(makeLogLevelLog(agentConfig.logLevel))

Object.entries(REQUIRE_CONFIG).forEach(([propertyName, description]) => {
if (agentConfig.enable && !agentConfig[propertyName]) {
Expand Down
2 changes: 0 additions & 2 deletions lib/context/disable-span-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class DisableSpanRecorder {
recordRemoteAddr() {}

recordException() {}

recordSpanEvent() {}
}

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

getTraceRoot() {
return this.traceRoot
}

traceBlockBegin() {
return new DisableSpanEventRecorder()
}
Expand Down
8 changes: 6 additions & 2 deletions lib/context/remote-trace-root-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

'use strict'

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

class RemoteTraceRoot {
constructor(traceId, traceRoot) {
Expand All @@ -18,6 +18,10 @@ class RemoteTraceRoot {
getTraceStartTime() {
return this.traceRoot.getTraceStartTime()
}

isSampled() {
return true
}
}

class RemoteTraceRootBuilder {
Expand Down
7 changes: 7 additions & 0 deletions lib/context/sequence-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class SequenceGenerator {
return this.next.toString()
}

getAndIncrement() {
if (this.sequence > this.maxValue) {
this.sequence = this.initValue
}
return this.sequence++
}

// for test
reset() {
this.sequence = this.initValue
Expand Down
61 changes: 59 additions & 2 deletions lib/context/span-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,74 @@
class Span {
constructor(traceRoot) {
this.traceRoot = traceRoot
this.startTime = traceRoot.getTraceStartTime()
}
}

class SpanBuilder {
constructor(traceRoot) {
this.traceRoot = traceRoot
this.annotations = []
this.startTime = traceRoot.getTraceStartTime()
}

setApiId(apiId) {
this.apiId = apiId
return this
}

setRpc(rpc) {
this.rpc = rpc
return this
}

setEndPoint(endPoint) {
this.endPoint = endPoint
return this
}

setRemoteAddress(remoteAddress) {
this.remoteAddress = remoteAddress
return this
}

addAnnotation(annotation) {
this.annotations.push(annotation)
return this
}

setExceptionInfo(id, message) {
this.exceptionInfo = { id, message }
return this
}

setAcceptorHost(acceptorHost) {
this.acceptorHost = acceptorHost
return this
}

setParentApplicationName(parentApplicationName) {
this.parentApplicationName = parentApplicationName
return this
}

setParentApplicationType(parentApplicationType) {
this.parentApplicationType = parentApplicationType
return this
}

build() {
return new Span(this.traceRoot)
const span = new Span(this.traceRoot)
this.startTime = this.startTime || Date.now()
span.apiId = this.apiId
span.rpc = this.rpc
span.endPoint = this.endPoint
span.remoteAddress = this.remoteAddress
span.annotations = this.annotations
span.exceptionInfo = this.exceptionInfo
span.acceptorHost = this.acceptorHost
span.parentApplicationName = this.parentApplicationName
span.parentApplicationType = this.parentApplicationType
return span
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/context/span-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

// SpanId.java in Java agent
class SpanId {
static nullSpanId = -1

constructor () {
this.MAX_NUM = Number.MAX_SAFE_INTEGER
}
Expand Down
6 changes: 0 additions & 6 deletions lib/context/span-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ class SpanRecorder {
this.span.err = 1
}
}

recordSpanEvent(spanEvent) {
if (this.span && spanEvent) {
this.span.spanEventList.push(spanEvent)
}
}
}

module.exports = SpanRecorder
32 changes: 0 additions & 32 deletions lib/context/trace-builder.js

This file was deleted.

28 changes: 10 additions & 18 deletions lib/context/trace-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ const sampler = require('../sampler/sampler')
const DisableTrace = require('./disable-trace')
const localStorage = require('../instrumentation/context/local-storage')
const TraceRootBuilder = require('./trace-root-builder')
const RemoteTraceRootBuilder = require('./remote-trace-root-builder')
const activeRequestRepository = require('./trace/active-request-repository')
const TraceSampler = require('./trace/trace-sampler')
const SpanBuilder = require('./span-builder')
const SpanChunkBuilder = require('./span-chunk-builder')
const SpanRepository = require('./trace/span-repository')
const SpanRecorder = require('./span-recorder')
const SpanEventRecorderBuilder = require('./trace/span-event-recorder-builder')
const TraceBuilder = require('./trace-builder')
const Trace2 = require('./trace/trace2')

class TraceContext {
constructor(agentInfo, dataSender, config) {
Expand All @@ -34,7 +31,7 @@ class TraceContext {
this.isSampling = sampler.getIsSampling(config.sampling, config.sampleRate)
this.enableSampling = config.sampling
}
this.traceSampler = new TraceSampler(config)
this.traceSampler = new TraceSampler(agentInfo, config)
this.localTraceRootBuilder = new TraceRootBuilder(agentInfo.agentId)
}

Expand Down Expand Up @@ -111,31 +108,26 @@ class TraceContext {

// disableSampling() method in DefaultBaseTraceFactory.java
disableSampling() {
const state = this.traceSampler.getContinueDisabledState()
return this.newLocalTrace(state.nextId())
const traceRoot = this.traceSampler.makeContinueDisableTraceRoot()
return this.newLocalTrace(traceRoot)
}

newLocalTrace(nextDisabledId) {
const traceRoot = this.localTraceRootBuilder.build(nextDisabledId)
newLocalTrace(traceRoot) {
activeRequestRepository.registry(traceRoot)
return new DisableTrace(traceRoot)
}

// newTraceObject method in DefaultBaseTraceFactory.java
newTraceObject2(urlPath) {
const state = this.traceSampler.newState(urlPath)
if (!state.isSampled()) {
return this.newLocalTrace(state.nextId())
const traceRoot = this.traceSampler.makeNewTraceRoot(urlPath)
if (!traceRoot.isSampled()) {
return this.newLocalTrace(traceRoot)
}

const traceRoot = new RemoteTraceRootBuilder(this.agentInfo).build(state.nextId())
const span = new SpanBuilder(traceRoot).build()
const spanBuilder = new SpanBuilder(traceRoot)
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
const repository = new SpanRepository(spanChunkBuilder, this.dataSender)

const spanRecorder = new SpanRecorder(span)
const spanEventRecorder = new SpanEventRecorderBuilder(traceRoot).build()
return new TraceBuilder(span, repository, spanRecorder, spanEventRecorder).build()
return new Trace2(spanBuilder, repository, this.agentInfo.getServiceType())
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/context/trace-root-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class TraceRoot {
getTraceStartTime() {
return this.traceStartTime
}

isSampled() {
return false
}
}

class TraceRootBuilder {
Expand Down
2 changes: 1 addition & 1 deletion lib/context/trace/continue-disabled-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'

const LongIdGenerator = require("./long-id-generator")
const LongIdGenerator = require('./long-id-generator')

// INITIAL_CONTINUED_DISABLED_ID in AtomicIdGenerator.java
const continueDisabledId = new LongIdGenerator(-1003)
Expand Down
2 changes: 1 addition & 1 deletion lib/context/trace/id-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'

const LongIdGenerator = require("./long-id-generator")
const LongIdGenerator = require('./long-id-generator')

// INITIAL_TRANSACTION_ID
const transactionId = new LongIdGenerator(1)
Expand Down
61 changes: 61 additions & 0 deletions lib/context/trace/span-event-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

class SpanEvent {
constructor(sequence) {
this.sequence = sequence
}
}

class SpanEventBuilder {
// DefaultCallStack.java: push sequence 0 start value, depth 1 start value
static nullObject = new SpanEventBuilder(-1, 0)

constructor(sequence, depth) {
this.sequence = sequence
this.depth = depth
this.annotations = []
this.startTime = Date.now()
}

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

addAnnotation(annotation) {
this.annotations.push(annotation)
return this
}

setApiId(apiId) {
this.apiId = apiId
return this
}

setNextAsyncId(asyncId) {
this.asyncId = asyncId
return this
}

makeSequenceAndDepthGrowth() {
return new SpanEventBuilder(this.sequence + 1, this.depth + 1)
}

build() {
const spanEvent = new SpanEvent(this.sequence)
spanEvent.apiId = this.apiId
spanEvent.depth = this.depth
spanEvent.annotations = this.annotations
spanEvent.startTime = this.startTime
spanEvent.elapsedTime = this.elapsedTime
return spanEvent
}
}

module.exports = SpanEventBuilder
25 changes: 0 additions & 25 deletions lib/context/trace/span-event-recorder-builder.js

This file was deleted.

Loading

0 comments on commit 35ecf54

Please sign in to comment.