-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add socket.io integration
Add otel integration and e2e tests. Signed-off-by: Kaung Zin Hein <[email protected]>
- Loading branch information
1 parent
8d2e189
commit 4df621b
Showing
14 changed files
with
312 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
dev-packages/node-integration-tests/suites/tracing/socket.io/scenario.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
59 changes: 59 additions & 0 deletions
59
dev-packages/node-integration-tests/suites/tracing/socket.io/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '/'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.