Skip to content

Update to always set db #16169

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

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,40 @@ sentryTest('should capture Supabase authentication spans', async ({ getLocalTest
const url = await getLocalTestUrl({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
const supabaseSpans = eventData.spans?.filter(({ op }) => op?.startsWith('db.auth'));
const supabaseSpans = eventData.spans?.filter(({ op }) => op?.startsWith('db'));

expect(supabaseSpans).toHaveLength(2);
expect(supabaseSpans![0]).toMatchObject({
description: 'signInWithPassword',
description: 'auth signInWithPassword',
op: 'db',
parent_span_id: eventData.contexts?.trace?.span_id,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: eventData.contexts?.trace?.trace_id,
status: 'ok',
data: expect.objectContaining({
'sentry.op': 'db.auth.signInWithPassword',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
'db.operation': 'auth.signInWithPassword',
'db.system': 'postgresql',
}),
});

expect(supabaseSpans![1]).toMatchObject({
description: 'signOut',
description: 'auth signOut',
op: 'db',
parent_span_id: eventData.contexts?.trace?.span_id,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: eventData.contexts?.trace?.trace_id,
status: 'ok',
data: expect.objectContaining({
'sentry.op': 'db.auth.signOut',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
'db.operation': 'auth.signOut',
'db.system': 'postgresql',
}),
});
});
Expand All @@ -124,22 +130,25 @@ sentryTest('should capture Supabase authentication errors', async ({ getLocalTes

const [errorEvent, transactionEvent] = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url });

const supabaseSpans = transactionEvent.spans?.filter(({ op }) => op?.startsWith('db.auth'));
const supabaseSpans = transactionEvent.spans?.filter(({ op }) => op?.startsWith('db'));

expect(errorEvent.exception?.values?.[0].value).toBe('Invalid email or password');

expect(supabaseSpans).toHaveLength(2);
expect(supabaseSpans![0]).toMatchObject({
description: 'signInWithPassword',
description: 'auth signInWithPassword',
op: 'db',
parent_span_id: transactionEvent.contexts?.trace?.span_id,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: transactionEvent.contexts?.trace?.trace_id,
status: 'unknown_error',
data: expect.objectContaining({
'sentry.op': 'db.auth.signInWithPassword',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
'db.operation': 'auth.signInWithPassword',
'db.system': 'postgresql',
}),
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ sentryTest('should capture Supabase database operation breadcrumbs', async ({ ge
timestamp: expect.any(Number),
type: 'supabase',
category: 'db.insert',
message: 'from(todos)',
data: expect.any(Object),
message: 'insert(...) filter(columns, ) from(todos)',
data: expect.objectContaining({
query: expect.arrayContaining(['filter(columns, )']),
}),
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ test('Sends server-side Supabase auth admin `createUser` span', async ({ page, b
const transactionEvent = await httpTransactionPromise;

expect(transactionEvent.spans).toContainEqual({
data: expect.any(Object),
description: 'createUser',
op: 'db.auth.admin.createUser',
data: expect.objectContaining({
'db.operation': 'auth.admin.createUser',
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
description: 'auth (admin) createUser',
op: 'db',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand Down Expand Up @@ -54,8 +59,15 @@ test('Sends client-side Supabase db-operation spans and breadcrumbs to Sentry',

expect(transactionEvent.spans).toContainEqual(
expect.objectContaining({
description: 'from(todos)',
op: 'db.select',
description: 'select(*) filter(order, asc) from(todos)',
op: 'db',
data: expect.objectContaining({
'db.operation': 'select',
'db.query': ['select(*)', 'filter(order, asc)'],
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand All @@ -67,9 +79,15 @@ test('Sends client-side Supabase db-operation spans and breadcrumbs to Sentry',
);

expect(transactionEvent.spans).toContainEqual({
data: expect.any(Object),
description: 'from(todos)',
op: 'db.insert',
data: expect.objectContaining({
'db.operation': 'select',
'db.query': ['select(*)', 'filter(order, asc)'],
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
description: 'select(*) filter(order, asc) from(todos)',
op: 'db',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand All @@ -83,15 +101,15 @@ test('Sends client-side Supabase db-operation spans and breadcrumbs to Sentry',
timestamp: expect.any(Number),
type: 'supabase',
category: 'db.select',
message: 'from(todos)',
message: 'select(*) filter(order, asc) from(todos)',
data: expect.any(Object),
});

expect(transactionEvent.breadcrumbs).toContainEqual({
timestamp: expect.any(Number),
type: 'supabase',
category: 'db.insert',
message: 'from(todos)',
message: 'insert(...) select(*) from(todos)',
data: expect.any(Object),
});
});
Expand All @@ -109,8 +127,15 @@ test('Sends server-side Supabase db-operation spans and breadcrumbs to Sentry',

expect(transactionEvent.spans).toContainEqual(
expect.objectContaining({
description: 'from(todos)',
op: 'db.select',
data: expect.objectContaining({
'db.operation': 'insert',
'db.query': ['select(*)'],
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
description: 'insert(...) select(*) from(todos)',
op: 'db',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand All @@ -122,9 +147,15 @@ test('Sends server-side Supabase db-operation spans and breadcrumbs to Sentry',
);

expect(transactionEvent.spans).toContainEqual({
data: expect.any(Object),
description: 'from(todos)',
op: 'db.insert',
data: expect.objectContaining({
'db.operation': 'select',
'db.query': ['select(*)'],
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
description: 'select(*) from(todos)',
op: 'db',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand All @@ -138,34 +169,38 @@ test('Sends server-side Supabase db-operation spans and breadcrumbs to Sentry',
timestamp: expect.any(Number),
type: 'supabase',
category: 'db.select',
message: 'from(todos)',
message: 'select(*) from(todos)',
data: expect.any(Object),
});

expect(transactionEvent.breadcrumbs).toContainEqual({
timestamp: expect.any(Number),
type: 'supabase',
category: 'db.insert',
message: 'from(todos)',
message: 'insert(...) select(*) from(todos)',
data: expect.any(Object),
});
});

test('Sends server-side Supabase auth admin `listUsers` span', async ({ page, baseURL }) => {
const httpTransactionPromise = waitForTransaction('supabase-nextjs', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent?.transaction === 'GET /api/list-users'
transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'GET /api/list-users'
);
});

await fetch(`${baseURL}/api/list-users`);
const transactionEvent = await httpTransactionPromise;

expect(transactionEvent.spans).toContainEqual({
data: expect.any(Object),
description: 'listUsers',
op: 'db.auth.admin.listUsers',
data: expect.objectContaining({
'db.operation': 'auth.admin.listUsers',
'db.system': 'postgresql',
'sentry.op': 'db',
'sentry.origin': 'auto.db.supabase',
}),
description: 'auth (admin) listUsers',
op: 'db',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
Expand Down
18 changes: 13 additions & 5 deletions packages/core/src/integrations/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ function instrumentAuthOperation(operation: AuthOperationFn, isAdmin = false): A
apply(target, thisArg, argumentsList) {
return startSpan(
{
name: operation.name,
name: `auth ${isAdmin ? '(admin) ' : ''}${operation.name}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.supabase',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `db.auth.${isAdmin ? 'admin.' : ''}${operation.name}`,
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db',
'db.system': 'postgresql',
'db.operation': `auth.${isAdmin ? 'admin.' : ''}${operation.name}`,
},
},
span => {
Expand Down Expand Up @@ -341,30 +343,36 @@ function instrumentPostgRESTFilterBuilder(PostgRESTFilterBuilder: PostgRESTFilte

const pathParts = typedThis.url.pathname.split('/');
const table = pathParts.length > 0 ? pathParts[pathParts.length - 1] : '';
const description = `from(${table})`;

const queryItems: string[] = [];
for (const [key, value] of typedThis.url.searchParams.entries()) {
// It's possible to have multiple entries for the same key, eg. `id=eq.7&id=eq.3`,
// so we need to use array instead of object to collect them.
queryItems.push(translateFiltersIntoMethods(key, value));
}

const body: Record<string, unknown> = Object.create(null);
if (isPlainObject(typedThis.body)) {
for (const [key, value] of Object.entries(typedThis.body)) {
body[key] = value;
}
}

// Adding operation to the beginning of the description if it's not a `select` operation
// For example, it can be an `insert` or `update` operation but the query can be `select(...)`
// For `select` operations, we don't need repeat it in the description
const description = `${operation === 'select' ? '' : `${operation}${body ? '(...) ' : ''}`}${queryItems.join(
' ',
)} from(${table})`;

const attributes: Record<string, any> = {
'db.table': table,
'db.schema': typedThis.schema,
'db.url': typedThis.url.origin,
'db.sdk': typedThis.headers['X-Client-Info'],
'db.system': 'postgresql',
'db.operation': operation,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.supabase',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `db.${operation}`,
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db',
};

if (queryItems.length) {
Expand Down