Instrument your node.js socket.io apps with OpenTelemetry like a PRO 🛠️
Tags: socketio, webdev, showdev, opensource
Published: 2021-06-03 11:19:48

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](http://aspecto.io), 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](https://github.com/mottibec/stocker))

now for the fun stuff
install open telemetry in your app
```
npm install \
  @opentelemetry/core \
  @opentelemetry/node \
  @opentelemetry/tracing
```
Now install our socket.io plugin. 
```
npm i opentelemetry-instrumentation-socket.io
```

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";
```

and to the exciting part, register our socket.io plugin
```
const provider = new NodeTracerProvider();
registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [new SocketIoInstrumentation()],
});
```

and some bla bla code
```
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
```

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();
```

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

Here is an example trace from [stocker](https://github.com/mottibec/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](https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-socket.io)

Isn't that cool? 😎

Here’s how we visualize Socket.io traces in [Aspecto](https://www.aspecto.io/?utm_source=dev.to&utm_medium=post&utm_campaign=socket-io-instrumentation) 🤯

![aspecto platform screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx1s9yvqd8hlertg4bcy.png)

Check out our other awesome [instrumentations](https://github.com/aspecto-io/opentelemetry-ext-js) made with ❤️ including [Kafka](https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-kafkajs) and [aws-sdk](https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-aws-sdk) instrumentations.

Feel free to [reach out to us](https://www.aspecto.io/contact-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.