Skip to content

Commit

Permalink
feat(node): Add socket.io integration
Browse files Browse the repository at this point in the history
Add otel integration and e2e tests.

Signed-off-by: Kaung Zin Hein <[email protected]>
  • Loading branch information
Zen-cronic committed Sep 14, 2024
1 parent 8d2e189 commit 4df621b
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 10 deletions.
2 changes: 2 additions & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
"redis-4": "npm:redis@^4.6.14",
"reflect-metadata": "0.2.1",
"rxjs": "^7.8.1",
"socket.io": "^4.7.5",
"socket.io-client": "^4.7.5",
"yargs": "^16.2.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { loggingTransport, sendPortToRunner } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});

// Stop the process from exiting before the transaction is sent
setInterval(() => {}, 1000);

const http = require('http');
const express = require('express');
const cors = require('cors');
const { Server } = require('socket.io');
const ioc = require('socket.io-client');

const PORT = 3005;

const run = async () => {
const app = express();
app.use(cors());

const expressServer = http.createServer(app);

const io = new Server(expressServer);

app.get('/', (_req, res) => {
io.emit('test', 'TEST MESSAGE');
res.send('123');
});

expressServer.listen(PORT, () => {
io.on('connection', client => {
client.on('test_reply', _data => {
client.disconnect();
io.close();
});
});

const clientSocket = ioc(`http://localhost:${PORT}`);

clientSocket.on('test', _msg => {
clientSocket.emit('test_reply', { test_key: 'test_value' });
});
});

sendPortToRunner(PORT);
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

describe('socket.io auto instrumentation', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('should auto-instrument `socket.io` package', done => {
const SERVER_TRANSACTION = {
transaction: 'GET /',
spans: expect.arrayContaining([
expect.objectContaining({
origin: 'auto.socket.otel.socket_io',
data: {
'sentry.origin': 'auto.socket.otel.socket_io',
'messaging.destination': '/',
'messaging.destination_kind': 'topic',
'messaging.socket.io.event_name': 'test',
'messaging.socket.io.namespace': '/',
'messaging.system': 'socket.io',
'otel.kind': 'PRODUCER',
'sentry.op': 'message',
},
description: '/ send',
op: 'message',
status: 'ok',
}),
]),
};

const CLIENT_TRANSACTION = {
transaction: 'test_reply receive',
contexts: {
trace: expect.objectContaining({
span_id: expect.any(String),
trace_id: expect.any(String),
data: expect.objectContaining({
'sentry.op': 'message',
'sentry.origin': 'auto.socket.otel.socket_io',
'otel.kind': 'CONSUMER',
'messaging.system': 'socket.io',
'messaging.destination': '/',
'messaging.operation': 'receive',
'messaging.socket.io.event_name': 'test_reply',
}),
origin: 'auto.socket.otel.socket_io',
op: 'message',
status: 'ok',
}),
},
};

createRunner(__dirname, 'scenario.js')
.expect({ transaction: SERVER_TRANSACTION })
.expect({ transaction: CLIENT_TRANSACTION })
.start(done)
.makeRequest('get', '/');
});
});
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export {
setupKoaErrorHandler,
setupNestErrorHandler,
setUser,
socketIoIntegration,
spanToBaggageHeader,
spanToJSON,
spanToTraceHeader,
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
prismaIntegration,
hapiIntegration,
setupHapiErrorHandler,
socketIoIntegration,
spotlightIntegration,
initOpenTelemetry,
spanToJSON,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export {
prismaIntegration,
hapiIntegration,
setupHapiErrorHandler,
socketIoIntegration,
spotlightIntegration,
initOpenTelemetry,
spanToJSON,
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
prismaIntegration,
hapiIntegration,
setupHapiErrorHandler,
socketIoIntegration,
spotlightIntegration,
initOpenTelemetry,
spanToJSON,
Expand Down
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@opentelemetry/instrumentation-nestjs-core": "0.40.0",
"@opentelemetry/instrumentation-pg": "0.44.0",
"@opentelemetry/instrumentation-redis-4": "0.42.0",
"@opentelemetry/instrumentation-socket.io": "0.42.0",
"@opentelemetry/instrumentation-undici": "0.6.0",
"@opentelemetry/resources": "^1.26.0",
"@opentelemetry/sdk-trace-base": "^1.26.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/integrations/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { instrumentMysql2, mysql2Integration } from './mysql2';
import { instrumentNest, nestIntegration } from './nest/nest';
import { instrumentPostgres, postgresIntegration } from './postgres';
import { instrumentRedis, redisIntegration } from './redis';
import { instrumentSocketIo, socketIoIntegration } from './socket.io';

/**
* With OTEL, all performance integrations will be added, as OTEL only initializes them when the patched package is actually required.
Expand All @@ -41,6 +42,7 @@ export function getAutoPerformanceIntegrations(): Integration[] {
connectIntegration(),
genericPoolIntegration(),
kafkaIntegration(),
socketIoIntegration(),
];
}

Expand All @@ -67,5 +69,6 @@ export function getOpenTelemetryInstrumentationToPreload(): (((options?: any) =>
instrumentGraphql,
instrumentRedis,
instrumentGenericPool,
instrumentSocketIo,
];
}
37 changes: 37 additions & 0 deletions packages/node/src/integrations/tracing/socket.io.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { SocketIoInstrumentation } from '@opentelemetry/instrumentation-socket.io';
import { defineIntegration } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import { generateInstrumentOnce } from '../../otel/instrument';
import { addOriginToSpan } from '../../utils/addOriginToSpan';

const INTEGRATION_NAME = 'Socket.io';

export const instrumentSocketIo = generateInstrumentOnce(
INTEGRATION_NAME,
() =>
new SocketIoInstrumentation({
emitHook(span) {
addOriginToSpan(span, 'auto.socket.otel.socket_io');
},
onHook(span) {
addOriginToSpan(span, 'auto.socket.otel.socket_io');
},
traceReserved: true,
}),
);

const _socketIoIntegration = (() => {
return {
name: INTEGRATION_NAME,
setupOnce() {
instrumentSocketIo();
},
};
}) satisfies IntegrationFn;

/**
* Socket.io integration
*
* Capture tracing data for Socket.io
*/
export const socketIoIntegration = defineIntegration(_socketIoIntegration);
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export {
setupKoaErrorHandler,
setupNestErrorHandler,
setUser,
socketIoIntegration,
spanToBaggageHeader,
spanToJSON,
spanToTraceHeader,
Expand Down
1 change: 1 addition & 0 deletions packages/solidstart/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export {
setupKoaErrorHandler,
setupNestErrorHandler,
setUser,
socketIoIntegration,
spanToBaggageHeader,
spanToJSON,
spanToTraceHeader,
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export {
setupKoaErrorHandler,
setupNestErrorHandler,
setUser,
socketIoIntegration,
spanToBaggageHeader,
spanToJSON,
spanToTraceHeader,
Expand Down
Loading

0 comments on commit 4df621b

Please sign in to comment.