Skip to content

Commit

Permalink
[#214] Replace MockGrpcDataSender to fixture
Browse files Browse the repository at this point in the history
* Add gRPC fixture
* Save AgentInfo in Agent initialization time

[#218] The serviceType and applicationServiceType of Span are same. It doesn't needs to pass to construct parameter
  • Loading branch information
feelform committed Aug 29, 2024
1 parent eb113c8 commit 547ebd9
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 173 deletions.
5 changes: 3 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ 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 getConfig = require('./config').getConfig
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 Agent {
constructor(initOptions) {
this.config = getConfig(initOptions)
initializeConfig(initOptions)
this.config = getConfig()

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

Expand Down
4 changes: 4 additions & 0 deletions lib/client/grpc-data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class GrpcDataSender {
if (this.profilerClient) {
this.profilerClient.close()
}
if (this.profilerStream) {
this.profilerStream.grpcStream.end()
}
}

initializeClients(collectorIp, collectorTcpPort, config) {
Expand Down Expand Up @@ -125,6 +128,7 @@ class GrpcDataSender {
profilerBuilder.setGrpcServiceConfig(config.grpcServiceConfig.getProfiler())
}
this.profilerClient = new services.ProfilerCommandServiceClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(), profilerBuilder.build())
this.profilerStream = new GrpcBidirectionalStream('profilerStream', this.profilerClient, this.profilerClient.handleCommandV2)
}

agentInfoRefreshInterval() {
Expand Down
12 changes: 9 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ const CONFIG_FILE_MAP = {
profilerSqlStat: 'profiler-sql-stat'
}

let agentConfig = null

const REQUIRE_CONFIG = {
agentId: 'an Agent ID',
applicationName: 'an Application Name'
Expand Down Expand Up @@ -237,6 +235,11 @@ const getConfig = (initOptions) => {
return agentConfig
}

const initializeConfig = (initOptions) => {
clear()
init(initOptions)
}

const clear = () => agentConfig && (agentConfig = null)

//https://github.com/sindresorhus/is-docker
Expand All @@ -261,11 +264,14 @@ function hasDockerCGroup() {
}
}

let agentConfig = readConfigJson(defaultConfig)

module.exports = {
getConfig,
clear,
readConfigJson,
readRootConfigFile,
getMainModulePath,
isContainerEnvironment
isContainerEnvironment,
initializeConfig,
}
6 changes: 3 additions & 3 deletions lib/context/transaction-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const transactionIdGenerator = require('./sequence-generators').transactionIdGenerator

const DELIMETER = '^'
const delimiter = '^'

class TransactionId {
constructor (agentId, agentStartTime, sequence) {
Expand All @@ -23,12 +23,12 @@ class TransactionId {
}

toString () {
return [this.agentId, this.agentStartTime, this.sequence].join(DELIMETER)
return [this.agentId, this.agentStartTime, this.sequence].join(delimiter)
}

static toTransactionId(str) {
if (str !== null && str !== undefined) {
const r = str.split(DELIMETER)
const r = str.split(delimiter)
if (r.length === 3) {
return new TransactionId(r[0], r[1], r[2])
}
Expand Down
30 changes: 21 additions & 9 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, host, parentApplicationName, parentApplicationType) {
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,12 +28,20 @@ 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 = host
this.parentApplicationName = parentApplicationName
this.parentApplicationType = parentApplicationType
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) {
Expand Down
Loading

0 comments on commit 547ebd9

Please sign in to comment.