@@ -37,6 +37,8 @@ import {
37
37
SEMATTRS_EXCEPTION_MESSAGE ,
38
38
SEMATTRS_EXCEPTION_STACKTRACE ,
39
39
SEMATTRS_EXCEPTION_TYPE ,
40
+ SEMATTRS_NET_PEER_NAME ,
41
+ SEMATTRS_NET_PEER_PORT ,
40
42
} from '@opentelemetry/semantic-conventions' ;
41
43
import * as assert from 'assert' ;
42
44
import * as testUtils from '@opentelemetry/contrib-test-utils' ;
@@ -57,6 +59,10 @@ const testCassandra = process.env.RUN_CASSANDRA_TESTS;
57
59
const testCassandraLocally = process . env . RUN_CASSANDRA_TESTS_LOCAL ;
58
60
const shouldTest = testCassandra || testCassandraLocally ;
59
61
const cassandraTimeoutMs = 60000 ;
62
+ const cassandraContactPoint =
63
+ process . env . CASSANDRA_HOST ?? testCassandraLocally
64
+ ? '127.0.0.1'
65
+ : 'cassandra' ;
60
66
61
67
function assertSpan (
62
68
span : ReadableSpan ,
@@ -80,11 +86,16 @@ function assertSpan(
80
86
testUtils . assertSpan ( span , SpanKind . CLIENT , attributes , [ ] , spanStatus ) ;
81
87
}
82
88
83
- function assertSingleSpan ( name : string , query ?: string , status ?: SpanStatus ) {
89
+ function assertSingleSpan (
90
+ name : string ,
91
+ query ?: string ,
92
+ status ?: SpanStatus ,
93
+ customAttributes ?: Attributes
94
+ ) {
84
95
const spans = memoryExporter . getFinishedSpans ( ) ;
85
96
assert . strictEqual ( spans . length , 1 ) ;
86
97
const [ span ] = spans ;
87
- assertSpan ( span , name , query , status ) ;
98
+ assertSpan ( span , name , query , status , customAttributes ) ;
88
99
}
89
100
90
101
function assertAttributeInSingleSpan ( name : string , attributes ?: Attributes ) {
@@ -97,7 +108,8 @@ function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
97
108
function assertErrorSpan (
98
109
name : string ,
99
110
error : Error & { code ?: number } ,
100
- query ?: string
111
+ query ?: string ,
112
+ customAttributes ?: Attributes
101
113
) {
102
114
const spans = memoryExporter . getFinishedSpans ( ) ;
103
115
assert . strictEqual ( spans . length , 1 ) ;
@@ -106,6 +118,7 @@ function assertErrorSpan(
106
118
const attributes : Attributes = {
107
119
[ SEMATTRS_DB_SYSTEM ] : DBSYSTEMVALUES_CASSANDRA ,
108
120
[ SEMATTRS_DB_USER ] : 'cassandra' ,
121
+ ...customAttributes ,
109
122
} ;
110
123
111
124
if ( query !== undefined ) {
@@ -154,12 +167,8 @@ describe('CassandraDriverInstrumentation', () => {
154
167
instrumentation . setTracerProvider ( provider ) ;
155
168
156
169
const cassandra = require ( 'cassandra-driver' ) ;
157
- const endpoint =
158
- process . env . CASSANDRA_HOST ?? testCassandraLocally
159
- ? '127.0.0.1'
160
- : 'cassandra' ;
161
170
client = new cassandra . Client ( {
162
- contactPoints : [ endpoint ] ,
171
+ contactPoints : [ cassandraContactPoint ] ,
163
172
localDataCenter : 'datacenter1' ,
164
173
credentials : {
165
174
username : 'cassandra' ,
@@ -198,12 +207,18 @@ describe('CassandraDriverInstrumentation', () => {
198
207
199
208
it ( 'creates a span for promise based execute' , async ( ) => {
200
209
await client . execute ( 'select * from ot.test' ) ;
201
- assertSingleSpan ( 'cassandra-driver.execute' ) ;
210
+ assertSingleSpan ( 'cassandra-driver.execute' , undefined , undefined , {
211
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
212
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
213
+ } ) ;
202
214
} ) ;
203
215
204
216
it ( 'creates a span for callback based execute' , done => {
205
217
client . execute ( 'select * from ot.test' , ( ) => {
206
- assertSingleSpan ( 'cassandra-driver.execute' ) ;
218
+ assertSingleSpan ( 'cassandra-driver.execute' , undefined , undefined , {
219
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
220
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
221
+ } ) ;
207
222
done ( ) ;
208
223
} ) ;
209
224
} ) ;
@@ -212,7 +227,10 @@ describe('CassandraDriverInstrumentation', () => {
212
227
try {
213
228
await client . execute ( 'selec * from' ) ;
214
229
} catch ( e : any ) {
215
- assertErrorSpan ( 'cassandra-driver.execute' , e ) ;
230
+ assertErrorSpan ( 'cassandra-driver.execute' , e , undefined , {
231
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
232
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
233
+ } ) ;
216
234
return ;
217
235
}
218
236
@@ -239,13 +257,31 @@ describe('CassandraDriverInstrumentation', () => {
239
257
it ( 'retains statements' , async ( ) => {
240
258
const query = 'select * from ot.test' ;
241
259
await client . execute ( query ) ;
242
- assertSingleSpan ( 'cassandra-driver.execute' , query ) ;
260
+ assertSingleSpan ( 'cassandra-driver.execute' , query , undefined , {
261
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
262
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
263
+ } ) ;
243
264
} ) ;
244
265
245
266
it ( 'truncates long queries' , async ( ) => {
246
267
const query = 'select userid, count from ot.test' ;
247
268
await client . execute ( query ) ;
248
- assertSingleSpan ( 'cassandra-driver.execute' , query . substring ( 0 , 25 ) ) ;
269
+ const customAttributes = {
270
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
271
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
272
+ } ;
273
+ assertSingleSpan (
274
+ 'cassandra-driver.execute' ,
275
+ query . substring ( 0 , 25 ) ,
276
+ undefined ,
277
+ customAttributes
278
+ ) ;
279
+ assertSingleSpan (
280
+ 'cassandra-driver.execute' ,
281
+ query . substring ( 0 , 25 ) ,
282
+ undefined ,
283
+ customAttributes
284
+ ) ;
249
285
} ) ;
250
286
} ) ;
251
287
@@ -278,6 +314,8 @@ describe('CassandraDriverInstrumentation', () => {
278
314
assertAttributeInSingleSpan ( 'cassandra-driver.execute' , {
279
315
[ customAttributeName ] : customAttributeValue ,
280
316
[ responseAttributeName ] : 2 ,
317
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
318
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
281
319
} ) ;
282
320
} ) ;
283
321
@@ -299,6 +337,8 @@ describe('CassandraDriverInstrumentation', () => {
299
337
300
338
assertAttributeInSingleSpan ( 'cassandra-driver.execute' , {
301
339
[ hookAttributeName ] : hookAttributeValue ,
340
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
341
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
302
342
} ) ;
303
343
} ) ;
304
344
} ) ;
@@ -320,7 +360,10 @@ describe('CassandraDriverInstrumentation', () => {
320
360
321
361
it ( 'creates a span for callback based batch' , done => {
322
362
client . batch ( [ q1 , q2 ] , ( ) => {
323
- assertSingleSpan ( 'cassandra-driver.batch' ) ;
363
+ assertSingleSpan ( 'cassandra-driver.batch' , undefined , undefined , {
364
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
365
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
366
+ } ) ;
324
367
done ( ) ;
325
368
} ) ;
326
369
} ) ;
@@ -370,7 +413,10 @@ describe('CassandraDriverInstrumentation', () => {
370
413
const spans = memoryExporter . getFinishedSpans ( ) ;
371
414
// stream internally uses execute
372
415
assert . strictEqual ( spans . length , 2 ) ;
373
- assertSpan ( spans [ 0 ] , 'cassandra-driver.execute' ) ;
416
+ assertSpan ( spans [ 0 ] , 'cassandra-driver.execute' , undefined , undefined , {
417
+ [ SEMATTRS_NET_PEER_NAME ] : cassandraContactPoint ,
418
+ [ SEMATTRS_NET_PEER_PORT ] : 9042 ,
419
+ } ) ;
374
420
assertSpan ( spans [ 1 ] , 'cassandra-driver.stream' ) ;
375
421
}
376
422
0 commit comments