Skip to content

Commit

Permalink
[#214] Save AgentInfo in Agent initialization time
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Aug 28, 2024
1 parent 18f065d commit c2c35d2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 8 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

'use strict'

const Agent = require('./lib/agent')
const agent = new Agent()
const PinpointAgent = require('./lib/pinpoint-agent')
const agent = new PinpointAgent()
module.exports = agent
24 changes: 18 additions & 6 deletions lib/instrumentation/context/span-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
* Apache License v2.0
*/

const TransactionId = require("../../context/transaction-id")
const { getConfig } = require("../../config")
const TraceId = require("../../context/trace-id")

class SpanBuilder {
constructor(traceId, agentId, applicationName, applicationServiceType, agentStartTime, serviceType) {
constructor(traceId, agentInfo) {
this.traceId = traceId
this.agentId = agentId
this.applicationName = applicationName
this.agentStartTime = agentStartTime
this.serviceType = serviceType
this.agentId = agentInfo.agentId
this.applicationName = agentInfo.applicationName
this.agentStartTime = agentInfo.agentStartTime
this.serviceType = agentInfo.serviceType
this.spanId = traceId.spanId
this.parentSpanId = traceId.parentSpanId
this.startTime = Date.now()
Expand All @@ -24,14 +28,22 @@ class SpanBuilder {
this.spanEventList = []
this.apiId = null
this.exceptionInfo = null
this.applicationServiceType = applicationServiceType
this.applicationServiceType = agentInfo.applicationServiceType
this.loggingTransactionInfo = null
this.version = 1
this.acceptorHost = undefined
this.parentApplicationName = undefined
this.parentApplicationType = undefined
}

static makeSpanBuilderWithSpanId(spanId, agentInfo) {
const config = getConfig()
const transactionId = new TransactionId(config.agentId, config.agentStartTime)
const traceId = new TraceId(transactionId, spanId)
const builder = new SpanBuilder(traceId, agentInfo)
return builder
}

static valueOf(span) {
const builder = new SpanBuilder(span.traceId, span.agentId, span.applicationName, span.applicationServiceType, span.agentStartTime, span.serviceType, span.acceptorHost, span.parentApplicationName, span.parentApplicationType)
builder.spanId = span.spanId
Expand Down
93 changes: 93 additions & 0 deletions lib/pinpoint-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const ModuleHook = require('./instrumentation/module-hook')
const traceContext = require('./context/trace-context')
const log = require('./utils/logger')
const stringMetaService = require('./context/string-meta-service')
const apiMetaService = require('./context/api-meta-service')
const Scheduler = require('./utils/scheduler')
const AgentStatsMonitor = require('./metric/agent-stats-monitor')
const { initializeConfig, getConfig } = require('./config')
const PinpointClient = require('./client/pinpoint-client')
const dataSenderFactory = require('./client/data-sender-factory')
const AgentInfo = require('./data/dto/agent-info')
const PinScheduler = require('./metric/ping-scheduler')

class PinpointAgent {
constructor(initOptions) {
initializeConfig(initOptions)
this.config = getConfig()

log.warn('[Pinpoint Agent] Configuration', this.config)

if (!this.config || !this.config.enable || this.config.enable.toString() !== 'true') {
log.warn('[Pinpoint Agent][' + this.config.agentId + '] Disabled')
return
}
log.warn('[Pinpoint Agent][' + this.config.agentId + '] Init Started')

const agentId = this.config.agentId
const agentStartTime = Date.now()
this.agentInfo = this.createAgentInfo(this.config, agentStartTime)

this.initializeDataSender()
this.initializePinpointClient()

this.traceContext = traceContext.init(this.agentInfo, this.dataSender, this.config)

stringMetaService.init(this.dataSender)
apiMetaService.init(this.dataSender)

this.startSchedule(agentId, agentStartTime)
this.initializeSupportModules()

log.warn('[Pinpoint Agent][' + agentId + '] Init Completed')
}

initializeDataSender() {
this.dataSender = dataSenderFactory.create(this.config, this.agentInfo)
this.dataSender.send(this.agentInfo)
}

initializeSupportModules() {
this.moduleHook = new ModuleHook(this)
}

initializePinpointClient() {
this.pinpointClient = new PinpointClient(this.config, this.agentInfo, this.dataSender)
}

createTraceObject(requestData) {
return this.traceContext.makeTrace(requestData)
}

currentTraceObject() {
return this.traceContext.currentTraceObject()
}

completeTraceObject(trace) {
this.traceContext.completeTraceObject(trace)
}

createAgentInfo(config, agentStartTime) {
return AgentInfo.create(config, agentStartTime)
}

startSchedule(agentId, agentStartTime) {
if (this.config.enabledStatsMonitor) {
this.mainScheduler = new Scheduler(5000)
const agentStatsMonitor = new AgentStatsMonitor(this.pinpointClient.dataSender, agentId, agentStartTime)
this.mainScheduler.addJob(() => { agentStatsMonitor.run() })
this.mainScheduler.start()
}
this.pingScheduler = new PinScheduler(this.dataSender)
}
}

module.exports = PinpointAgent

0 comments on commit c2c35d2

Please sign in to comment.