Skip to content

Commit a32c58a

Browse files
committed
[#243] Support Send Messages by local gRPC Fixtures a Server
1 parent 820450e commit a32c58a

File tree

2 files changed

+167
-141
lines changed

2 files changed

+167
-141
lines changed

test/instrumentation/context/nested-async-trace.test.js

Lines changed: 158 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -249,155 +249,172 @@ test(`nested mysql async query with express`, async (t) => {
249249
})
250250
})
251251

252-
test.skip(`nested mysql2 async query with express`, async (t) => {
253-
agent.bindHttpWithCallSite()
254-
const source = path.resolve(fixtures, 'mysql.sql')
255-
const container = await new MySqlContainer()
256-
.withCommand(['--default-authentication-plugin=mysql_native_password'])
257-
.withEnvironment({
258-
'MYSQL_DATABASE': 'test',
259-
'TZ': 'Asia/Seoul',
260-
})
261-
.withCopyFilesToContainer([{
262-
source: source,
263-
target: '/docker-entrypoint-initdb.d/mysql.sql'
264-
}])
265-
.start()
266-
267-
const app = new express()
268-
app.get('/test1', async (req, res) => {
269-
const connection = mysql2.createConnection({
270-
host: container.getHost(),
271-
port: container.getPort(),
272-
database: 'test',
273-
user: container.getUsername(),
274-
password: container.getUserPassword(),
275-
timezone: '+09:00'
276-
})
252+
test(`nested mysql2 async query with express`, async (t) => {
253+
const collectorServer = new grpc.Server()
254+
collectorServer.addService(services.AgentService, {
255+
pingSession: unaryService
256+
})
257+
collectorServer.addService(services.StatService, {
258+
sendAgentStat: unaryService
259+
})
260+
collectorServer.addService(services.SpanService, {
261+
sendSpan: spanMessageStreamService
262+
})
263+
264+
collectorServer.bindAsync('localhost:0', grpc.ServerCredentials.createInsecure(), async (err, port) => {
265+
agent.bindHttpWithCallSite()
266+
const source = path.resolve(fixtures, 'mysql.sql')
267+
const container = await new MySqlContainer()
268+
.withCommand(['--default-authentication-plugin=mysql_native_password'])
269+
.withEnvironment({
270+
'MYSQL_DATABASE': 'test',
271+
'TZ': 'Asia/Seoul',
272+
})
273+
.withCopyFilesToContainer([{
274+
source: source,
275+
target: '/docker-entrypoint-initdb.d/mysql.sql'
276+
}])
277+
.start()
278+
279+
const app = new express()
280+
app.get('/test1', async (req, res) => {
281+
const connection = mysql2.createConnection({
282+
host: container.getHost(),
283+
port: container.getPort(),
284+
database: 'test',
285+
user: container.getUsername(),
286+
password: container.getUserPassword(),
287+
timezone: '+09:00'
288+
})
289+
290+
connection.query(`SELECT * FROM member`, async function (error, results) {
291+
if (error) throw error
292+
t.equal(results[0].id, 'a', 'SELECT member id')
293+
t.equal(results[0].name, 'name1', 'SELECT member name')
294+
t.equal(results[0].joined.getDate(), new Date('2023-01-18T00:00:00+09:00').getDate(), 'SELECT member joined')
295+
296+
connection.query(`SELECT * FROM member WHERE id = ?`, results[0].id, async function (error, results) {
297+
298+
})
299+
})
300+
301+
const [rows] = await connection.promise().query(`SELECT * FROM member WHERE id = ?`, 'a')
302+
t.equal(rows[0].id, 'a', 'id in SELECT query hooking')
303+
t.equal(rows[0].name, 'name1', 'name in SELECT query hooking')
304+
t.equal(rows[0].joined.toISOString().slice(0, 10), '2023-01-17', 'joined in SELECT query hooking')
305+
306+
setTimeout(() => {
307+
res.send('ok get')
308+
}, 1000)
309+
310+
agent.callbackTraceClose(async (trace) => {
311+
let actualBuilder = new MethodDescriptorBuilder(expected('get', 'app.get'))
312+
.setClassName(expected('app', 'Function'))
313+
.setLineNumber(280)
314+
.setFileName('nested-async-trace.test.js')
315+
let actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
316+
let actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 0)
317+
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
318+
t.equal(actualMethodDescriptor.apiDescriptor, expected('app.get', 'Function.app.get'), 'apiDescriptor is equal')
319+
t.equal(actualMethodDescriptor.className, expected('app', 'Function'), 'className is equal')
320+
t.equal(actualMethodDescriptor.methodName, 'get', 'methodName is equal')
321+
t.equal(actualSpanEvent.sequence, 0, 'sequence is 0')
322+
t.equal(actualSpanEvent.depth, 1, 'depth is 0')
323+
t.equal(actualSpanEvent.serviceType, expressServiceType.getCode(), 'serviceType is express')
324+
325+
actualBuilder = new MethodDescriptorBuilder('createConnection')
326+
.setLineNumber(281)
327+
.setFileName('nested-async-trace.test.js')
328+
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
329+
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 1)
330+
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
331+
t.equal(actualSpanEvent.endPoint, 'localhost', 'endPoint is equal')
332+
t.equal(actualSpanEvent.destinationId, 'test', 'destinationId is equal')
333+
t.equal(actualSpanEvent.sequence, 1, 'sequence is 1')
334+
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
335+
t.equal(actualSpanEvent.serviceType, mysqlServiceType.getCode(), 'serviceType is mysql')
277336

278-
connection.query(`SELECT * FROM member`, async function (error, results) {
279-
if (error) throw error
280-
t.equal(results[0].id, 'a', 'SELECT member id')
281-
t.equal(results[0].name, 'name1', 'SELECT member name')
282-
t.equal(results[0].joined.getDate(), new Date('2023-01-18T00:00:00+09:00').getDate(), 'SELECT member joined')
337+
actualBuilder = new MethodDescriptorBuilder('query')
338+
.setClassName('Connection')
339+
.setLineNumber(290)
340+
.setFileName('nested-async-trace.test.js')
341+
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
342+
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 2)
343+
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
344+
t.equal(actualSpanEvent.sequence, 2, 'sequence is 2')
345+
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
346+
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
347+
let actualNextAsyncId = actualSpanEvent.asyncId
348+
349+
let actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
350+
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
351+
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
352+
t.equal(actualSpanChunk.localAsyncId.asyncId, actualSpanEvent.asyncId.asyncId, 'localAsyncId is equal')
353+
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
354+
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
355+
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
356+
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
357+
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
283358

284-
connection.query(`SELECT * FROM member WHERE id = ?`, results[0].id, async function (error, results) {
359+
actualBuilder = new MethodDescriptorBuilder('query')
360+
.setClassName('Connection')
361+
.setLineNumber(296)
362+
.setFileName('nested-async-trace.test.js')
363+
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
364+
actualSpanEvent = actualSpanChunk.spanEventList[1]
365+
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
366+
t.equal(actualSpanEvent.depth, 2, 'depth is 3')
367+
t.equal(actualSpanEvent.sequence, 1, 'sequence is 2')
368+
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
369+
actualNextAsyncId = actualSpanEvent.asyncId
285370

371+
actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
372+
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
373+
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
374+
t.equal(actualSpanChunk.localAsyncId.asyncId, actualNextAsyncId.asyncId, 'localAsyncId is equal')
375+
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
376+
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
377+
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
378+
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
379+
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
380+
381+
actualBuilder = new MethodDescriptorBuilder('query')
382+
.setClassName('Connection')
383+
.setLineNumber(103)
384+
.setFileName('promise.js')
385+
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
386+
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 3)
387+
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
388+
t.equal(actualSpanEvent.sequence, 3, 'sequence is 3')
389+
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
390+
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
391+
actualNextAsyncId = actualSpanEvent.asyncId
392+
393+
actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
394+
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
395+
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
396+
t.equal(actualSpanChunk.localAsyncId.asyncId, actualNextAsyncId.asyncId, 'localAsyncId is equal')
397+
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
398+
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
399+
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
400+
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
401+
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
402+
403+
connection.end()
404+
await container.stop()
286405
})
287406
})
288407

289-
const [rows] = await connection.promise().query(`SELECT * FROM member WHERE id = ?`, 'a')
290-
t.equal(rows[0].id, 'a', 'id in SELECT query hooking')
291-
t.equal(rows[0].name, 'name1', 'name in SELECT query hooking')
292-
t.equal(rows[0].joined.toISOString().slice(0, 10), '2023-01-17', 'joined in SELECT query hooking')
293-
294-
setTimeout(() => {
295-
res.send('ok get')
296-
}, 1000)
297-
298-
agent.callbackTraceClose(async (trace) => {
299-
let actualBuilder = new MethodDescriptorBuilder(expected('get', 'app.get'))
300-
.setClassName(expected('app', 'Function'))
301-
.setLineNumber(218)
302-
.setFileName('nested-async-trace.test.js')
303-
let actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
304-
let actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 0)
305-
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
306-
t.equal(actualMethodDescriptor.apiDescriptor, expected('app.get', 'Function.app.get'), 'apiDescriptor is equal')
307-
t.equal(actualMethodDescriptor.className, expected('app', 'Function'), 'className is equal')
308-
t.equal(actualMethodDescriptor.methodName, 'get', 'methodName is equal')
309-
t.equal(actualSpanEvent.sequence, 0, 'sequence is 0')
310-
t.equal(actualSpanEvent.depth, 1, 'depth is 0')
311-
t.equal(actualSpanEvent.serviceType, expressServiceType.getCode(), 'serviceType is express')
312-
313-
actualBuilder = new MethodDescriptorBuilder('createConnection')
314-
.setLineNumber(219)
315-
.setFileName('nested-async-trace.test.js')
316-
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
317-
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 1)
318-
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
319-
t.equal(actualSpanEvent.endPoint, 'localhost', 'endPoint is equal')
320-
t.equal(actualSpanEvent.destinationId, 'test', 'destinationId is equal')
321-
t.equal(actualSpanEvent.sequence, 1, 'sequence is 1')
322-
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
323-
t.equal(actualSpanEvent.serviceType, mysqlServiceType.getCode(), 'serviceType is mysql')
324-
325-
actualBuilder = new MethodDescriptorBuilder('query')
326-
.setClassName('Connection')
327-
.setLineNumber(228)
328-
.setFileName('nested-async-trace.test.js')
329-
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
330-
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 2)
331-
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
332-
t.equal(actualSpanEvent.sequence, 2, 'sequence is 2')
333-
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
334-
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
335-
let actualNextAsyncId = actualSpanEvent.asyncId
336-
337-
let actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
338-
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
339-
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
340-
t.equal(actualSpanChunk.localAsyncId.asyncId, actualSpanEvent.asyncId.asyncId, 'localAsyncId is equal')
341-
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
342-
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
343-
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
344-
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
345-
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
346-
347-
actualBuilder = new MethodDescriptorBuilder('query')
348-
.setClassName('Connection')
349-
.setLineNumber(234)
350-
.setFileName('nested-async-trace.test.js')
351-
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
352-
actualSpanEvent = actualSpanChunk.spanEventList[1]
353-
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
354-
t.equal(actualSpanEvent.depth, 2, 'depth is 3')
355-
t.equal(actualSpanEvent.sequence, 1, 'sequence is 2')
356-
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
357-
actualNextAsyncId = actualSpanEvent.asyncId
358-
359-
actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
360-
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
361-
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
362-
t.equal(actualSpanChunk.localAsyncId.asyncId, actualNextAsyncId.asyncId, 'localAsyncId is equal')
363-
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
364-
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
365-
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
366-
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
367-
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
368-
369-
actualBuilder = new MethodDescriptorBuilder('query')
370-
.setClassName('Connection')
371-
.setLineNumber(103)
372-
.setFileName('promise.js')
373-
actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
374-
actualSpanEvent = trace.spanBuilder.spanEventList.find(spanEvent => spanEvent.sequence === 3)
375-
t.equal(actualMethodDescriptor.apiId, actualSpanEvent.apiId, 'apiId is equal')
376-
t.equal(actualSpanEvent.sequence, 3, 'sequence is 3')
377-
t.equal(actualSpanEvent.depth, 2, 'depth is 2')
378-
t.equal(actualSpanEvent.serviceType, mysqlExecuteQueryServiceType.getCode(), 'serviceType is mysql')
379-
actualNextAsyncId = actualSpanEvent.asyncId
380-
381-
actualSpanChunk = trace.repository.dataSender.findSpanChunk(actualNextAsyncId)
382-
t.equal(actualSpanChunk.spanId, actualSpanEvent.spanId, 'spanId is equal')
383-
t.equal(actualSpanChunk.getTraceRoot().getTransactionId(), trace.spanBuilder.getTraceRoot().getTransactionId(), 'transactionIdObject is equal')
384-
t.equal(actualSpanChunk.localAsyncId.asyncId, actualNextAsyncId.asyncId, 'localAsyncId is equal')
385-
t.equal(actualSpanChunk.localAsyncId.sequence, 1, 'localAsyncId.sequence is equal')
386-
t.equal(actualSpanChunk.spanEventList[0].apiId, defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.apiId, 'apiId is equal')
387-
t.equal(actualSpanChunk.spanEventList[0].depth, 1, 'depth is equal')
388-
t.equal(actualSpanChunk.spanEventList[0].sequence, 0, 'sequence is equal')
389-
t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql')
390-
391-
connection.end()
392-
await container.stop()
408+
const server = app.listen(5006, async () => {
409+
const result = await axios.get('http://localhost:5006/test1')
410+
t.equal(result.status, 200, 'status is 200')
411+
412+
t.end()
413+
server.close()
393414
})
394415
})
395416

396-
const server = app.listen(5006, async () => {
397-
const result = await axios.get('http://localhost:5006/test1')
398-
t.equal(result.status, 200, 'status is 200')
399-
400-
t.end()
401-
server.close()
417+
t.teardown(() => {
418+
collectorServer.forceShutdown()
402419
})
403420
})

0 commit comments

Comments
 (0)