Instrument your Node.js Socket.io apps with OpenTelemetry like a PRO š ļø
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.