Skip to content

Commit 86dba74

Browse files
authored
fix: change span names for socket-io (#2495)
1 parent 30e8fc5 commit 86dba74

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

plugins/node/instrumentation-socket.io/src/socket.io.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,9 @@ export class SocketIoInstrumentation extends InstrumentationBase<SocketIoInstrum
292292
}
293293
const wrappedListener = function (this: any, ...args: any[]) {
294294
const eventName = ev;
295-
const defaultNamespace = '/';
296295
const namespace = this.name || this.adapter?.nsp?.name;
297-
const destination =
298-
namespace === defaultNamespace
299-
? eventName
300-
: `${namespace} ${eventName}`;
301296
const span: Span = self.tracer.startSpan(
302-
`${destination} ${MESSAGINGOPERATIONVALUES_RECEIVE}`,
297+
`${MESSAGINGOPERATIONVALUES_RECEIVE} ${namespace}`,
303298
{
304299
kind: SpanKind.CONSUMER,
305300
attributes: {
@@ -393,8 +388,7 @@ export class SocketIoInstrumentation extends InstrumentationBase<SocketIoInstrum
393388
namespace;
394389
attributes[SEMATTRS_MESSAGING_DESTINATION] = namespace;
395390
}
396-
const spanRooms = rooms.length ? `[${rooms.join()}]` : '';
397-
const span = self.tracer.startSpan(`${namespace}${spanRooms} send`, {
391+
const span = self.tracer.startSpan(`send ${namespace}`, {
398392
kind: SpanKind.PRODUCER,
399393
attributes,
400394
});

plugins/node/instrumentation-socket.io/test/socket.io.test.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
io,
4040
getSocketIoSpans,
4141
expectSpan,
42+
expectSpans,
4243
isV2,
4344
} from './utils';
4445

@@ -56,7 +57,7 @@ describe('SocketIoInstrumentation', () => {
5657
it('emit is instrumented', () => {
5758
const io = createServerInstance();
5859
io.emit('test');
59-
expectSpan('/ send', span => {
60+
expectSpan('send /', span => {
6061
expect(span.kind).toEqual(SpanKind.PRODUCER);
6162
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual('socket.io');
6263
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION_KIND]).toEqual(
@@ -87,14 +88,14 @@ describe('SocketIoInstrumentation', () => {
8788
} catch (error) {}
8889
if (isV2) {
8990
// only for v2: connect do not throw, but are just ignored
90-
return expectSpan('/ send', span => {
91+
return expectSpan('send /', span => {
9192
expect(span.kind).toEqual(SpanKind.PRODUCER);
9293
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
9394
'socket.io'
9495
);
9596
});
9697
}
97-
expectSpan('/ send', span => {
98+
expectSpan('send /', span => {
9899
expect(span.status.code).toEqual(SpanStatusCode.ERROR);
99100
expect(span.status.message).toEqual(
100101
'"connect" is a reserved event name'
@@ -105,7 +106,7 @@ describe('SocketIoInstrumentation', () => {
105106
it('send is instrumented', () => {
106107
const io = createServerInstance();
107108
io.send('test');
108-
expectSpan('/ send', span => {
109+
expectSpan('send /', span => {
109110
expect(span.kind).toEqual(SpanKind.PRODUCER);
110111
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual('socket.io');
111112
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION_KIND]).toEqual(
@@ -125,7 +126,7 @@ describe('SocketIoInstrumentation', () => {
125126

126127
const io = createServerInstance();
127128
io.emit('test', 1234);
128-
expectSpan('/ send', span => {
129+
expectSpan('send /', span => {
129130
expect(span.attributes['payload']).toEqual(JSON.stringify([1234]));
130131
});
131132
});
@@ -164,17 +165,22 @@ describe('SocketIoInstrumentation', () => {
164165
socket.on('test_reply', data => {
165166
client.close();
166167
sio.close();
168+
167169
//trace is created after the listener method is completed
168170
setTimeout(() => {
169-
expectSpan(
170-
'test_reply receive',
171-
span => {
171+
expectSpans(
172+
'receive /',
173+
spans => {
172174
try {
173-
expect(span.kind).toEqual(SpanKind.CONSUMER);
174-
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
175-
'socket.io'
176-
);
177-
expect(span.attributes['payload']).toEqual(
175+
expect(spans[0].kind).toEqual(SpanKind.CONSUMER);
176+
expect(
177+
spans[0].attributes[SEMATTRS_MESSAGING_SYSTEM]
178+
).toEqual('socket.io');
179+
expect(spans[1].kind).toEqual(SpanKind.CONSUMER);
180+
expect(
181+
spans[1].attributes[SEMATTRS_MESSAGING_SYSTEM]
182+
).toEqual('socket.io');
183+
expect(spans[1].attributes['payload']).toEqual(
178184
JSON.stringify([data])
179185
);
180186
done();
@@ -207,7 +213,7 @@ describe('SocketIoInstrumentation', () => {
207213
sio.on('connection', () => {
208214
//trace is created after the listener method is completed
209215
setTimeout(() => {
210-
expectSpan('connection receive', span => {
216+
expectSpan('receive /', span => {
211217
expect(span.kind).toEqual(SpanKind.CONSUMER);
212218
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
213219
'socket.io'
@@ -238,7 +244,7 @@ describe('SocketIoInstrumentation', () => {
238244
//trace is created after the listener method is completed
239245
setTimeout(() => {
240246
expectSpan(
241-
'test_reply receive',
247+
'receive /',
242248
span => {
243249
try {
244250
expect(span.kind).toEqual(SpanKind.CONSUMER);
@@ -291,7 +297,7 @@ describe('SocketIoInstrumentation', () => {
291297
const roomName = 'room';
292298
const sio = createServerInstance();
293299
sio.to(roomName).emit('broadcast', '1234');
294-
expectSpan('/[room] send', span => {
300+
expectSpan('send /', span => {
295301
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual('/');
296302
expect(
297303
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS]
@@ -302,7 +308,7 @@ describe('SocketIoInstrumentation', () => {
302308
it('broadcast to multiple rooms', () => {
303309
const sio = createServerInstance();
304310
sio.to('room1').to('room2').emit('broadcast', '1234');
305-
expectSpan('/[room1,room2] send', span => {
311+
expectSpan('send /', span => {
306312
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual('/');
307313
expect(
308314
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS]
@@ -316,7 +322,7 @@ describe('SocketIoInstrumentation', () => {
316322
const io = createServerInstance();
317323
const namespace = io.of('/testing');
318324
namespace.emit('namespace');
319-
expectSpan('/testing send', span => {
325+
expectSpan('send /testing', span => {
320326
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
321327
'/testing'
322328
);
@@ -331,7 +337,7 @@ describe('SocketIoInstrumentation', () => {
331337
const io = createServerInstance();
332338
const namespace = io.of('/testing');
333339
namespace.to(roomName).emit('broadcast', '1234');
334-
expectSpan('/testing[room] send', span => {
340+
expectSpan('send /testing', span => {
335341
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
336342
'/testing'
337343
);
@@ -348,7 +354,7 @@ describe('SocketIoInstrumentation', () => {
348354
const io = createServerInstance();
349355
const namespace = io.of('/testing');
350356
namespace.to('room1').to('room2').emit('broadcast', '1234');
351-
expectSpan('/testing[room1,room2] send', span => {
357+
expectSpan('send /testing', span => {
352358
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
353359
'/testing'
354360
);
@@ -379,7 +385,7 @@ describe('SocketIoInstrumentation', () => {
379385
//trace is created after the listener method is completed
380386
setTimeout(() => {
381387
expectSpan(
382-
'/testing test_reply receive',
388+
'receive /testing',
383389
span => {
384390
try {
385391
expect(span.kind).toEqual(SpanKind.CONSUMER);
@@ -421,7 +427,7 @@ describe('SocketIoInstrumentation', () => {
421427
client.close();
422428
sio.close();
423429
expectSpan(
424-
`/[${socket.id}] send`,
430+
'send /',
425431
span => {
426432
try {
427433
expect(span.kind).toEqual(SpanKind.PRODUCER);

plugins/node/instrumentation-socket.io/test/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ export const expectSpan = (
7373
callback(span);
7474
}
7575
};
76+
77+
export const expectSpans = (
78+
spanNames: string,
79+
callback?: (spans: ReadableSpan[]) => void,
80+
spanCount?: number
81+
) => {
82+
const spans = getSocketIoSpans();
83+
expect(spans.length).toEqual(spanCount || 1);
84+
const foundSpans = spans.filter(span => spanNames.includes(span.name));
85+
expect(foundSpans).toBeDefined();
86+
callback?.(foundSpans);
87+
};

0 commit comments

Comments
 (0)