Skip to content

Commit

Permalink
refactor(instr-socket.io): use exported strings for attributes (#2147)
Browse files Browse the repository at this point in the history
* refactor(instr-socket.io): use exported strings for attributes

* chore(instr-socket.io): fix lint issues

* fix(instr-socket.io): fix missing entries in package-lock.json

* fix(instr-socket.io): fix bad entry in package.json
  • Loading branch information
david-luna committed Apr 29, 2024
1 parent 7822461 commit 56392f4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 59 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions plugins/node/instrumentation-socket.io/README.md
Expand Up @@ -59,6 +59,19 @@ Few breaking changes were made during porting to the contrib repo:
The instrumentation's config `filterHttpTransport` option was removed to decouple this instrumentation from the http instrumentation.
if you do not want to trace the socket.io http requests, add the default socket.io route (`/socket.io/`) to the `HttpInstrumentationConfig.ignoreIncomingPaths` array

## Semantic Conventions

This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)

Attributes collected:

| Attribute | Short Description |
| ---------------------------- | ------------------------------------------------------------------------------------------------ |
| `messaging.destination` | The message destination name. This might be equal to the span name but is required nevertheless. |
| `messaging.destination_kind` | The kind of message destination. |
| `messaging.operation` | A string identifying the kind of message consumption. |
| `messaging.system` | A string identifying the messaging system. |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-socket.io/package.json
Expand Up @@ -59,7 +59,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.51.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-socket.io#readme"
}
27 changes: 15 additions & 12 deletions plugins/node/instrumentation-socket.io/src/socket.io.ts
Expand Up @@ -28,9 +28,12 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import {
SemanticAttributes,
MessagingOperationValues,
MessagingDestinationKindValues,
SEMATTRS_MESSAGING_DESTINATION,
SEMATTRS_MESSAGING_DESTINATION_KIND,
SEMATTRS_MESSAGING_OPERATION,
SEMATTRS_MESSAGING_SYSTEM,
MESSAGINGOPERATIONVALUES_RECEIVE,
MESSAGINGDESTINATIONKINDVALUES_TOPIC,
} from '@opentelemetry/semantic-conventions';
import { SocketIoInstrumentationConfig } from './types';
import { SocketIoInstrumentationAttributes } from './AttributeNames';
Expand Down Expand Up @@ -302,14 +305,14 @@ export class SocketIoInstrumentation extends InstrumentationBase {
? eventName
: `${namespace} ${eventName}`;
const span: Span = self.tracer.startSpan(
`${destination} ${MessagingOperationValues.RECEIVE}`,
`${destination} ${MESSAGINGOPERATIONVALUES_RECEIVE}`,
{
kind: SpanKind.CONSUMER,
attributes: {
[SemanticAttributes.MESSAGING_SYSTEM]: 'socket.io',
[SemanticAttributes.MESSAGING_DESTINATION]: namespace,
[SemanticAttributes.MESSAGING_OPERATION]:
MessagingOperationValues.RECEIVE,
[SEMATTRS_MESSAGING_SYSTEM]: 'socket.io',
[SEMATTRS_MESSAGING_DESTINATION]: namespace,
[SEMATTRS_MESSAGING_OPERATION]:
MESSAGINGOPERATIONVALUES_RECEIVE,
[SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]:
eventName,
},
Expand Down Expand Up @@ -379,9 +382,9 @@ export class SocketIoInstrumentation extends InstrumentationBase {
const messagingSystem = 'socket.io';
const eventName = ev;
const attributes: any = {
[SemanticAttributes.MESSAGING_SYSTEM]: messagingSystem,
[SemanticAttributes.MESSAGING_DESTINATION_KIND]:
MessagingDestinationKindValues.TOPIC,
[SEMATTRS_MESSAGING_SYSTEM]: messagingSystem,
[SEMATTRS_MESSAGING_DESTINATION_KIND]:
MESSAGINGDESTINATIONKINDVALUES_TOPIC,
[SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]: eventName,
};

Expand All @@ -394,7 +397,7 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (namespace) {
attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] =
namespace;
attributes[SemanticAttributes.MESSAGING_DESTINATION] = namespace;
attributes[SEMATTRS_MESSAGING_DESTINATION] = namespace;
}
const spanRooms = rooms.length ? `[${rooms.join()}]` : '';
const span = self.tracer.startSpan(`${namespace}${spanRooms} send`, {
Expand Down
82 changes: 38 additions & 44 deletions plugins/node/instrumentation-socket.io/test/socket.io.test.ts
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/
import {
MessagingDestinationKindValues,
SemanticAttributes,
MESSAGINGDESTINATIONKINDVALUES_TOPIC,
SEMATTRS_MESSAGING_DESTINATION,
SEMATTRS_MESSAGING_DESTINATION_KIND,
SEMATTRS_MESSAGING_SYSTEM,
} from '@opentelemetry/semantic-conventions';
import {
SocketIoInstrumentation,
Expand Down Expand Up @@ -56,12 +58,10 @@ describe('SocketIoInstrumentation', () => {
io.emit('test');
expectSpan('/ send', span => {
expect(span.kind).toEqual(SpanKind.PRODUCER);
expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual(
'socket.io'
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION_KIND]).toEqual(
MESSAGINGDESTINATIONKINDVALUES_TOPIC
);
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND]
).toEqual(MessagingDestinationKindValues.TOPIC);
});
});

Expand Down Expand Up @@ -89,7 +89,7 @@ describe('SocketIoInstrumentation', () => {
// only for v2: connect do not throw, but are just ignored
return expectSpan('/ send', span => {
expect(span.kind).toEqual(SpanKind.PRODUCER);
expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual(
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
});
Expand All @@ -107,12 +107,10 @@ describe('SocketIoInstrumentation', () => {
io.send('test');
expectSpan('/ send', span => {
expect(span.kind).toEqual(SpanKind.PRODUCER);
expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual(
'socket.io'
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION_KIND]).toEqual(
MESSAGINGDESTINATIONKINDVALUES_TOPIC
);
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND]
).toEqual(MessagingDestinationKindValues.TOPIC);
});
});

Expand Down Expand Up @@ -173,9 +171,9 @@ describe('SocketIoInstrumentation', () => {
span => {
try {
expect(span.kind).toEqual(SpanKind.CONSUMER);
expect(
span.attributes[SemanticAttributes.MESSAGING_SYSTEM]
).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
expect(span.attributes['payload']).toEqual(
JSON.stringify([data])
);
Expand Down Expand Up @@ -213,9 +211,9 @@ describe('SocketIoInstrumentation', () => {
setTimeout(() => {
expectSpan('connection receive', span => {
expect(span.kind).toEqual(SpanKind.CONSUMER);
expect(
span.attributes[SemanticAttributes.MESSAGING_SYSTEM]
).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
done();
});
});
Expand Down Expand Up @@ -244,9 +242,9 @@ describe('SocketIoInstrumentation', () => {
span => {
try {
expect(span.kind).toEqual(SpanKind.CONSUMER);
expect(
span.attributes[SemanticAttributes.MESSAGING_SYSTEM]
).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
done();
} catch (e) {
done(e);
Expand Down Expand Up @@ -294,9 +292,7 @@ describe('SocketIoInstrumentation', () => {
const sio = createServerInstance();
sio.to(roomName).emit('broadcast', '1234');
expectSpan('/[room] send', span => {
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toEqual('/');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual('/');
expect(
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS]
).toEqual([roomName]);
Expand All @@ -307,9 +303,7 @@ describe('SocketIoInstrumentation', () => {
const sio = createServerInstance();
sio.to('room1').to('room2').emit('broadcast', '1234');
expectSpan('/[room1,room2] send', span => {
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toEqual('/');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual('/');
expect(
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS]
).toEqual(['room1', 'room2']);
Expand All @@ -323,9 +317,9 @@ describe('SocketIoInstrumentation', () => {
const namespace = io.of('/testing');
namespace.emit('namespace');
expectSpan('/testing send', span => {
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toEqual('/testing');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
'/testing'
);
expect(
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE]
).toEqual('/testing');
Expand All @@ -338,9 +332,9 @@ describe('SocketIoInstrumentation', () => {
const namespace = io.of('/testing');
namespace.to(roomName).emit('broadcast', '1234');
expectSpan('/testing[room] send', span => {
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toEqual('/testing');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
'/testing'
);
expect(
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS]
).toEqual([roomName]);
Expand All @@ -355,9 +349,9 @@ describe('SocketIoInstrumentation', () => {
const namespace = io.of('/testing');
namespace.to('room1').to('room2').emit('broadcast', '1234');
expectSpan('/testing[room1,room2] send', span => {
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toEqual('/testing');
expect(span.attributes[SEMATTRS_MESSAGING_DESTINATION]).toEqual(
'/testing'
);
expect(
span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE]
).toEqual('/testing');
Expand Down Expand Up @@ -389,11 +383,11 @@ describe('SocketIoInstrumentation', () => {
span => {
try {
expect(span.kind).toEqual(SpanKind.CONSUMER);
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
expect(
span.attributes[SemanticAttributes.MESSAGING_SYSTEM]
).toEqual('socket.io');
expect(
span.attributes[SemanticAttributes.MESSAGING_DESTINATION]
span.attributes[SEMATTRS_MESSAGING_DESTINATION]
).toEqual('/testing');
done();
} catch (e) {
Expand Down Expand Up @@ -431,9 +425,9 @@ describe('SocketIoInstrumentation', () => {
span => {
try {
expect(span.kind).toEqual(SpanKind.PRODUCER);
expect(
span.attributes[SemanticAttributes.MESSAGING_SYSTEM]
).toEqual('socket.io');
expect(span.attributes[SEMATTRS_MESSAGING_SYSTEM]).toEqual(
'socket.io'
);
done();
} catch (e) {
done(e);
Expand Down

0 comments on commit 56392f4

Please sign in to comment.