Skip to content

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

Update unified logs queries to fetch status, method and pathname properly for storage logs (#50465)

Joshen Limbe9ec25270c0
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
index 3eee4bca3bf..ad779e652ff 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
@@ -76,8 +76,12 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
"log_attributes['source'] IN ('worker_ingress_logs','worker_guest_logs','worker_api_logs')"
expect(sql).toContain(`WHEN ${workerCondition} THEN null`)
- expect(sql).toContain(`if(${workerCondition}, null, log_attributes['request.method'])`)
- expect(sql).toContain(`if(${workerCondition}, null, log_attributes['request.path'])`)
+ expect(sql).toContain(
+ `if(${workerCondition}, null, if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method']))`
+ )
+ expect(sql).toContain(
+ `if(${workerCondition}, null, if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path']))`
+ )
expect(sql).toContain(`if(${workerCondition}, log_attributes, map()) AS metadata`)
})
@@ -96,17 +100,25 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
const sql = getUnifiedLogsQuery(
withFilters('method:eq:GET', 'status:eq:401', 'pathname:eq:/customers')
)
- expect(sql).toContain(`log_attributes['request.method'] IN ('GET')`)
+ // Method/pathname filters wrap the if() that picks the gateway or
+ // storage-service attribute key so storage rows match too.
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method'])) IN ('GET')`
+ )
// Status filter wraps the CASE that picks HTTP code or Postgres SQLSTATE
// so e.g. '00000' matches postgres success rows.
expect(sql).toContain(`log_attributes['parsed.sql_state_code']`)
expect(sql).toMatch(/END\) IN \('401'\)/)
- expect(sql).toContain(`log_attributes['request.path'] LIKE '%/customers%'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) LIKE '%/customers%'`
+ )
})
it('flips IN to NOT IN when the operator is `<>`', () => {
const sql = getUnifiedLogsQuery(withFilters('method:neq:GET', 'status:neq:401'))
- expect(sql).toContain(`log_attributes['request.method'] NOT IN ('GET')`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method'])) NOT IN ('GET')`
+ )
expect(sql).toMatch(/END\) NOT IN \('401'\)/)
})
@@ -115,45 +127,73 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
// `response.status_code`, so without this their 4xx/5xx classify as
// success and the severity filter returns nothing.
const sql = getUnifiedLogsQuery(withFilters('log_type:eq:auth'))
+ expect(sql).toContain(`WHEN source = 'auth_logs' THEN log_attributes['status']`)
+ })
+
+ it('reads the HTTP status from log_attributes[res.statusCode] for storage rows', () => {
+ // Storage-service logs expose their status under `res.statusCode`, not the
+ // gateway's `response.status_code`.
+ const sql = getUnifiedLogsQuery(withFilters('log_type:eq:storage'))
+ expect(sql).toContain(`WHEN source = 'storage_logs' THEN log_attributes['res.statusCode']`)
+ })
+
+ it('reads the method/pathname from log_attributes[req.method/req.url] for storage rows', () => {
+ // Storage-service logs expose method/path under `req.method` / `req.url`,
+ // not the gateway's `request.method` / `request.path`.
+ const sql = getUnifiedLogsQuery(withFilters('log_type:eq:storage'))
+ expect(sql).toContain(
+ `if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method'])`
+ )
expect(sql).toContain(
- `if(source = 'auth_logs', log_attributes['status'], log_attributes['response.status_code'])`
+ `if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])`
)
})
it('flips LIKE to NOT LIKE when the pathname/host operator is `<>`', () => {
const sql = getUnifiedLogsQuery(withFilters('pathname:neq:/health', 'host:neq:cdn.foo'))
- expect(sql).toContain(`log_attributes['request.path'] NOT LIKE '%/health%'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) NOT LIKE '%/health%'`
+ )
expect(sql).toContain(`log_attributes['request.url'] NOT LIKE '%cdn.foo%'`)
})
it('emits ILIKE for pathname `~~*`, case-insensitive unlike the `=` LIKE behavior', () => {
const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:Customers'))
- expect(sql).toContain(`log_attributes['request.path'] ILIKE '%Customers%'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) ILIKE '%Customers%'`
+ )
})
it('emits NOT ILIKE for pathname `!~~*` so rows matching the term are excluded', () => {
const sql = getUnifiedLogsQuery(withFilters('pathname:notilike:health'))
- expect(sql).toContain(`log_attributes['request.path'] NOT ILIKE '%health%'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) NOT ILIKE '%health%'`
+ )
})
it('joins multiple pathname NOT ILIKE values with AND (row must match none)', () => {
const sql = getUnifiedLogsQuery(
withFilters('pathname:notilike:health', 'pathname:notilike:metrics')
)
- expect(sql).toMatch(
- /log_attributes\['request\.path'\] NOT ILIKE '%health%' AND log_attributes\['request\.path'\] NOT ILIKE '%metrics%'/
+ const pathExpr = `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path']))`
+ expect(sql).toContain(
+ `${pathExpr} NOT ILIKE '%health%' AND ${pathExpr} NOT ILIKE '%metrics%'`
)
})
it('passes through user-supplied `%` wildcards on pathname ILIKE without double-wrapping', () => {
const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:foo%'))
- expect(sql).toContain(`log_attributes['request.path'] ILIKE 'foo%'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) ILIKE 'foo%'`
+ )
expect(sql).not.toContain(`'%foo%%'`)
})
it('passes through user-supplied `_` wildcards on pathname ILIKE without wrapping', () => {
const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:fo_bar'))
- expect(sql).toContain(`log_attributes['request.path'] ILIKE 'fo_bar'`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) ILIKE 'fo_bar'`
+ )
expect(sql).not.toContain(`'%fo_bar%'`)
})
@@ -330,9 +370,7 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
it('buckets auth rows by their log_attributes[status] so 4xx/5xx are not counted as success', () => {
const sql = getLogsChartQuery(baseSearch)
- expect(sql).toContain(
- `if(source = 'auth_logs', log_attributes['status'], log_attributes['response.status_code'])`
- )
+ expect(sql).toContain(`WHEN source = 'auth_logs' THEN log_attributes['status']`)
})
it('does not classify Compute rows into a severity bucket', () => {
@@ -443,13 +481,17 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
// Postgres-only and rejected by both analytics engines. analyticsLiteral
// doubles the backslash inside plain `'…'` delimiters instead.
const sql = getUnifiedLogsQuery(withFilters('method:eq:a\\b'))
- expect(sql).toContain(`log_attributes['request.method'] IN ('a\\\\b')`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method'])) IN ('a\\\\b')`
+ )
expect(sql).not.toContain(`E'a`)
})
it("escapes single quotes by doubling them ('' rather than \\')", () => {
const sql = getUnifiedLogsQuery(withFilters("method:eq:GET' OR '1'='1"))
- expect(sql).toContain(`log_attributes['request.method'] IN ('GET'' OR ''1''=''1')`)
+ expect(sql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.method'], log_attributes['request.method'])) IN ('GET'' OR ''1''=''1')`
+ )
})
})
})
@@ -529,7 +571,9 @@ describe('pathname ILIKE prefix matching (cross-builder)', () => {
const clickhouseSql = getUnifiedLogsQuery(withFilters('pathname:ilike:foo%'))
const bqSql = getUnifiedLogsQueryBQ(withFilters('pathname:ilike:foo%'))
- expect(clickhouseSql).toContain(`log_attributes['request.path'] ILIKE 'foo%'`)
+ expect(clickhouseSql).toContain(
+ `(if(source = 'storage_logs', log_attributes['req.url'], log_attributes['request.path'])) ILIKE 'foo%'`
+ )
expect(bqSql).toContain("LOWER(`pathname`) LIKE LOWER('foo%')")
// Neither backend should have double-wrapped the wildcard into a
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
index 777a6e49ba3..27d79b6f727 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
@@ -39,12 +39,22 @@ const ATTR = {
} as const
// The HTTP status code lives under different OTEL attribute keys per service:
-// gateway rows (edge / postgrest / storage / edge function) expose it as
-// `response.status_code`, while auth-service rows expose it as `status`. This
-// normalizes the two so both the displayed status and the derived severity are
-// correct for auth logs (which would otherwise have an empty status and fall
+// gateway rows (edge / postgrest / edge function) expose it as
+// `response.status_code`, auth-service rows expose it as `status`, and
+// storage-service rows expose it as `res.statusCode`. This normalizes the
+// three so both the displayed status and the derived severity are correct
+// for those sources (which would otherwise have an empty status and fall
// back to their `severity_text` of INFO, classifying every 4xx/5xx as success).
-const HTTP_STATUS_EXPR: SafeLogSqlFragment = safeSql`if(source = 'auth_logs', log_attributes['status'], ${ATTR.status})`
+const HTTP_STATUS_EXPR: SafeLogSqlFragment = safeSql`CASE
+ WHEN source = 'auth_logs' THEN log_attributes['status']
+ WHEN source = 'storage_logs' THEN log_attributes['res.statusCode']
+ ELSE ${ATTR.status}
+ END`
+
+// storage-service rows expose method/path under `req.method` / `req.url`
+// rather than the gateway's `request.method` / `request.path`.
+const HTTP_METHOD_EXPR: SafeLogSqlFragment = safeSql`if(source = 'storage_logs', log_attributes['req.method'], ${ATTR.method})`
+const HTTP_PATH_EXPR: SafeLogSqlFragment = safeSql`if(source = 'storage_logs', log_attributes['req.url'], ${ATTR.path})`
/**
* Condition that matches rows belonging to a given log_type. Mirrors the
@@ -99,8 +109,8 @@ const STATUS_EXPR: SafeLogSqlFragment = safeSql`CASE
ELSE toString((${HTTP_STATUS_EXPR}))
END`
-const METHOD_EXPR: SafeLogSqlFragment = safeSql`if(${WORKER_LOG_SOURCE_CONDITION}, null, ${ATTR.method})`
-const PATHNAME_EXPR: SafeLogSqlFragment = safeSql`if(${WORKER_LOG_SOURCE_CONDITION}, null, ${ATTR.path})`
+const METHOD_EXPR: SafeLogSqlFragment = safeSql`if(${WORKER_LOG_SOURCE_CONDITION}, null, ${HTTP_METHOD_EXPR})`
+const PATHNAME_EXPR: SafeLogSqlFragment = safeSql`if(${WORKER_LOG_SOURCE_CONDITION}, null, ${HTTP_PATH_EXPR})`
const METADATA_EXPR: SafeLogSqlFragment = safeSql`if(${WORKER_LOG_SOURCE_CONDITION}, log_attributes, map())`
// SQL expression for derived `level`. Used inline (not as alias reference)
@@ -174,23 +184,29 @@ const translateFilter = (
// No simple raw column for level; reference the inline CASE expression.
return safeSql`(${LEVEL_EXPR}) ${inOp} ${inList(values)}`
case 'method':
- return safeSql`${ATTR.method} ${inOp} ${inList(values)}`
+ // Match the displayed method: gateway rows read `request.method`,
+ // storage rows read `req.method`. Inline HTTP_METHOD_EXPR so filtering
+ // picks up storage rows too.
+ return safeSql`(${HTTP_METHOD_EXPR}) ${inOp} ${inList(values)}`
case 'status':
// Match the displayed status: HTTP response code for gateway rows,
// Postgres SQLSTATE for postgres rows. Inline STATUS_EXPR so e.g.
// filtering on '00000' picks up postgres success rows.
return safeSql`(${STATUS_EXPR}) ${inOp} ${inList(values)}`
case 'pathname': {
+ // Match the displayed pathname: gateway rows read `request.path`,
+ // storage rows read `req.url`. Inline HTTP_PATH_EXPR so filtering
+ // picks up storage rows too.
if (operator === '~~*' || operator === '!~~*') {
const op = operator === '~~*' ? ILIKE_OP : NOT_ILIKE_OP
const join = operator === '!~~*' ? ' AND ' : ' OR '
return safeSql`(${joinSqlFragments(
- values.map((v) => safeSql`${ATTR.path} ${op} ${lit(wrapIlikePattern(v))}`),
+ values.map((v) => safeSql`(${HTTP_PATH_EXPR}) ${op} ${lit(wrapIlikePattern(v))}`),
join
)})`
}
return safeSql`(${joinSqlFragments(
- values.map((v) => safeSql`${ATTR.path} ${likeOp} ${lit('%' + v + '%')}`),
+ values.map((v) => safeSql`(${HTTP_PATH_EXPR}) ${likeOp} ${lit('%' + v + '%')}`),
joinAndOr
)})`
}