Skip to content

Commit e40c4d5

Browse files
committed
instr-cassandra-driver test fixes: net.peer.{name,port} attributes appear on some spans where they did not before, presumably because AsyncLocalStorage is doing a better job tracing the async context
1 parent cb29816 commit e40c4d5

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {
3737
SEMATTRS_EXCEPTION_MESSAGE,
3838
SEMATTRS_EXCEPTION_STACKTRACE,
3939
SEMATTRS_EXCEPTION_TYPE,
40+
SEMATTRS_NET_PEER_NAME,
41+
SEMATTRS_NET_PEER_PORT,
4042
} from '@opentelemetry/semantic-conventions';
4143
import * as assert from 'assert';
4244
import * as testUtils from '@opentelemetry/contrib-test-utils';
@@ -57,6 +59,10 @@ const testCassandra = process.env.RUN_CASSANDRA_TESTS;
5759
const testCassandraLocally = process.env.RUN_CASSANDRA_TESTS_LOCAL;
5860
const shouldTest = testCassandra || testCassandraLocally;
5961
const cassandraTimeoutMs = 60000;
62+
const cassandraContactPoint =
63+
process.env.CASSANDRA_HOST ?? testCassandraLocally
64+
? '127.0.0.1'
65+
: 'cassandra';
6066

6167
function assertSpan(
6268
span: ReadableSpan,
@@ -80,11 +86,16 @@ function assertSpan(
8086
testUtils.assertSpan(span, SpanKind.CLIENT, attributes, [], spanStatus);
8187
}
8288

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+
) {
8495
const spans = memoryExporter.getFinishedSpans();
8596
assert.strictEqual(spans.length, 1);
8697
const [span] = spans;
87-
assertSpan(span, name, query, status);
98+
assertSpan(span, name, query, status, customAttributes);
8899
}
89100

90101
function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
@@ -97,7 +108,8 @@ function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
97108
function assertErrorSpan(
98109
name: string,
99110
error: Error & { code?: number },
100-
query?: string
111+
query?: string,
112+
customAttributes?: Attributes
101113
) {
102114
const spans = memoryExporter.getFinishedSpans();
103115
assert.strictEqual(spans.length, 1);
@@ -106,6 +118,7 @@ function assertErrorSpan(
106118
const attributes: Attributes = {
107119
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
108120
[SEMATTRS_DB_USER]: 'cassandra',
121+
...customAttributes,
109122
};
110123

111124
if (query !== undefined) {
@@ -154,12 +167,8 @@ describe('CassandraDriverInstrumentation', () => {
154167
instrumentation.setTracerProvider(provider);
155168

156169
const cassandra = require('cassandra-driver');
157-
const endpoint =
158-
process.env.CASSANDRA_HOST ?? testCassandraLocally
159-
? '127.0.0.1'
160-
: 'cassandra';
161170
client = new cassandra.Client({
162-
contactPoints: [endpoint],
171+
contactPoints: [cassandraContactPoint],
163172
localDataCenter: 'datacenter1',
164173
credentials: {
165174
username: 'cassandra',
@@ -198,12 +207,18 @@ describe('CassandraDriverInstrumentation', () => {
198207

199208
it('creates a span for promise based execute', async () => {
200209
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+
});
202214
});
203215

204216
it('creates a span for callback based execute', done => {
205217
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+
});
207222
done();
208223
});
209224
});
@@ -212,7 +227,10 @@ describe('CassandraDriverInstrumentation', () => {
212227
try {
213228
await client.execute('selec * from');
214229
} 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+
});
216234
return;
217235
}
218236

@@ -239,13 +257,31 @@ describe('CassandraDriverInstrumentation', () => {
239257
it('retains statements', async () => {
240258
const query = 'select * from ot.test';
241259
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+
});
243264
});
244265

245266
it('truncates long queries', async () => {
246267
const query = 'select userid, count from ot.test';
247268
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+
);
249285
});
250286
});
251287

@@ -278,6 +314,8 @@ describe('CassandraDriverInstrumentation', () => {
278314
assertAttributeInSingleSpan('cassandra-driver.execute', {
279315
[customAttributeName]: customAttributeValue,
280316
[responseAttributeName]: 2,
317+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
318+
[SEMATTRS_NET_PEER_PORT]: 9042,
281319
});
282320
});
283321

@@ -299,6 +337,8 @@ describe('CassandraDriverInstrumentation', () => {
299337

300338
assertAttributeInSingleSpan('cassandra-driver.execute', {
301339
[hookAttributeName]: hookAttributeValue,
340+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
341+
[SEMATTRS_NET_PEER_PORT]: 9042,
302342
});
303343
});
304344
});
@@ -320,7 +360,10 @@ describe('CassandraDriverInstrumentation', () => {
320360

321361
it('creates a span for callback based batch', done => {
322362
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+
});
324367
done();
325368
});
326369
});
@@ -370,7 +413,10 @@ describe('CassandraDriverInstrumentation', () => {
370413
const spans = memoryExporter.getFinishedSpans();
371414
// stream internally uses execute
372415
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+
});
374420
assertSpan(spans[1], 'cassandra-driver.stream');
375421
}
376422

0 commit comments

Comments
 (0)