socket-client-opentelemetry-demo
v1.0.0-beta.5
Published
OpenTelemetry Socket.io Client wrapper for NestJS
Downloads
28
Readme
Socket Client OpenTelemetry PoC
Description
This project demonstrates a proof-of-concept (PoC) for a Socket.IO client with integrated OpenTelemetry tracing, designed for use with NestJS applications. It provides a reusable client module and service to connect to Socket.IO servers, emit and receive events, and automatically instrument socket communications for distributed tracing and observability.
Features
- Socket.IO Client: Connects to Socket.IO servers with customizable options.
- OpenTelemetry Integration: Automatically traces socket events and communications.
- NestJS Module: Easy integration into NestJS projects.
- Customizable Headers: Supports sending custom headers (e.g., authentication, device ID).
- Service Injection: Use the client via dependency injection in your services.
Installation
Install dependencies:
npm installUsage
Register the Module
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SocketOtelClientModule } from './lib/socket-otel-client.module';
@Module({
imports: [
SocketOtelClientModule.register({
url: 'http://localhost:3000',
socketOptions: {
extraHeaders: {
key: 'your-device-key',
authorization: 'Bearer <your-token>',
},
},
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}Use in a Service
import { Injectable } from '@nestjs/common';
import { SocketOtelClientService } from './lib/socket-otel-client.service';
@Injectable()
export class AppService {
constructor(private readonly socketClientService: SocketOtelClientService) {}
sendMessageToServer() {
this.socketClientService.emit('messageToServer', { msg: 'Hello from NestJS client!' });
}
getSocket() {
return this.socketClientService.getSocket();
}
}Running the App
npm run startExample
- Connects to a Socket.IO server at
http://localhost:3000 - Emits a message with tracing context
- Receives and logs responses with OpenTelemetry instrumentation
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
MIT
