Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Add socketIoIntegration to node #13578

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 with express server', done => {
const SERVER_TRANSACTION = {
transaction: 'GET /',
spans: expect.arrayContaining([
expect.objectContaining({
data: {
'sentry.origin': 'auto.socket.otel.producer',
'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',
},
origin: 'auto.socket.otel.producer',
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.consumer',
'otel.kind': 'CONSUMER',
'messaging.system': 'socket.io',
'messaging.destination': '/',
'messaging.operation': 'receive',
'messaging.socket.io.event_name': 'test_reply',
}),
origin: 'auto.socket.otel.consumer',
op: 'message',
status: 'ok',
}),
},
};

createRunner(__dirname, 'scenario.js')
.expect({ transaction: SERVER_TRANSACTION })
.expect({ transaction: CLIENT_TRANSACTION })
Copy link
Contributor Author

@Zen-cronic Zen-cronic Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why the server socket and client socket are not part of the same transaction. In scenario.js, the server socket emits an event to the client, which triggers the client to send back to the server.

Update: the transactions are also not part of the same trace (different trace_id) - verified by debugging.

.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
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export { prismaIntegration } from './integrations/tracing/prisma';
export { hapiIntegration, setupHapiErrorHandler } from './integrations/tracing/hapi';
export { koaIntegration, setupKoaErrorHandler } from './integrations/tracing/koa';
export { connectIntegration, setupConnectErrorHandler } from './integrations/tracing/connect';
export { socketIoIntegration } from './integrations/tracing/socket.io';
export { spotlightIntegration } from './integrations/spotlight';
export { genericPoolIntegration } from './integrations/tracing/genericPool';

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.producer');
},
onHook(span) {
addOriginToSpan(span, 'auto.socket.otel.consumer');
},
traceReserved: true,
Copy link
Contributor Author

@Zen-cronic Zen-cronic Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expose options such as traceReserved, emitIgnoreEventList, and onIgnoreEventList for users to configure.

}),
);

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
Loading