From c2c35d222af354f0ac64be3f5b89903ea0caee29 Mon Sep 17 00:00:00 2001 From: Yongseok Date: Wed, 28 Aug 2024 21:42:24 +0900 Subject: [PATCH] [#214] Save AgentInfo in Agent initialization time --- index.js | 4 +- lib/instrumentation/context/span-builder.js | 24 ++++-- lib/pinpoint-agent.js | 93 +++++++++++++++++++++ 3 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 lib/pinpoint-agent.js diff --git a/index.js b/index.js index 37c841ef..7c7a2c99 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/instrumentation/context/span-builder.js b/lib/instrumentation/context/span-builder.js index c30e2add..df43de18 100644 --- a/lib/instrumentation/context/span-builder.js +++ b/lib/instrumentation/context/span-builder.js @@ -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() @@ -24,7 +28,7 @@ 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 @@ -32,6 +36,14 @@ class SpanBuilder { 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 diff --git a/lib/pinpoint-agent.js b/lib/pinpoint-agent.js new file mode 100644 index 00000000..ef388291 --- /dev/null +++ b/lib/pinpoint-agent.js @@ -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