10BC0 Socket.IO + Nest.JS's `platform-socket.io` integration doesn't work · Issue #3070 · open-telemetry/opentelemetry-js-contrib · GitHub
[go: up one dir, main page]

Skip to content

Socket.IO + Nest.JS's platform-socket.io integration doesn't work #3070

@ValeriyMaslenikov

Description

@ValeriyMaslenikov

What version of OpenTelemetry are you using?

    "@opentelemetry/api": "^1.9.0",
    "@opentelemetry/auto-instrumentations-node": "^0.64.1",
    "@opentelemetry/exporter-metrics-otlp-grpc": "^0.205.0",
    "@opentelemetry/exporter-trace-otlp-grpc": "^0.205.0",
    "@opentelemetry/instrumentation-socket.io": "^0.52.0",
    "@opentelemetry/resources": "^2.1.0",
    "@opentelemetry/sdk-metrics": "^2.1.0",
    "@opentelemetry/sdk-node": "^0.205.0",
    "@opentelemetry/sdk-trace-node": "^2.1.0",
    "@opentelemetry/semantic-conventions": "^1.37.0",

What version of Node are you using?

v23.11.0

What did you do?

I have nestjs-pino installed.

instrumentation.ts:

import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
...

const sdk = new NodeSDK({
 ...,
 instrumentations: [
   getNodeAutoInstrumentations()
 ],
});

sdk.start();

WS Gateway:

@WebSocketGateway()
export class TestWebSocketGateway {
  private readonly logger = new Logger(TestWebSocketGateway.name);
  @SubscribeMessage('test-socket-io-event-name')
  async handleGetGameInfo(): Promise<WsResponseDto<GameInfoDto>> {
    this.logger.log('Test event');
  }
}

What did you expect to see?

I expected to have the trace_id/span_id logged with Test event message, in the way how it works when socket.io library is used with .on('event-name', cb) .

What did you see instead?

I see the log logged, without any additional context from the OTLP instrumentation.

Additional context

Looks like the problem is related to how the Nest.JS subscribes on the socket.io listener.
It uses the rxjs fromEvent(socket, message) to do that.

Under the hood that function tries to "guess" which methods are available on the target object to register the "listener". Based on my observations for Nest.JS it goes to the branch of "node style event emitter":

Image

which means that the condition on whether methods addListener/removeListener are present on socket.io "Socket" instance.

So eventually it uses the addListener and removeListener methods.
Based on the source code it looks like "@opentelemetry/instrumentation-socket.io" patches only on and emit methods.

Also, they define both on and addListener in their separate component emitter library: https://github.com/socketio/emitter/blob/main/lib/esm/index.js#L35C1-L42C1

Tip: React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpkg:instrumentation-socket.iopriority:p2Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0