Skip to content

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

feat: add editor hints and documentation

TzeYiingc4360d5504dd
diff --git a/apps/docs/pages/guides/platform/logs.mdx b/apps/docs/pages/guides/platform/logs.mdx
index 2f1e8d79dd4..a748b241fbc 100644
--- a/apps/docs/pages/guides/platform/logs.mdx
+++ b/apps/docs/pages/guides/platform/logs.mdx
@@ -1,4 +1,5 @@
import Layout from '~/layouts/DefaultGuideLayout'
+import { logConstants } from 'shared-data'
export const meta = {
id: 'logs',
@@ -249,6 +250,33 @@ from
where h.x_real_ip is not null and r.method = "GET";
```
+### Log Source Reference
+
+Refer to the full field reference for each available source below. Do note that in order to access each nested key, you would need to perform the [necessary unnesting joins](#unnesting-arrays)
+
+<Tabs scrollable size="small" type="underlined" defaultActiveId="edge_logs">
+ {logConstants.schemas.map((schema) => (
+ <TabPanel id={schema.reference} label={schema.name}>
+ <table>
+ <thead>
+ <td className="font-bold">Path</td>
+ <td className="font-bold">Type</td>
+ </thead>
+ <tbody>
+ {schema.fields
+ .sort((a, b) => a.path - b.path)
+ .map((field) => (
+ <tr>
+ <td className="font-mono">{field.path}</td>
+ <td className="font-mono">{field.type}</td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </TabPanel>
+ ))}
+</Tabs>
+
export const Page = ({ children }) => <Layout meta={meta} children={children} />
export default Page
diff --git a/packages/shared-data/index.ts b/packages/shared-data/index.ts
index d469d45cc22..37d9a59f2a2 100644
--- a/packages/shared-data/index.ts
+++ b/packages/shared-data/index.ts
@@ -1,3 +1,4 @@
import tweets from './tweets'
+import logConstants from './logConstants'
-export { tweets }
+export { tweets, logConstants }
diff --git a/packages/shared-data/logConstants.ts b/packages/shared-data/logConstants.ts
new file mode 100644
index 00000000000..79bb8bb0212
--- /dev/null
+++ b/packages/shared-data/logConstants.ts
@@ -0,0 +1,237 @@
+const schemas = [
+ {
+ name: 'API Edge Network',
+ reference: 'edge_logs',
+ fields: [
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'event_message', type: 'string' },
+ { path: 'metadata.request.cf.asOrganization', type: 'string' },
+ { path: 'metadata.request.cf.asn', type: 'number' },
+ { path: 'metadata.request.cf.botManagement.corporateProxy', type: 'boolean' },
+ { path: 'metadata.request.cf.botManagement.detectionIds', type: 'number[]' },
+ { path: 'metadata.request.cf.botManagement.ja3Hash', type: 'string' },
+ { path: 'metadata.request.cf.botManagement.score', type: 'number' },
+ { path: 'metadata.request.cf.botManagement.staticResource', type: 'boolean' },
+ { path: 'metadata.request.cf.botManagement.verifiedBot', type: 'boolean' },
+ { path: 'metadata.request.cf.city', type: 'string' },
+ { path: 'metadata.request.cf.clientTcpRtt', type: 'number' },
+ { path: 'metadata.request.cf.clientTrustScore', type: 'number' },
+ { path: 'metadata.request.cf.colo', type: 'string' },
+ { path: 'metadata.request.cf.continent', type: 'string' },
+ { path: 'metadata.request.cf.country', type: 'string' },
+ { path: 'metadata.request.cf.edgeRequestKeepAliveStatus', type: 'number' },
+ { path: 'metadata.request.cf.httpProtocol', type: 'string' },
+ { path: 'metadata.request.cf.latitude', type: 'string' },
+ { path: 'metadata.request.cf.longitude', type: 'string' },
+ { path: 'metadata.request.cf.metroCode', type: 'string' },
+ { path: 'metadata.request.cf.postalCode', type: 'string' },
+ { path: 'metadata.request.cf.region', type: 'string' },
+ { path: 'metadata.request.cf.timezone', type: 'string' },
+ { path: 'metadata.request.cf.tlsCipher', type: 'string' },
+ { path: 'metadata.request.cf.tlsClientAuth.certPresented', type: 'string' },
+ { path: 'metadata.request.cf.tlsClientAuth.certRevoked', type: 'string' },
+ { path: 'metadata.request.cf.tlsClientAuth.certVerified', type: 'string' },
+ { path: 'metadata.request.cf.tlsExportedAuthenticator.clientFinished', type: 'string' },
+ { path: 'metadata.request.cf.tlsExportedAuthenticator.clientHandshake', type: 'string' },
+ { path: 'metadata.request.cf.tlsExportedAuthenticator.serverFinished', type: 'string' },
+ { path: 'metadata.request.cf.tlsExportedAuthenticator.serverHandshake', type: 'string' },
+ { path: 'metadata.request.cf.tlsVersion', type: 'string' },
+ { path: 'metadata.request.headers.cf_connecting_ip', type: 'string' },
+ { path: 'metadata.request.headers.cf_ipcountry', type: 'string' },
+ { path: 'metadata.request.headers.cf_ray', type: 'string' },
+ { path: 'metadata.request.headers.host', type: 'string' },
+ { path: 'metadata.request.headers.x_client_info', type: 'string' },
+ { path: 'metadata.request.headers.x_forwarded_proto', type: 'string' },
+ { path: 'metadata.request.headers.x_real_ip', type: 'string' },
+ { path: 'metadata.request.host', type: 'string' },
+ { path: 'metadata.request.method', type: 'string' },
+ { path: 'metadata.request.path', type: 'string' },
+ { path: 'metadata.request.protocol', type: 'string' },
+ { path: 'metadata.request.search', type: 'string' },
+ { path: 'metadata.request.url', type: 'string' },
+ { path: 'metadata.response.headers.cf_cache_status', type: 'string' },
+ { path: 'metadata.response.headers.cf_ray', type: 'string' },
+ { path: 'metadata.response.headers.content_location', type: 'string' },
+ { path: 'metadata.response.headers.content_range', type: 'string' },
+ { path: 'metadata.response.headers.content_type', type: 'string' },
+ { path: 'metadata.response.headers.date', type: 'string' },
+ { path: 'metadata.response.headers.sb_gateway_version', type: 'string' },
+ { path: 'metadata.response.headers.transfer_encoding', type: 'string' },
+ { path: 'metadata.response.headers.x_kong_proxy_latency', type: 'string' },
+ { path: 'metadata.response.origin_time', type: 'number' },
+ { path: 'metadata.response.status_code', type: 'number' },
+ ],
+ },
+ {
+ name: 'Auth',
+ reference: 'auth_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.auth_event.action', type: 'string' },
+ { path: 'metadata.auth_event.actor_id', type: 'string' },
+ { path: 'metadata.auth_event.actor_username', type: 'string' },
+ { path: 'metadata.auth_event.log_type', type: 'string' },
+ { path: 'metadata.auth_event.traits.provider', type: 'string' },
+ { path: 'metadata.auth_event.traits.user_email', type: 'string' },
+ { path: 'metadata.auth_event.traits.user_id', type: 'string' },
+ { path: 'metadata.auth_event.traits.user_phone', type: 'string' },
+ { path: 'metadata.component', type: 'string' },
+ { path: 'metadata.duration', type: 'number' },
+ { path: 'metadata.host', type: 'string' },
+ { path: 'metadata.level', type: 'string' },
+ { path: 'metadata.method', type: 'string' },
+ { path: 'metadata.msg', type: 'string' },
+ { path: 'metadata.path', type: 'string' },
+ { path: 'metadata.referer', type: 'string' },
+ { path: 'metadata.remote_addr', type: 'string' },
+ { path: 'metadata.status', type: 'number' },
+ { path: 'metadata.timestamp', type: 'string' },
+ ],
+ },
+ {
+ name: 'Storage',
+ reference: 'storage_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.context.host', type: 'string' },
+ { path: 'metadata.context.pid', type: 'number' },
+ { path: 'metadata.level', type: 'string' },
+ { path: 'metadata.project', type: 'string' },
+ { path: 'metadata.req.headers.accept', type: 'string' },
+ { path: 'metadata.req.headers.cf_connecting_ip', type: 'string' },
+ { path: 'metadata.req.headers.cf_ray', type: 'string' },
+ { path: 'metadata.req.headers.content_length', type: 'string' },
+ { path: 'metadata.req.headers.content_type', type: 'string' },
+ { path: 'metadata.req.headers.host', type: 'string' },
+ { path: 'metadata.req.headers.referer', type: 'string' },
+ { path: 'metadata.req.headers.user_agent', type: 'string' },
+ { path: 'metadata.req.headers.x_client_info', type: 'string' },
+ { path: 'metadata.req.headers.x_forwarded_proto', type: 'string' },
+ { path: 'metadata.req.hostname', type: 'string' },
+ { path: 'metadata.req.method', type: 'string' },
+ { path: 'metadata.req.remoteAddress', type: 'string' },
+ { path: 'metadata.req.remotePort', type: 'number' },
+ { path: 'metadata.req.url', type: 'string' },
+ { path: 'metadata.reqId', type: 'string' },
+ { path: 'metadata.res.statusCode', type: 'number' },
+ { path: 'metadata.responseTime', type: 'number' },
+ { path: 'metadata.tenantId', type: 'string' },
+ ],
+ },
+ {
+ name: 'Function Edge',
+ reference: 'function_edge_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.deployment_id', type: 'string' },
+ { path: 'metadata.execution_time_ms', type: 'number' },
+ { path: 'metadata.function_id', type: 'string' },
+ { path: 'metadata.project_ref', type: 'string' },
+ { path: 'metadata.request.headers.accept', type: 'string' },
+ { path: 'metadata.request.headers.content_length', type: 'string' },
+ { path: 'metadata.request.headers.host', type: 'string' },
+ { path: 'metadata.request.headers.user_agent', type: 'string' },
+ { path: 'metadata.request.host', type: 'string' },
+ { path: 'metadata.request.method', type: 'string' },
+ { path: 'metadata.request.pathname', type: 'string' },
+ { path: 'metadata.request.protocol', type: 'string' },
+ { path: 'metadata.request.url', type: 'string' },
+ { path: 'metadata.response.headers.content_length', type: 'string' },
+ { path: 'metadata.response.headers.content_type', type: 'string' },
+ { path: 'metadata.response.headers.date', type: 'string' },
+ { path: 'metadata.response.headers.server', type: 'string' },
+ { path: 'metadata.response.headers.vary', type: 'string' },
+ { path: 'metadata.response.status_code', type: 'number' },
+ { path: 'metadata.version', type: 'string' },
+ ],
+ },
+ {
+ name: 'Function Runtime',
+ reference: 'function_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.deployment_id', type: 'string' },
+ { path: 'metadata.event_type', type: 'string' },
+ { path: 'metadata.execution_id', type: 'string' },
+ { path: 'metadata.function_id', type: 'string' },
+ { path: 'metadata.level', type: 'string' },
+ { path: 'metadata.project_ref', type: 'string' },
+ { path: 'metadata.region', type: 'string' },
+ { path: 'metadata.timestamp', type: 'string' },
+ { path: 'metadata.version', type: 'string' },
+ ],
+ },
+ {
+ name: 'Postgres',
+ reference: 'postgres_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.host', type: 'string' },
+ { path: 'metadata.parsed.backend_type', type: 'string' },
+ { path: 'metadata.parsed.command_tag', type: 'string' },
+ { path: 'metadata.parsed.connection_from', type: 'string' },
+ { path: 'metadata.parsed.database_name', type: 'string' },
+ { path: 'metadata.parsed.error_severity', type: 'string' },
+ { path: 'metadata.parsed.process_id', type: 'number' },
+ { path: 'metadata.parsed.query_id', type: 'number' },
+ { path: 'metadata.parsed.session_id', type: 'string' },
+ { path: 'metadata.parsed.session_line_num', type: 'number' },
+ { path: 'metadata.parsed.session_start_time', type: 'string' },
+ { path: 'metadata.parsed.sql_state_code', type: 'string' },
+ { path: 'metadata.parsed.timestamp', type: 'string' },
+ { path: 'metadata.parsed.transaction_id', type: 'number' },
+ { path: 'metadata.parsed.user_name', type: 'string' },
+ { path: 'metadata.parsed.virtual_transaction_id', type: 'string' },
+ ],
+ },
+ {
+ name: 'Realtime',
+ reference: 'realtime_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.level', type: 'string' },
+ { path: 'metadata.measurements.connected', type: 'number' },
+ { path: 'metadata.measurements.connected_cluster', type: 'number' },
+ { path: 'metadata.measurements.limit', type: 'number' },
+ { path: 'metadata.measurements.sum', type: 'number' },
+ { path: 'metadata.external_id', type: 'string' },
+ ],
+ },
+ {
+ name: 'PostgREST',
+ reference: 'postgrest_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.host', type: 'string' },
+ ],
+ },
+ {
+ name: 'PgBouncer',
+ reference: 'pgbouncer_logs',
+ fields: [
+ { path: 'event_message', type: 'string' },
+ { path: 'id', type: 'string' },
+ { path: 'timestamp', type: 'datetime' },
+ { path: 'metadata.host', type: 'string' },
+ ],
+ },
+]
+
+export default {
+ schemas,
+}
diff --git a/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
index d039b002740..24e42b749fa 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@ -2,6 +2,12 @@ import { Filters, LogData, LogsEndpointParams, LogsTableName, SQL_FILTER_TEMPLAT
import dayjs, { Dayjs } from 'dayjs'
import { get } from 'lodash'
import { StripeSubscription } from 'components/interfaces/Billing'
+import { useMonaco } from '@monaco-editor/react'
+import logConstants from 'shared-data/logConstants'
+import BackwardIterator from 'components/to-be-cleaned/SqlEditor/BackwardIterator'
+import { uniqBy } from 'lodash'
+import { useEffect } from 'react'
+
/**
* Convert a micro timestamp from number/string to iso timestamp
@@ -264,3 +270,84 @@ export const ensureNoTimestampConflict = (
return [nextStart, nextEnd]
}
}
+
+
+/**
+ * Adds SQL code hints to logs explorer code editor
+ */
+export const useEditorHints = ()=>{
+ const monaco = useMonaco()
+
+ useEffect(() => {
+ if (monaco) {
+ const competionProvider = {
+ triggerCharacters: ['`', ' ', '.'],
+ provideCompletionItems: function (model: any, position: any, context: any) {
+ let iterator = new BackwardIterator(model, position.column - 2, position.lineNumber - 1)
+ if (iterator.isNextDQuote()) return { suggestions: [] }
+ let suggestions: { label: string; kind: any; insertText: string }[] = []
+
+ let schemasInUse = logConstants.schemas.filter((schema) =>
+ iterator._text.includes(schema.reference)
+ )
+ if (schemasInUse.length === 0) {
+ schemasInUse = logConstants.schemas
+ }
+
+ if (iterator.isNextPeriod()) {
+ // should be nested key reference, suggest all tail endings of available fields
+ const fields = schemasInUse.flatMap((schema) => schema.fields)
+ const trailingKeys = fields.flatMap((field) => {
+ const [_head, ...rest] = field.path.split('.')
+ return rest
+ })
+
+ const trailingToAdd = trailingKeys.map((key) => ({
+ label: key,
+ kind: monaco.languages.CompletionItemKind.Property,
+ insertText: key,
+ }))
+ suggestions = suggestions.concat(trailingToAdd)
+ }
+
+ if (context.triggerCharacter === '`' || context.triggerCharacter === ' ') {
+ // should be reference or start of key
+ const referencesToAdd = logConstants.schemas.map((schema) => ({
+ label: schema.reference,
+ kind: monaco.languages.CompletionItemKind.Class,
+ insertText: schema.reference,
+ }))
+
+ const fields = schemasInUse.flatMap((schema) => schema.fields)
+ const leadingKeys = fields.flatMap((field) => {
+ const splitPath = field.path.split('.')
+
+ return splitPath.slice(0, -1)
+ })
+
+ const leadingToAdd = leadingKeys.map((key) => ({
+ label: key,
+ kind: monaco.languages.CompletionItemKind.Property,
+ insertText: key,
+ }))
+ suggestions = suggestions.concat(leadingToAdd)
+ suggestions = suggestions.concat(referencesToAdd)
+ }
+ return {
+ suggestions: uniqBy(suggestions, 'label'),
+ }
+ },
+ }
+
+ // register completion item provider for pgsql
+ const completeProvider = monaco.languages.registerCompletionItemProvider(
+ 'pgsql',
+ competionProvider
+ )
+
+ return () => {
+ completeProvider.dispose()
+ }
+ }
+ }, [monaco])
+}
\ No newline at end of file
diff --git a/studio/pages/project/[ref]/logs/explorer/index.tsx b/studio/pages/project/[ref]/logs/explorer/index.tsx
index 2a93f527629..6dbf40db641 100644
--- a/studio/pages/project/[ref]/logs/explorer/index.tsx
+++ b/studio/pages/project/[ref]/logs/explorer/index.tsx
@@ -23,12 +23,14 @@ import {
LogTemplate,
maybeShowUpgradePrompt,
TEMPLATES,
+ useEditorHints,
} from 'components/interfaces/Settings/Logs'
import { useUpgradePrompt } from 'hooks/misc/useUpgradePrompt'
import UpgradePrompt from 'components/interfaces/Settings/Logs/UpgradePrompt'
import LogsExplorerHeader from 'components/ui/Logs/LogsExplorerHeader'
export const LogsExplorerPage: NextPageWithLayout = () => {
+ useEditorHints()
const { ui, content } = useStore()
const router = useRouter()
const { ref: projectRef, q, ite, its } = useParams()
@@ -38,7 +40,6 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
const [warnings, setWarnings] = useState<LogsWarning[]>([])
const { data: subscription } = useProjectSubscriptionQuery({ projectRef })
const tier = subscription?.tier
-
const [{ params, logData, error, isLoading }, { changeQuery, runQuery, setParams }] =
useLogsQuery(projectRef as string, {
iso_timestamp_start: its ? (its as string) : undefined,
@@ -146,6 +147,7 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
}
}
+
return (
<div className="w-full h-full px-5 py-6 mx-auto">
<LogsExplorerHeader />