1
1
'use strict'
2
2
3
+ const { randomUUID } = require ( 'crypto' )
3
4
const { expect } = require ( 'chai' )
4
5
const agent = require ( '../../dd-trace/test/plugins/agent' )
5
6
const { expectSomeSpan, withDefaults } = require ( '../../dd-trace/test/plugins/helpers' )
@@ -10,14 +11,12 @@ const DataStreamsContext = require('../../dd-trace/src/datastreams/context')
10
11
const { computePathwayHash } = require ( '../../dd-trace/src/datastreams/pathway' )
11
12
const { ENTRY_PARENT_HASH , DataStreamsProcessor } = require ( '../../dd-trace/src/datastreams/processor' )
12
13
13
- const testTopic = 'test-topic'
14
-
15
- const getDsmPathwayHash = ( isProducer , parentHash ) => {
14
+ const getDsmPathwayHash = ( testTopic , isProducer , parentHash ) => {
16
15
let edgeTags
17
16
if ( isProducer ) {
18
17
edgeTags = [ 'direction:out' , 'topic:' + testTopic , 'type:kafka' ]
19
18
} else {
20
- edgeTags = [ 'direction:in' , 'group:test-group' , 'topic:' + testTopic , 'type:kafka' ]
19
+ edgeTags = [ 'direction:in' , 'group:test-group-confluent ' , 'topic:' + testTopic , 'type:kafka' ]
21
20
}
22
21
23
22
edgeTags . sort ( )
@@ -26,6 +25,7 @@ const getDsmPathwayHash = (isProducer, parentHash) => {
26
25
27
26
describe ( 'Plugin' , ( ) => {
28
27
const module = '@confluentinc/kafka-javascript'
28
+ const groupId = 'test-group-confluent'
29
29
30
30
describe ( 'confluentinc-kafka-javascript' , function ( ) {
31
31
this . timeout ( 30000 )
@@ -36,11 +36,13 @@ describe('Plugin', () => {
36
36
37
37
withVersions ( 'confluentinc-kafka-javascript' , module , ( version ) => {
38
38
let kafka
39
+ let admin
39
40
let tracer
40
41
let Kafka
41
42
let ConfluentKafka
42
43
let messages
43
44
let nativeApi
45
+ let testTopic
44
46
45
47
describe ( 'without configuration' , ( ) => {
46
48
beforeEach ( async ( ) => {
@@ -60,11 +62,24 @@ describe('Plugin', () => {
60
62
kafka = new Kafka ( {
61
63
kafkaJS : {
62
64
clientId : `kafkajs-test-${ version } ` ,
63
- brokers : [ '127.0.0.1:9092' ]
65
+ brokers : [ '127.0.0.1:9092' ] ,
66
+ logLevel : ConfluentKafka . logLevel . WARN
64
67
}
65
68
} )
69
+ testTopic = `test-topic-${ randomUUID ( ) } `
70
+ admin = kafka . admin ( )
71
+ await admin . connect ( )
72
+ await admin . createTopics ( {
73
+ topics : [ {
74
+ topic : testTopic ,
75
+ numPartitions : 1 ,
76
+ replicationFactor : 1
77
+ } ]
78
+ } )
66
79
} )
67
80
81
+ afterEach ( ( ) => admin . disconnect ( ) )
82
+
68
83
describe ( 'kafkaJS api' , ( ) => {
69
84
describe ( 'producer' , ( ) => {
70
85
it ( 'should be instrumented' , async ( ) => {
@@ -74,7 +89,7 @@ describe('Plugin', () => {
74
89
meta : {
75
90
'span.kind' : 'producer' ,
76
91
component : 'confluentinc-kafka-javascript' ,
77
- 'messaging.destination.name' : 'test-topic' ,
92
+ 'messaging.destination.name' : testTopic ,
78
93
'messaging.kafka.bootstrap.servers' : '127.0.0.1:9092'
79
94
} ,
80
95
metrics : {
@@ -125,7 +140,7 @@ describe('Plugin', () => {
125
140
beforeEach ( async ( ) => {
126
141
messages = [ { key : 'key1' , value : 'test2' } ]
127
142
consumer = kafka . consumer ( {
128
- kafkaJS : { groupId : 'test-group' }
143
+ kafkaJS : { groupId, fromBeginning : true , autoCommit : false }
129
144
} )
130
145
await consumer . connect ( )
131
146
await consumer . subscribe ( { topic : testTopic } )
@@ -142,7 +157,7 @@ describe('Plugin', () => {
142
157
meta : {
143
158
'span.kind' : 'consumer' ,
144
159
component : 'confluentinc-kafka-javascript' ,
145
- 'messaging.destination.name' : 'test-topic'
160
+ 'messaging.destination.name' : testTopic
146
161
} ,
147
162
resource : testTopic ,
148
163
error : 0 ,
@@ -151,7 +166,7 @@ describe('Plugin', () => {
151
166
152
167
const consumerReceiveMessagePromise = new Promise ( resolve => {
153
168
consumer . run ( {
154
- eachMessage : async ( ) => {
169
+ eachMessage : ( ) => {
155
170
resolve ( )
156
171
}
157
172
} )
@@ -221,7 +236,7 @@ describe('Plugin', () => {
221
236
[ ERROR_STACK ] : fakeError . stack ,
222
237
'span.kind' : 'consumer' ,
223
238
component : 'confluentinc-kafka-javascript' ,
224
- 'messaging.destination.name' : 'test-topic'
239
+ 'messaging.destination.name' : testTopic
225
240
} ,
226
241
resource : testTopic ,
227
242
error : 1 ,
@@ -344,7 +359,10 @@ describe('Plugin', () => {
344
359
beforeEach ( async ( ) => {
345
360
nativeConsumer = new Consumer ( {
346
361
'bootstrap.servers' : '127.0.0.1:9092' ,
347
- 'group.id' : 'test-group'
362
+ 'group.id' : groupId ,
363
+ 'enable.auto.commit' : false ,
364
+ } , {
365
+ 'auto.offset.reset' : 'earliest'
348
366
} )
349
367
350
368
await new Promise ( ( resolve , reject ) => {
@@ -491,15 +509,15 @@ describe('Plugin', () => {
491
509
tracer . use ( 'confluentinc-kafka-javascript' , { dsmEnabled : true } )
492
510
messages = [ { key : 'key1' , value : 'test2' } ]
493
511
consumer = kafka . consumer ( {
494
- kafkaJS : { groupId : 'test-group' , fromBeginning : false }
512
+ kafkaJS : { groupId, fromBeginning : true }
495
513
} )
496
514
await consumer . connect ( )
497
515
await consumer . subscribe ( { topic : testTopic } )
498
516
} )
499
517
500
- before ( ( ) => {
501
- expectedProducerHash = getDsmPathwayHash ( true , ENTRY_PARENT_HASH )
502
- expectedConsumerHash = getDsmPathwayHash ( false , expectedProducerHash )
518
+ beforeEach ( ( ) => {
519
+ expectedProducerHash = getDsmPathwayHash ( testTopic , true , ENTRY_PARENT_HASH )
520
+ expectedConsumerHash = getDsmPathwayHash ( testTopic , false , expectedProducerHash )
503
521
} )
504
522
505
523
afterEach ( async ( ) => {
@@ -617,24 +635,22 @@ describe('Plugin', () => {
617
635
partition,
618
636
offset : Number ( message . offset )
619
637
}
620
- // Signal that we've processed a message
621
638
messageProcessedResolve ( )
622
639
}
623
640
} )
624
641
625
- consumerRunPromise . catch ( ( ) => { } )
642
+ await consumerRunPromise
626
643
627
644
// wait for the message to be processed before continuing
628
- await sendMessages ( kafka , testTopic , messages ) . then (
629
- async ( ) => await messageProcessedPromise
630
- )
645
+ await sendMessages ( kafka , testTopic , messages )
646
+ await messageProcessedPromise
631
647
632
648
for ( const call of setOffsetSpy . getCalls ( ) ) {
633
649
expect ( call . args [ 0 ] ) . to . not . have . property ( 'type' , 'kafka_commit' )
634
650
}
635
651
636
652
const newConsumer = kafka . consumer ( {
637
- kafkaJS : { groupId : 'test-group' , autoCommit : false }
653
+ kafkaJS : { groupId, fromBeginning : true , autoCommit : false }
638
654
} )
639
655
await newConsumer . connect ( )
640
656
await sendMessages ( kafka , testTopic , [ { key : 'key1' , value : 'test2' } ] )
@@ -648,12 +664,11 @@ describe('Plugin', () => {
648
664
649
665
// Check our work
650
666
const runArg = setOffsetSpy . lastCall . args [ 0 ]
651
- expect ( setOffsetSpy ) . to . be . calledOnce
652
667
expect ( runArg ) . to . have . property ( 'offset' , commitMeta . offset )
653
668
expect ( runArg ) . to . have . property ( 'partition' , commitMeta . partition )
654
669
expect ( runArg ) . to . have . property ( 'topic' , commitMeta . topic )
655
670
expect ( runArg ) . to . have . property ( 'type' , 'kafka_commit' )
656
- expect ( runArg ) . to . have . property ( 'consumer_group' , 'test-group' )
671
+ expect ( runArg ) . to . have . property ( 'consumer_group' , groupId )
657
672
} )
658
673
659
674
it ( 'Should add backlog on producer response' , async ( ) => {
0 commit comments