DEV Community

Cover image for Instrument your node.js socket.io apps with OpenTelemetry like a PRO ๐Ÿ› ๏ธ
Motti Bechhofer for Aspecto

Posted on • Edited on • Originally published at aspecto.io

Instrument your node.js socket.io apps with OpenTelemetry like a PRO ๐Ÿ› ๏ธ

Today we released a first-of-its-kind open-telemetry auto instrumentation plugin.
A plugin for socket.io instrumentation.

What makes this instrumentation different?

Well, first, it's the first full instrumentation for socket.io ever written.
Second, there are no open-telemetry 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) and 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 open telemetry in your app

npm install \
  @opentelemetry/core \
  @opentelemetry/node \
  @opentelemetry/tracing
Enter fullscreen mode Exit fullscreen mode

Now install our socket.io plugin.

npm i opentelemetry-instrumentation-socket.io
Enter fullscreen mode Exit fullscreen mode

Now, this is the most essential part. Since the plugin patches socket.io, you must init 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

import { NodeTracerProvider } from "@opentelemetry/node";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { SocketIoInstrumentation } from "opentelemetry-instrumentation-socket.io"; 
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/tracing";
Enter fullscreen mode Exit fullscreen mode

and to the exciting part, register our socket.io plugin

const provider = new NodeTracerProvider();
registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [new SocketIoInstrumentation()],
});
Enter fullscreen mode Exit fullscreen mode

and some bla bla code

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
Enter fullscreen mode Exit fullscreen mode

And we are done!

The complete initialization code should look like this.

import { NodeTracerProvider } from "@opentelemetry/node";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { SocketIoInstrumentation } from "opentelemetry-instrumentation-socket.io";
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/tracing";

const provider = new NodeTracerProvider();
registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [new SocketIoInstrumentation()],
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
Enter fullscreen mode Exit fullscreen mode

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

Here 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: []
}
Enter fullscreen mode Exit fullscreen mode

You can find the source code on GitHub

Isn't that cool? ๐Ÿ˜Ž

Hereโ€™s how we visualize Socket.io traces in Aspecto ๐Ÿคฏ

aspecto platform screenshot

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.

Top comments (0)