Skip to content

Commit

Permalink
[pinpoint-apm#214] Replace MockGrpcDataSender to fixture
Browse files Browse the repository at this point in the history
* Add gRPC fixture

[pinpoint-apm#218] The serviceType and applicationServiceType of Span are same. It doesn't needs to pass to construct parameter
  • Loading branch information
feelform committed Aug 28, 2024
1 parent eb113c8 commit 18f065d
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 168 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
8 changes: 4 additions & 4 deletions lib/instrumentation/context/span-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

class SpanBuilder {
constructor(traceId, agentId, applicationName, applicationServiceType, agentStartTime, serviceType, host, parentApplicationName, parentApplicationType) {
constructor(traceId, agentId, applicationName, applicationServiceType, agentStartTime, serviceType) {
this.traceId = traceId
this.agentId = agentId
this.applicationName = applicationName
Expand All @@ -27,9 +27,9 @@ class SpanBuilder {
this.applicationServiceType = 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 valueOf(span) {
Expand Down
222 changes: 133 additions & 89 deletions test/client/grpc-data-sender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ const SpanChunk = require('../../lib/context/span-chunk')
const Span = require('../../lib/context/span')
const SpanEvent = require('../../lib/context/span-event')
const MockGrpcDataSender = require('./mock-grpc-data-sender')
const grpc = require('@grpc/grpc-js')
const services = require('../../lib/data/v1/Service_grpc_pb')
const { beforeSpecificOne, afterOne, getCallRequests, getMetadata, DataSourceCallCountable } = require('./grpc-fixture')

function sendSpan(call, callback) {
call.on('error', function (error) {
})
call.on('data', function (spanMessage) {
const span = spanMessage.getSpan()
const callRequests = getCallRequests()
callRequests.push(span)
})
call.on('end', function () {
})
const callMetadata = getMetadata()
callMetadata.push(call.metadata)
}

class DataSource extends DataSourceCallCountable {
constructor(collectorIp, collectorTcpPort, collectorStatPort, collectorSpanPort, agentInfo, config) {
super(collectorIp, collectorTcpPort, collectorStatPort, collectorSpanPort, agentInfo, config)
}

initializeClients() { }
initializeMetadataClients() { }
initializeStatStream() { }
initializePingStream() { }
initializeAgentInfoScheduler() { }
}

test('Should send span ', function (t) {
const expectedSpan = {
Expand Down Expand Up @@ -64,7 +93,22 @@ test('Should send span ', function (t) {
agentStartTime: 1592574173350
}), expectedSpan)

const grpcDataSender = new MockGrpcDataSender('', 0, 0, 0, {agentId: 'agent', applicationName: 'applicationName', agentStartTime: 1234344})
const server = new grpc.Server()
server.addService(services.SpanService, {
sendSpan: sendSpan
})
let dataSender
server.bindAsync('localhost:0', grpc.ServerCredentials.createInsecure(), (error, port) => {
dataSender = beforeSpecificOne(port, DataSource)
dataSender.sendSpan(span)
afterOne(t)
})
t.teardown(() => {
dataSender.close()
server.forceShutdown()
})

const grpcDataSender = new MockGrpcDataSender('', 0, 0, 0, { agentId: 'agent', applicationName: 'applicationName', agentStartTime: 1234344 })
grpcDataSender.sendSpan(span)

t.plan(20)
Expand Down Expand Up @@ -120,7 +164,7 @@ test('Should send span ', function (t) {
t.equal(actual.getLoggingtransactioninfo(), 0, 'logging transaction info')
})

const grpcDataSender = new MockGrpcDataSender('', 0, 0, 0, {agentId: 'agent', applicationName: 'applicationName', agentStartTime: 1234344})
const grpcDataSender = new MockGrpcDataSender('', 0, 0, 0, { agentId: 'agent', applicationName: 'applicationName', agentStartTime: 1234344 })

test('sendSpanChunk redis.SET.end', function (t) {
let expectedSpanChunk = {
Expand All @@ -140,51 +184,51 @@ test('sendSpanChunk redis.SET.end', function (t) {
'sequence': 0
},
'spanEventList': [Object.assign(new SpanEvent({
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 0), {
'spanId': 7056897257955935,
'sequence': 0,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 14,
'serviceType': 100,
'endPoint': null,
'annotations': [],
'depth': 1,
'nextSpanId': -1,
'destinationId': null,
'apiId': 1,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}),
Object.assign(new SpanEvent({
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 1), {
'spanId': 7056897257955935,
'sequence': 1,
'startTime': 1592872091543,
'elapsedTime': 2,
'startElapsed': 7,
'serviceType': 8200,
'endPoint': 'localhost:6379',
'annotations': [Annotations.of(annotationKey.API.getCode(), 'redis.SET.end')],
'depth': 2,
'nextSpanId': 1508182809976945,
'destinationId': 'Redis',
'apiId': 0,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
})
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 0), {
'spanId': 7056897257955935,
'sequence': 0,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 14,
'serviceType': 100,
'endPoint': null,
'annotations': [],
'depth': 1,
'nextSpanId': -1,
'destinationId': null,
'apiId': 1,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}),
Object.assign(new SpanEvent({
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 1), {
'spanId': 7056897257955935,
'sequence': 1,
'startTime': 1592872091543,
'elapsedTime': 2,
'startElapsed': 7,
'serviceType': 8200,
'endPoint': 'localhost:6379',
'annotations': [Annotations.of(annotationKey.API.getCode(), 'redis.SET.end')],
'depth': 2,
'nextSpanId': 1508182809976945,
'destinationId': 'Redis',
'apiId': 0,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
})
],
'endPoint': null,
'applicationServiceType': 1400,
Expand Down Expand Up @@ -269,48 +313,48 @@ test('sendSpanChunk redis.GET.end', (t) => {
'sequence': 0
},
'spanEventList': [Object.assign(new SpanEvent({
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 0), {
'spanId': 7056897257955935,
'sequence': 0,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 14,
'serviceType': 100,
'endPoint': null,
'annotations': [],
'depth': 1,
'nextSpanId': -1,
'destinationId': null,
'apiId': 1,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}),
{
'spanId': 7056897257955935,
'sequence': 1,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 7,
'serviceType': 8200,
'endPoint': 'localhost:6379',
'annotations': [Annotations.of(annotationKey.API.getCode(), 'redis.GET.end')],
'depth': 2,
'nextSpanId': 6277978728741477,
'destinationId': 'Redis',
'apiId': 0,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}
spanId: 7056897257955935,
endPoint: 'localhost:6379'
}, 0), {
'spanId': 7056897257955935,
'sequence': 0,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 14,
'serviceType': 100,
'endPoint': null,
'annotations': [],
'depth': 1,
'nextSpanId': -1,
'destinationId': null,
'apiId': 1,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}),
{
'spanId': 7056897257955935,
'sequence': 1,
'startTime': 1592872091543,
'elapsedTime': 0,
'startElapsed': 7,
'serviceType': 8200,
'endPoint': 'localhost:6379',
'annotations': [Annotations.of(annotationKey.API.getCode(), 'redis.GET.end')],
'depth': 2,
'nextSpanId': 6277978728741477,
'destinationId': 'Redis',
'apiId': 0,
'exceptionInfo': null,
'asyncId': null,
'nextAsyncId': null,
'asyncSequence': null,
'dummyId': null,
'nextDummyId': null
}
],
'endPoint': null,
'applicationServiceType': 1400,
Expand Down
Loading

0 comments on commit 18f065d

Please sign in to comment.