Skip to content

Commit

Permalink
[#229] Support TraceRoot
Browse files Browse the repository at this point in the history
*  Refactoring Trace, AsyncTrace, DisableTrace and AsyncDisableTrace With TraceRoot
* continueTrace parent Information header parsing
* implements TraceId
  • Loading branch information
feelform committed Oct 24, 2024
1 parent 10f5e38 commit 6484775
Show file tree
Hide file tree
Showing 38 changed files with 857 additions and 186 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
10 changes: 10 additions & 0 deletions lib/context/async-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class AsyncId {
static asyncIdGenerator = new SequenceGenerator(1)
static nonAsyncId = new AsyncId(0, 0)

// DefaultAsyncIdGenerator.java: nextAsyncId()
static make() {
return new AsyncId(AsyncId.asyncIdGenerator.next, 0)
}

constructor(asyncId, sequence) {
this.asyncId = asyncId
this.sequence = sequence
Expand All @@ -31,6 +36,11 @@ class AsyncId {
return new AsyncId(asyncId, this.sequence + 1)
}

// DefaultAsyncId.java: nextLocalAsyncId
nextLocalAsyncId2() {
return new AsyncId(this.asyncId, this.sequence + 1)
}

sequenceNextLocalAsyncId() {
if (this === AsyncId.nonAsyncId) {
return this.nextLocalAsyncId()
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
8 changes: 2 additions & 6 deletions lib/context/span-chunk-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
*/

'use strict'
class SpanChunk {
constructor(traceRoot, spanEventList) {
this.traceRoot = traceRoot
this.spanEventList = spanEventList
}
}

const SpanChunk = require('./trace/span-chunk2')

class SpanChunkBuilder {
constructor(traceRoot) {
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.

35 changes: 16 additions & 19 deletions lib/context/trace-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ const log = require('../utils/logger')
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,8 +30,7 @@ class TraceContext {
this.isSampling = sampler.getIsSampling(config.sampling, config.sampleRate)
this.enableSampling = config.sampling
}
this.traceSampler = new TraceSampler(config)
this.localTraceRootBuilder = new TraceRootBuilder(agentInfo.agentId)
this.traceSampler = new TraceSampler(agentInfo, config)
}

continueTraceObject(requestData) {
Expand Down Expand Up @@ -111,31 +106,33 @@ 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)
return new Trace2(spanBuilder, repository, this.agentInfo.getServiceType())
}

const spanRecorder = new SpanRecorder(span)
const spanEventRecorder = new SpanEventRecorderBuilder(traceRoot).build()
return new TraceBuilder(span, repository, spanRecorder, spanEventRecorder).build()
// DefaultAsyncContext.java: newAsyncContextTrace
// DefaultBaseTraceFactory.java: continueAsyncContextTraceObject
continueAsyncContextTraceObject(traceRoot, asyncId) {
const localAsyncId = asyncId.nextLocalAsyncId2()

}
}

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
43 changes: 43 additions & 0 deletions lib/context/trace/async-span-chunk-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const SpanChunk = require('./span-chunk2')

// DefaultAsyncSpanChunk.java
class AsyncSpanChunk extends SpanChunk {
constructor(traceRoot, spanEventList, localAsyncId) {
super(traceRoot, spanEventList)
this.localAsyncId = localAsyncId
}

getLocalAsyncId() {
return this.localAsyncId
}

toString() {
return `AsyncSpanChunk(traceRoot=${this.traceRoot}, spanEventList=${this.spanEventList}, localAsyncId=${this.localAsyncId})`
}
}

// AsyncSpanChunkFactory.java
class AsyncSpanChunkBuilder {
constructor(traceRoot, localAsyncId) {
this.traceRoot = traceRoot
this.localAsyncId = localAsyncId
}

build(spanEventList) {
return new AsyncSpanChunk(this.traceRoot, this.asyncId, spanEventList)
}

toString() {
return `AsyncSpanChunkBuilder(traceRoot=${this.traceRoot}, localAsyncId=${this.localAsyncId})`
}
}

module.exports = AsyncSpanChunkBuilder
Loading

0 comments on commit 6484775

Please sign in to comment.