Skip to content

Commit

Permalink
[pinpoint-apm#214] Replace MockGrpcDataSender to fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Aug 27, 2024
1 parent 36b393f commit fa311e2
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 126 deletions.
223 changes: 131 additions & 92 deletions test/client/grpc-data-sender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,34 @@ 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')

const service = (call, callback) => {
callback(null, result)
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) {
Expand Down Expand Up @@ -68,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 @@ -124,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 @@ -144,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 @@ -273,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 Expand Up @@ -681,7 +721,6 @@ test('sendSpan', (t) => {

const CommandType = require('../../lib/constant/commaned-type')
const Annotations = require('../../lib/instrumentation/context/annotation/annotations')
const { result } = require('lodash')
test.skip('sendHandshake', (t) => {
let expectedParams = {
supportCommandList: [CommandType.ECHO, CommandType.ACTIVE_THREAD_COUNT, CommandType.ACTIVE_THREAD_COUNT_RESPONSE],
Expand Down
39 changes: 38 additions & 1 deletion test/client/grpc-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const config = require('../../lib/config')
const AgentInfo = require('../../lib/data/dto/agent-info')
const GrpcDataSender = require('../../lib/client/grpc-data-sender')

let callCount = 0
let afterCount = 0
Expand Down Expand Up @@ -65,10 +66,46 @@ function increaseCallCount() {
callCount++
}

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

sendAgentInfo(agentInfo, callArguments) {
increaseCallCount()
super.sendAgentInfo(agentInfo, callArguments)
}

sendApiMetaInfo(apiMetaInfo, callArguments) {
increaseCallCount()
super.sendApiMetaInfo(apiMetaInfo, callArguments)
}

sendStringMetaInfo(stringMetaInfo, callArguments) {
increaseCallCount()
super.sendStringMetaInfo(stringMetaInfo, callArguments)
}

sendSqlMetaInfo(sqlMetaData, callback) {
increaseCallCount()
super.sendSqlMetaInfo(sqlMetaData, callback)
}

sendSqlUidMetaData(sqlMetaData, callback) {
increaseCallCount()
super.sendSqlUidMetaData(sqlMetaData, callback)
}

sendSpan(span) {
increaseCallCount()
super.sendSpan(span)
}
}

module.exports = {
beforeSpecificOne,
afterOne,
getCallRequests,
getMetadata,
increaseCallCount,
DataSourceCallCountable,
}
34 changes: 1 addition & 33 deletions test/client/grpc-unary-rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ const spanMessages = require('../../lib/data/v1/Span_pb')
const AgentInfo = require('../../lib/data/dto/agent-info')
const ApiMetaInfo = require('../../lib/data/dto/api-meta-info')
const StringMetaInfo = require('../../lib/data/dto/string-meta-info')
const GrpcDataSender = require('../../lib/client/grpc-data-sender')
const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-builder')
const CallArgumentsBuilder = require('../../lib/client/call-arguments-builder')
const config = require('../../lib/config')
const SqlMetaData = require('../../lib/client/sql-meta-data')
const sqlMetadataService = require('../../lib/instrumentation/sql/sql-metadata-service')
const SqlUidMetaData = require('../../lib/client/sql-uid-meta-data')
const { beforeSpecificOne, afterOne, getCallRequests, getMetadata, increaseCallCount } = require('./grpc-fixture')
const { beforeSpecificOne, afterOne, getCallRequests, getMetadata, DataSourceCallCountable } = require('./grpc-fixture')

// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/v5.0.0/examples/src/grpcjs/client.ts
const service = (call, callback) => {
Expand Down Expand Up @@ -53,37 +52,6 @@ function agentInfo() {
})
}

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

sendAgentInfo(agentInfo, callArguments) {
increaseCallCount()
super.sendAgentInfo(agentInfo, callArguments)
}

sendApiMetaInfo(apiMetaInfo, callArguments) {
increaseCallCount()
super.sendApiMetaInfo(apiMetaInfo, callArguments)
}

sendStringMetaInfo(stringMetaInfo, callArguments) {
increaseCallCount()
super.sendStringMetaInfo(stringMetaInfo, callArguments)
}

sendSqlMetaInfo(sqlMetaData, callback) {
increaseCallCount()
super.sendSqlMetaInfo(sqlMetaData, callback)
}

sendSqlUidMetaData(sqlMetaData, callback) {
increaseCallCount()
super.sendSqlUidMetaData(sqlMetaData, callback)
}
}

let agentInfoRefreshInterval
class AgentInfoOnlyDataSource extends DataSourceCallCountable {
constructor(collectorIp, collectorTcpPort, collectorStatPort, collectorSpanPort, agentInfo, config) {
Expand Down

0 comments on commit fa311e2

Please sign in to comment.