-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add Supabase Queues support #15921
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
Open
onurtemizkan
wants to merge
17
commits into
develop
Choose a base branch
from
onur/supabase-queues
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3f55cf5
feat(core): Add Supabase Queues support
onurtemizkan ecf67cc
Skip tests on bundles
onurtemizkan 13d4a6e
Update test usage
onurtemizkan 8d38e7c
Lint
onurtemizkan a5a271e
Update implementation
onurtemizkan 6e0bc24
Update playwright tests
onurtemizkan 5327a3c
Tidy up test endpoints
onurtemizkan a5744f0
Refactor / reimplement
onurtemizkan 2e6012a
Add missing import
onurtemizkan b5ebc9a
Rename `SupabaseClientConstructor` to `SupabaseClientConstructorType`
onurtemizkan 1d32d86
Extract `instrumentRpcMethod`
onurtemizkan d650de1
WIP
onurtemizkan 9accd4b
Match with OTEL semantics improve tests
onurtemizkan e68a576
Add OTEL operation attributes to queue spans
onurtemizkan 09ef20a
Fix browser integration tests
onurtemizkan 23c89bb
Improve data integrity and distributed tracing
onurtemizkan 81d0767
Use span links for consumer distributed tracing, fix prototype instru…
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
dev-packages/browser-integration-tests/suites/integrations/supabase/queues-rpc/init.js
This file contains hidden or 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,34 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { createClient } from '@supabase/supabase-js'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| const supabaseClient = createClient('https://test.supabase.co', 'test-key', { | ||
| db: { | ||
| schema: 'pgmq_public', | ||
| }, | ||
| }); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [Sentry.browserTracingIntegration(), Sentry.supabaseIntegration({ supabaseClient })], | ||
| tracesSampleRate: 1.0, | ||
| }); | ||
|
|
||
| // Simulate queue operations | ||
| async function performQueueOperations() { | ||
| try { | ||
| await supabaseClient.rpc('send', { | ||
| queue_name: 'todos', | ||
| msg: { title: 'Test Todo' }, | ||
| }); | ||
|
|
||
| await supabaseClient.rpc('pop', { | ||
| queue_name: 'todos', | ||
| }); | ||
| } catch (error) { | ||
| Sentry.captureException(error); | ||
| } | ||
| } | ||
|
|
||
| performQueueOperations(); | ||
82 changes: 82 additions & 0 deletions
82
dev-packages/browser-integration-tests/suites/integrations/supabase/queues-rpc/test.ts
This file contains hidden or 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,82 @@ | ||
| import type { Page } from '@playwright/test'; | ||
| import { expect } from '@playwright/test'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; | ||
|
|
||
| async function mockSupabaseRoute(page: Page) { | ||
| await page.route('**/rpc/**/send', route => { | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: JSON.stringify([0]), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/rpc/**/pop', route => { | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: JSON.stringify([ | ||
| { | ||
| msg_id: 0, | ||
| }, | ||
| ]), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const bundle = process.env.PW_BUNDLE || ''; | ||
| // We only want to run this in non-CDN bundle mode | ||
| if (bundle.startsWith('bundle')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| sentryTest('should capture Supabase queue spans from client.rpc', async ({ getLocalTestUrl, page }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| return; | ||
| } | ||
|
|
||
| await mockSupabaseRoute(page); | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const event = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
| const queueSpans = event.spans?.filter(({ op }) => op?.startsWith('queue.')); | ||
|
|
||
| expect(queueSpans).toHaveLength(2); | ||
|
|
||
| expect(queueSpans![0]).toMatchObject({ | ||
| description: 'supabase.db.rpc', | ||
| parent_span_id: event.contexts?.trace?.span_id, | ||
| span_id: expect.any(String), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: event.contexts?.trace?.trace_id, | ||
| data: expect.objectContaining({ | ||
| 'sentry.op': 'queue.publish', | ||
| 'sentry.origin': 'auto.db.supabase', | ||
| 'messaging.destination.name': 'todos', | ||
| 'messaging.message.id': '0', | ||
| }), | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| expect(queueSpans![1]).toMatchObject({ | ||
| description: 'supabase.db.rpc', | ||
| parent_span_id: event.contexts?.trace?.span_id, | ||
| span_id: expect.any(String), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: event.contexts?.trace?.trace_id, | ||
| data: expect.objectContaining({ | ||
| 'sentry.op': 'queue.process', | ||
| 'sentry.origin': 'auto.db.supabase', | ||
| 'messaging.destination.name': 'todos', | ||
| 'messaging.message.id': '0', | ||
| }), | ||
| }); | ||
| }); | ||
34 changes: 34 additions & 0 deletions
34
dev-packages/browser-integration-tests/suites/integrations/supabase/queues-schema/init.js
This file contains hidden or 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,34 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { createClient } from '@supabase/supabase-js'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| const supabaseClient = createClient('https://test.supabase.co', 'test-key', { | ||
| db: { | ||
| schema: 'pgmq_public', | ||
| }, | ||
| }); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [Sentry.browserTracingIntegration(), Sentry.supabaseIntegration({ supabaseClient })], | ||
| tracesSampleRate: 1.0, | ||
| }); | ||
|
|
||
| // Simulate queue operations | ||
| async function performQueueOperations() { | ||
| try { | ||
| await supabaseClient.schema('pgmq_public').rpc('send', { | ||
| queue_name: 'todos', | ||
| msg: { title: 'Test Todo' }, | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await supabaseClient.schema('pgmq_public').rpc('pop', { | ||
| queue_name: 'todos', | ||
| }); | ||
| } catch (error) { | ||
| Sentry.captureException(error); | ||
| } | ||
| } | ||
|
|
||
| performQueueOperations(); | ||
82 changes: 82 additions & 0 deletions
82
dev-packages/browser-integration-tests/suites/integrations/supabase/queues-schema/test.ts
This file contains hidden or 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,82 @@ | ||
| import { type Page, expect } from '@playwright/test'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; | ||
|
|
||
| async function mockSupabaseRoute(page: Page) { | ||
| await page.route('**/rpc/**/send', route => { | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: JSON.stringify([0]), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/rpc/**/pop', route => { | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: JSON.stringify([ | ||
| { | ||
| msg_id: 0, | ||
| }, | ||
| ]), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const bundle = process.env.PW_BUNDLE || ''; | ||
| // We only want to run this in non-CDN bundle mode | ||
| if (bundle.startsWith('bundle')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| sentryTest('should capture Supabase queue spans from client.schema(...).rpc', async ({ getLocalTestUrl, page }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| return; | ||
| } | ||
|
|
||
| await mockSupabaseRoute(page); | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const event = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
|
||
| const queueSpans = event.spans?.filter(({ op }) => op?.startsWith('queue.')); | ||
|
|
||
| expect(queueSpans).toHaveLength(2); | ||
|
|
||
| expect(queueSpans![0]).toMatchObject({ | ||
| description: 'supabase.db.rpc', | ||
| parent_span_id: event.contexts?.trace?.span_id, | ||
| span_id: expect.any(String), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: event.contexts?.trace?.trace_id, | ||
| data: expect.objectContaining({ | ||
| 'sentry.op': 'queue.publish', | ||
| 'sentry.origin': 'auto.db.supabase', | ||
| 'messaging.destination.name': 'todos', | ||
| 'messaging.message.id': '0', | ||
| }), | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| expect(queueSpans![1]).toMatchObject({ | ||
| description: 'supabase.db.rpc', | ||
| parent_span_id: event.contexts?.trace?.span_id, | ||
| span_id: expect.any(String), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: event.contexts?.trace?.trace_id, | ||
| data: expect.objectContaining({ | ||
| 'sentry.op': 'queue.process', | ||
| 'sentry.origin': 'auto.db.supabase', | ||
| 'messaging.destination.name': 'todos', | ||
| 'messaging.message.id': '0', | ||
| }), | ||
| }); | ||
| }); | ||
This file contains hidden or 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
85 changes: 85 additions & 0 deletions
85
dev-packages/e2e-tests/test-applications/supabase-nextjs/pages/api/queue/batch-flow.ts
This file contains hidden or 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,85 @@ | ||
| import { NextApiRequest, NextApiResponse } from 'next'; | ||
| import { createClient } from '@supabase/supabase-js'; | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| // These are the default development keys for a local Supabase instance | ||
| const NEXT_PUBLIC_SUPABASE_URL = 'http://localhost:54321'; | ||
| const SUPABASE_SERVICE_ROLE_KEY = | ||
| 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU'; | ||
|
|
||
| const supabaseClient = createClient(NEXT_PUBLIC_SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, { | ||
| db: { | ||
| schema: 'pgmq_public', | ||
| }, | ||
| }); | ||
|
|
||
| Sentry.instrumentSupabaseClient(supabaseClient); | ||
|
|
||
| export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| // Step 1: Batch produce multiple messages | ||
| const { data: sendData, error: sendError } = await supabaseClient.rpc('send_batch', { | ||
| queue_name: 'batch-flow-queue', | ||
| messages: [ | ||
| { | ||
| taskType: 'email', | ||
| recipient: 'user1@example.com', | ||
| subject: 'Welcome!', | ||
| }, | ||
| { | ||
| taskType: 'email', | ||
| recipient: 'user2@example.com', | ||
| subject: 'Verification', | ||
| }, | ||
| { | ||
| taskType: 'sms', | ||
| recipient: '+1234567890', | ||
| message: 'Your code is 123456', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| if (sendError) { | ||
| return res.status(500).json({ error: `Send batch failed: ${sendError.message}` }); | ||
| } | ||
|
|
||
| // Step 2: Consume multiple messages from the queue | ||
| const { data: receiveData, error: receiveError } = await supabaseClient.rpc('receive', { | ||
| queue_name: 'batch-flow-queue', | ||
| vt: 30, | ||
| qty: 3, | ||
| }); | ||
|
|
||
| if (receiveError) { | ||
| return res.status(500).json({ error: `Receive failed: ${receiveError.message}` }); | ||
| } | ||
|
|
||
| // Step 3: Process all messages | ||
| const processedMessages = receiveData?.map((msg: any) => ({ | ||
| messageId: msg.msg_id, | ||
| taskType: msg.message?.taskType, | ||
| processed: true, | ||
| })); | ||
|
|
||
| // Step 4: Archive all processed messages | ||
| const messageIds = receiveData?.map((msg: any) => msg.msg_id).filter(Boolean); | ||
| if (messageIds && messageIds.length > 0) { | ||
| const { error: archiveError } = await supabaseClient.rpc('archive', { | ||
| queue_name: 'batch-flow-queue', | ||
| msg_ids: messageIds, | ||
| }); | ||
|
|
||
| if (archiveError) { | ||
| return res.status(500).json({ error: `Archive failed: ${archiveError.message}` }); | ||
| } | ||
| } | ||
|
|
||
| return res.status(200).json({ | ||
| success: true, | ||
| batchSize: 3, | ||
| produced: { messageIds: sendData }, | ||
| consumed: { | ||
| count: receiveData?.length || 0, | ||
| messages: processedMessages, | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.