Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): migrate auth log links to OTEL (#50718)

Jordi Enric01321d92220a
diff --git a/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx b/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
index 418e95a14cc..afd5feea018 100644
--- a/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
+++ b/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
@@ -1,6 +1,6 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useParams } from 'common'
+import { useFlag, useParams } from 'common'
import { useEffect } from 'react'
import { useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
@@ -21,6 +21,8 @@ import { AlertError } from '@/components/ui/AlertError'
import { InlineLink } from '@/components/ui/InlineLink'
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
import { useAuthConfigUpdateMutation } from '@/data/auth/auth-config-update-mutation'
+import { pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
+import { safeSql } from '@/data/logs/safe-analytics-sql'
import { useTablesQuery } from '@/data/tables/tables-query'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
@@ -31,9 +33,36 @@ const schema = z.object({
const AUDIT_LOG_ENTRIES_TABLE = 'audit_log_entries'
+const AUTH_AUDIT_LOGS_QUERY = safeSql`
+ select
+ cast(timestamp as datetime) as timestamp,
+ event_message,
+ metadata
+ from auth_audit_logs
+ limit 10
+`
+
+const AUTH_AUDIT_LOGS_QUERY_OTEL = safeSql`
+ select
+ timestamp,
+ event_message,
+ log_attributes
+ from logs
+ where source = 'auth_audit_logs'
+ order by timestamp desc
+ limit 10
+`
+
export const AuditLogsForm = () => {
const { ref: projectRef } = useParams()
+ const useOtel = useFlag('otelLegacyLogs')
const { data: project } = useSelectedProjectQuery()
+ const authAuditLogsQuery = pickLogsQueryBuilder(
+ useOtel,
+ AUTH_AUDIT_LOGS_QUERY_OTEL,
+ AUTH_AUDIT_LOGS_QUERY
+ )
+ const authAuditLogsUrl = `/project/${projectRef}/logs/explorer?q=${encodeURIComponent(authAuditLogsQuery)}`
const { can: canUpdateConfig } = useAsyncCheckPermissions(
PermissionAction.UPDATE,
@@ -141,13 +170,7 @@ export const AuditLogsForm = () => {
table.
<br />
You can disable this to reduce disk usage while still accessing logs
- through the{' '}
- <InlineLink
- href={`/project/${projectRef}/logs/explorer?q=select%0A++cast(timestamp+as+datetime)+as+timestamp%2C%0A++event_message%2C+metadata+%0Afrom+auth_audit_logs+%0Alimit+10%0A`}
- >
- Auth logs
- </InlineLink>
- .
+ through the <InlineLink href={authAuditLogsUrl}>Auth logs</InlineLink>.
</p>
}
>
@@ -169,13 +192,8 @@ export const AuditLogsForm = () => {
description={
<p>
Future audit logs will only appear in the project’s{' '}
- <InlineLink
- href={`/project/${projectRef}/logs/explorer?q=select%0A++cast(timestamp+as+datetime)+as+timestamp%2C%0A++event_message%2C+metadata+%0Afrom+auth_audit_logs+%0Alimit+10%0A`}
- >
- auth logs
- </InlineLink>
- . You are responsible for backing up, copying, or migrating existing data
- from the{' '}
+ <InlineLink href={authAuditLogsUrl}>auth logs</InlineLink>. You are
+ responsible for backing up, copying, or migrating existing data from the{' '}
<code className="text-code-inline break-keep!">
{AUDIT_LOG_ENTRIES_TABLE}
</code>{' '}
diff --git a/apps/studio/components/interfaces/Auth/Users/UserLogs.tsx b/apps/studio/components/interfaces/Auth/Users/UserLogs.tsx
index fcfeba45cd8..f54a9762083 100644
--- a/apps/studio/components/interfaces/Auth/Users/UserLogs.tsx
+++ b/apps/studio/components/interfaces/Auth/Users/UserLogs.tsx
@@ -1,4 +1,4 @@
-import { useParams } from 'common'
+import { useFlag, useParams } from 'common'
import { ExternalLink, RefreshCw } from 'lucide-react'
import Link from 'next/link'
import { useQueryState } from 'nuqs'
@@ -13,6 +13,8 @@ import { PANEL_PADDING } from './Users.constants'
import { LOGS_TABLES } from '@/components/interfaces/Settings/Logs/Logs.constants'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { User } from '@/data/auth/users-infinite-query'
+import { pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
+import { analyticsLiteral, safeSql } from '@/data/logs/safe-analytics-sql'
import useLogsPreview from '@/hooks/analytics/useLogsPreview'
import { useLogsUrlState } from '@/hooks/analytics/useLogsUrlState'
@@ -20,13 +22,38 @@ interface UserLogsProps {
user: User
}
-const API_LOGS_QUERY = (userId: string) =>
- `select\n cast(timestamp as datetime) as timestamp,\n event_message, metadata \nfrom edge_logs \nWHERE (\n metadata[SAFE_OFFSET(0)].request[SAFE_OFFSET(0)].sb[SAFE_OFFSET(0)].auth_user\n = '${userId}'\n)\nlimit 100`
+const getApiLogsQuery = (userId: string) => safeSql`
+ select
+ cast(timestamp as datetime) as timestamp,
+ event_message,
+ metadata
+ from edge_logs
+ where metadata[SAFE_OFFSET(0)].request[SAFE_OFFSET(0)].sb[SAFE_OFFSET(0)].auth_user = ${analyticsLiteral(userId)}
+ limit 100
+`
+
+const getApiLogsQueryOtel = (userId: string) => safeSql`
+ select
+ timestamp,
+ event_message,
+ log_attributes
+ from logs
+ where source = 'auth_logs'
+ and log_attributes['auth_event.actor_id'] = ${analyticsLiteral(userId)}
+ order by timestamp desc
+ limit 100
+`
export const UserLogs = ({ user }: UserLogsProps) => {
const { ref } = useParams()
+ const useOtel = useFlag('otelLegacyLogs')
const { filters, setFilters } = useLogsUrlState()
const [, setFiltersValue] = useQueryState('f')
+ const apiLogsQuery = pickLogsQueryBuilder(
+ useOtel,
+ getApiLogsQueryOtel,
+ getApiLogsQuery
+ )(user.id ?? '')
const {
logData: authLogs,
@@ -57,16 +84,12 @@ export const UserLogs = ({ user }: UserLogsProps) => {
<div className={cn('flex flex-col gap-y-3', PANEL_PADDING)}>
<div>
- <p>API logs</p>
- <p className="text-sm text-foreground-light">
- View edge logs for requests made by this user
- </p>
+ <p>Auth API logs</p>
+ <p className="text-sm text-foreground-light">View logs associated with this Auth user</p>
</div>
<Button asChild className="w-min">
- <Link
- href={`/project/${ref}/logs/explorer?q=${encodeURIComponent(API_LOGS_QUERY(user.id ?? ''))}`}
- >
+ <Link href={`/project/${ref}/logs/explorer?q=${encodeURIComponent(apiLogsQuery)}`}>
Open in Log Explorer
</Link>
</Button>
diff --git a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
index ed8828dd3c7..e24be1662d3 100644
--- a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
+++ b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
@@ -70,6 +70,8 @@ import { useUserDeleteMutation } from '@/data/auth/user-delete-mutation'
import { useUserIndexStatusesQuery } from '@/data/auth/user-search-indexes-query'
import { useUsersCountQuery } from '@/data/auth/users-count-query'
import { User, useUsersInfiniteQuery } from '@/data/auth/users-infinite-query'
+import { pickLogsQueryBuilder } from '@/data/logs/logs-endpoint'
+import { safeSql } from '@/data/logs/safe-analytics-sql'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
@@ -81,13 +83,37 @@ import { useRoleImpersonationStateSnapshot } from '@/state/role-impersonation-st
const SORT_BY_VALUE_COUNT_THRESHOLD = 10_000
const IMPROVED_SEARCH_COUNT_THRESHOLD = 10_000
-const INDEX_WORKER_LOGS_SEARCH_STRING = `select id, auth_logs.timestamp, metadata.level, event_message, metadata.msg as msg, metadata.error
-from auth_logs
-cross join unnest(metadata) as metadata
-where metadata.worker_type = 'apiworker_index_worker'
- and auth_logs.timestamp >= timestamp_sub(current_timestamp(), interval 3 hour)
-order by timestamp desc
-limit 100`
+const INDEX_WORKER_LOGS_QUERY = safeSql`
+ select
+ id,
+ auth_logs.timestamp,
+ metadata.level,
+ event_message,
+ metadata.msg as msg,
+ metadata.error
+ from auth_logs
+ cross join unnest(metadata) as metadata
+ where metadata.worker_type = 'apiworker_index_worker'
+ and auth_logs.timestamp >= timestamp_sub(current_timestamp(), interval 3 hour)
+ order by timestamp desc
+ limit 100
+`
+
+const INDEX_WORKER_LOGS_QUERY_OTEL = safeSql`
+ select
+ id,
+ timestamp,
+ log_attributes['level'] as level,
+ event_message,
+ log_attributes['msg'] as msg,
+ log_attributes['error'] as error
+ from logs
+ where source = 'auth_logs'
+ and log_attributes['worker_type'] = 'apiworker_index_worker'
+ and timestamp >= now() - interval 3 hour
+ order by timestamp desc
+ limit 100
+`
export const UsersV2 = () => {
const router = useRouter()
@@ -99,6 +125,12 @@ export const UsersV2 = () => {
isError: isProjectError,
} = useSelectedProjectQuery()
const roleImpersonationState = useRoleImpersonationStateSnapshot()
+ const useOtel = useFlag('otelLegacyLogs')
+ const indexWorkerLogsQuery = pickLogsQueryBuilder(
+ useOtel,
+ INDEX_WORKER_LOGS_QUERY_OTEL,
+ INDEX_WORKER_LOGS_QUERY
+ )
const gridRef = useRef<DataGridHandle>(null)
const searchInputRef = useRef<HTMLInputElement>(null)
@@ -570,7 +602,7 @@ export const UsersV2 = () => {
<Button variant="link" iconRight={<ExternalLinkIcon />} asChild>
<Link
- href={`/project/${projectRef}/logs/explorer?q=${encodeURI(INDEX_WORKER_LOGS_SEARCH_STRING)}`}
+ href={`/project/${projectRef}/logs/explorer?q=${encodeURIComponent(indexWorkerLogsQuery)}`}
target="_blank"
>
View logs
@@ -963,7 +995,7 @@ export const UsersV2 = () => {
<li className="marker:text-foreground-light">
You can monitor the progress in the{' '}
<InlineLink
- href={`/project/${projectRef}/logs/explorer?q=${encodeURI(INDEX_WORKER_LOGS_SEARCH_STRING)}`}
+ href={`/project/${projectRef}/logs/explorer?q=${encodeURIComponent(indexWorkerLogsQuery)}`}
target="_blank"
>
project logs