-
-
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 amqplibIntegration (#13714)
resolves #13312 Before submitting a pull request, please take a look at our [Contributing](https://github.com/getsentry/sentry-javascript/blob/master/CONTRIBUTING.md) guidelines and verify: - [x] If you've added code that should be tested, please add tests. - [x] Ensure your code lints and the test suite passes (`yarn lint`) & (`yarn test`).
- Loading branch information
Showing
21 changed files
with
316 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,6 @@ packages/deno/lib.deno.d.ts | |
|
||
# gatsby | ||
packages/gatsby/gatsby-node.d.ts | ||
|
||
# intellij | ||
*.iml |
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
15 changes: 15 additions & 0 deletions
15
dev-packages/node-integration-tests/suites/tracing/amqplib/constants.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,15 @@ | ||
const amqpUsername = 'sentry'; | ||
const amqpPassword = 'sentry'; | ||
|
||
export const AMQP_URL = `amqp://${amqpUsername}:${amqpPassword}@localhost:5672/`; | ||
export const ACKNOWLEDGEMENT = { noAck: false }; | ||
|
||
export const QUEUE_OPTIONS = { | ||
durable: true, // Make the queue durable | ||
exclusive: false, // Not exclusive | ||
autoDelete: false, // Don't auto-delete the queue | ||
arguments: { | ||
'x-message-ttl': 30000, // Message TTL of 30 seconds | ||
'x-max-length': 1000, // Maximum queue length of 1000 messages | ||
}, | ||
}; |
16 changes: 16 additions & 0 deletions
16
dev-packages/node-integration-tests/suites/tracing/amqplib/docker-compose.yml
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,16 @@ | ||
version: '3' | ||
|
||
services: | ||
rabbitmq: | ||
image: rabbitmq:management | ||
container_name: rabbitmq | ||
environment: | ||
- RABBITMQ_DEFAULT_USER=sentry | ||
- RABBITMQ_DEFAULT_PASS=sentry | ||
ports: | ||
- "5672:5672" | ||
- "15672:15672" | ||
|
||
networks: | ||
default: | ||
driver: bridge |
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/amqplib/init.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,9 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); |
19 changes: 19 additions & 0 deletions
19
dev-packages/node-integration-tests/suites/tracing/amqplib/scenario-message.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,19 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import './init'; | ||
import { connectToRabbitMQ, consumeMessageFromQueue, createQueue, sendMessageToQueue } from './utils'; | ||
|
||
const queueName = 'queue1'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
(async () => { | ||
const { connection, channel } = await connectToRabbitMQ(); | ||
await createQueue(queueName, channel); | ||
|
||
await Sentry.startSpan({ name: 'root span' }, async () => { | ||
sendMessageToQueue(queueName, channel, JSON.stringify({ foo: 'bar01' })); | ||
}); | ||
|
||
await consumeMessageFromQueue(queueName, channel); | ||
await channel.close(); | ||
await connection.close(); | ||
})(); |
54 changes: 54 additions & 0 deletions
54
dev-packages/node-integration-tests/suites/tracing/amqplib/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,54 @@ | ||
import type { TransactionEvent } from '@sentry/types'; | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
jest.setTimeout(30_000); | ||
|
||
const EXPECTED_MESSAGE_SPAN_PRODUCER = expect.objectContaining({ | ||
op: 'message', | ||
data: expect.objectContaining({ | ||
'messaging.system': 'rabbitmq', | ||
'otel.kind': 'PRODUCER', | ||
'sentry.op': 'message', | ||
'sentry.origin': 'auto.amqplib.otel.publisher', | ||
}), | ||
status: 'ok', | ||
}); | ||
|
||
const EXPECTED_MESSAGE_SPAN_CONSUMER = expect.objectContaining({ | ||
op: 'message', | ||
data: expect.objectContaining({ | ||
'messaging.system': 'rabbitmq', | ||
'otel.kind': 'CONSUMER', | ||
'sentry.op': 'message', | ||
'sentry.origin': 'auto.amqplib.otel.consumer', | ||
}), | ||
status: 'ok', | ||
}); | ||
|
||
describe('amqplib auto-instrumentation', () => { | ||
afterAll(async () => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('should be able to send and receive messages', done => { | ||
createRunner(__dirname, 'scenario-message.ts') | ||
.withDockerCompose({ | ||
workingDirectory: [__dirname], | ||
readyMatches: ['Time to start RabbitMQ'], | ||
}) | ||
.expect({ | ||
transaction: (transaction: TransactionEvent) => { | ||
expect(transaction.transaction).toEqual('root span'); | ||
expect(transaction.spans?.length).toEqual(1); | ||
expect(transaction.spans![0]).toMatchObject(EXPECTED_MESSAGE_SPAN_PRODUCER); | ||
}, | ||
}) | ||
.expect({ | ||
transaction: (transaction: TransactionEvent) => { | ||
expect(transaction.transaction).toEqual('queue1 process'); | ||
expect(transaction.contexts?.trace).toMatchObject(EXPECTED_MESSAGE_SPAN_CONSUMER); | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
dev-packages/node-integration-tests/suites/tracing/amqplib/utils.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,38 @@ | ||
import amqp from 'amqplib'; | ||
import type { Channel, Connection } from 'amqplib'; | ||
import { ACKNOWLEDGEMENT, AMQP_URL, QUEUE_OPTIONS } from './constants'; | ||
|
||
export type RabbitMQData = { | ||
connection: Connection; | ||
channel: Channel; | ||
}; | ||
|
||
export async function connectToRabbitMQ(): Promise<RabbitMQData> { | ||
const connection = await amqp.connect(AMQP_URL); | ||
const channel = await connection.createChannel(); | ||
return { connection, channel }; | ||
} | ||
|
||
export async function createQueue(queueName: string, channel: Channel): Promise<void> { | ||
await channel.assertQueue(queueName, QUEUE_OPTIONS); | ||
} | ||
|
||
export function sendMessageToQueue(queueName: string, channel: Channel, message: string): void { | ||
channel.sendToQueue(queueName, Buffer.from(message)); | ||
} | ||
|
||
async function consumer(queueName: string, channel: Channel): Promise<void> { | ||
await channel.consume( | ||
queueName, | ||
message => { | ||
if (message) { | ||
channel.ack(message); | ||
} | ||
}, | ||
ACKNOWLEDGEMENT, | ||
); | ||
} | ||
|
||
export async function consumeMessageFromQueue(queueName: string, channel: Channel): Promise<void> { | ||
await consumer(queueName, channel); | ||
} |
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
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,30 @@ | ||
import type { Span } from '@opentelemetry/api'; | ||
import { AmqplibInstrumentation, type AmqplibInstrumentationConfig } from '@opentelemetry/instrumentation-amqplib'; | ||
import { defineIntegration } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
import { addOriginToSpan } from '../../utils/addOriginToSpan'; | ||
|
||
const INTEGRATION_NAME = 'Amqplib'; | ||
|
||
const config: AmqplibInstrumentationConfig = { | ||
consumeEndHook: (span: Span) => { | ||
addOriginToSpan(span, 'auto.amqplib.otel.consumer'); | ||
}, | ||
publishHook: (span: Span) => { | ||
addOriginToSpan(span, 'auto.amqplib.otel.publisher'); | ||
}, | ||
}; | ||
|
||
export const instrumentAmqplib = generateInstrumentOnce(INTEGRATION_NAME, () => new AmqplibInstrumentation(config)); | ||
|
||
const _amqplibIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentAmqplib(); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
export const amqplibIntegration = defineIntegration(_amqplibIntegration); |
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
Oops, something went wrong.