Instrument your Node.js Socket.io apps with OpenTelemetry like a PRO šŸ› ļø

Motti Bechhofer
Aspecto
Published in
3 min readJun 3, 2021

Today we released a first-of-its-kind OpenTelemetry auto instrumentation plugin.

An OpenTelemetry Socket.io Instrumentation for Node.js.

What makes this instrumentation different?

Well, first, itā€™s the first full instrumentation for Socket.io ever written.
Second, there are no OpenTelemetry specs for Socket.io or WebSocket; usually, at Aspecto, we try to make our instrumentation as close to the specs as possible, but in this case, we had to improvise.

This is what we choose to do

It seemed reasonable to categorized Socket.io under the messaging specs since we have a producer (emit) a receiver (on) and a message.

Having looked at how other messaging systems with unique characteristics like Kafka do it, we added some custom attributes to the Socket.io traces:
messaging.socket.io.event_name with the event name
messaging.socket.io.rooms with an array of socket.io rooms
messaging.socket.io.namespace with the namespace

So what do you need to do?

First, create a Node.js socket.io app (need one? we got you)

Now for the fun stuff.
Install OpenTelemetry in your app

npm install \
@opentelemetry/core \
@opentelemetry/node \
@opentelemetry/tracing

Then install our Socket.io instrumentation.

npm install opentelemetry-instrumentation-socket.io

Now, this is the most essential part. Since the plugin patches Socket.io, you must initialize it before any module requires it, so weā€™re going to put our initialization code at the top of the `index.ts` file.

First, letā€™s import the files. It should look something like this:

And to the exciting part, register our Socket.io instrumentation:

And we are done!

The complete initialization code should look like this:

Now you can run the app and see the traces in the console thanks to the ConsoleSpanExporter.

Hereā€™s is an example trace from stocker.


{
traceId: ā€˜e05c479e3d3c7980a1290ef8a8c1d669ā€™,
parentId: undefined,
name: ā€˜/[AAPL] sendā€™,
id: ā€˜327a29bf1d58469cā€™,
kind: 3,
timestamp: 1622705582441890,
duration: 428,
attributes: {
ā€˜messaging.systemā€™: ā€˜socket.ioā€™,
ā€˜messaging.destination_kindā€™: ā€˜topicā€™,
ā€˜messaging.socket.io.event_nameā€™: ā€˜price-updateā€™,
ā€˜messaging.socket.io.roomsā€™: [ ā€˜AAPLā€™ ],
ā€˜messaging.socket.io.namespaceā€™: ā€˜/ā€™,
ā€˜messaging.destinationā€™: ā€˜/ā€™
},
status: { code: 0 },
events: []
}

You can find the source code on GitHub.

Isnā€™t that cool? šŸ˜Ž

Hereā€™s how we visualize Socket.io traces in Aspecto šŸ¤Æ

Check out our other awesome instrumentations made with ā¤ļø including Kafka and aws-sdk instrumentations.

Feel free to reach out to us with any feedback āœŒļø

About Aspecto

Aspecto is an OpenTelemetry-based troubleshooting platform that helps developers prevent distributed application issues from their local dev environment and across the entire development cycle.

You can think of it as the Chrome DevTools for your distributed applications.

Aspecto is used for detecting and troubleshooting microservices-based distributed systems, and preventing software failures before deployment.

--

--