Skip to content
Merged
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
27 changes: 19 additions & 8 deletions checks/_helpers/observability.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ export function workersWindow(sinceIso, untilIso) {
const FAILURE_DETAIL_LIMIT = 300

function failure(service, status, detail) {
const text = detail.trim()
if (!text)
// Cut by code point, so the limit never splits a surrogate pair.
const characters = Array.from(detail.trim())
if (!characters.length)
return { _tag: 'Err', reason: `${service} returned HTTP ${status}.` }
const bounded = text.length > FAILURE_DETAIL_LIMIT ? `${text.slice(0, FAILURE_DETAIL_LIMIT)}…` : text
const bounded = characters.length > FAILURE_DETAIL_LIMIT ? `${characters.slice(0, FAILURE_DETAIL_LIMIT).join('')}…` : characters.join('')
return { _tag: 'Err', reason: `${service} returned HTTP ${status}: ${bounded}` }
}

function errorMessages(errors) {
return Array.isArray(errors)
? errors.map(error => error?.message).filter(message => typeof message === 'string')
: []
}

function parseJson(text) {
try {
return { _tag: 'Ok', value: JSON.parse(text) }
Expand All @@ -98,19 +105,23 @@ export function parseWorkersResponse(status, text) {
const errors = json._tag === 'Ok' ? json.value?.errors : undefined
const hasErrors = errors != null && (!Array.isArray(errors) || errors.length > 0)
if (!isSuccess(status) || json._tag === 'Err' || hasErrors) {
const messages = Array.isArray(errors) ? errors.map(error => error?.message).filter(message => typeof message === 'string') : []
return failure('Cloudflare GraphQL', status, messages.join('; '))
return failure('Cloudflare GraphQL', status, errorMessages(errors).join('; '))
}
const rows = json.value?.data?.viewer?.accounts?.[0]?.workersInvocationsAdaptive
return Array.isArray(rows) ? { _tag: 'Ok', rows } : { _tag: 'Err', reason: 'Cloudflare Worker evidence is unavailable.' }
}

/** Parse an Analytics Engine SQL response into its rows, or the reason it failed. */
export function parseAnalyticsEngineResponse(status, text) {
// The SQL API answers a rejected query with a plain-text message.
if (!isSuccess(status))
return failure('Analytics Engine', status, text)
const json = parseJson(text)
if (!isSuccess(status)) {
// A rejected SQL query answers in plain text, the API gateway in a JSON
// envelope, and a proxy in HTML, whose markup says nothing useful.
const detail = json._tag === 'Ok'
? errorMessages(json.value?.errors).join('; ')
: text.trimStart().startsWith('<') ? '' : text
return failure('Analytics Engine', status, detail)
}
const rows = json._tag === 'Ok' ? json.value?.data : undefined
return Array.isArray(rows) ? { _tag: 'Ok', rows } : { _tag: 'Err', reason: 'Analytics Engine copy evidence is unavailable.' }
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/daily-checkin-observability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,29 @@ describe('daily check-in observability', () => {
.toEqual({ _tag: 'Ok', rows })
})

// GraphQL sends `errors: null` beside data when a query succeeds.
it('returns the Worker rows of a response whose errors are null', () => {
const rows = [{ dimensions: { scriptName: 'skilld-dev', status: 'success' }, sum: { requests: 3 } }]
expect(parseWorkersResponse(200, JSON.stringify({ data: { viewer: { accounts: [{ workersInvocationsAdaptive: rows }] } }, errors: null })))
.toEqual({ _tag: 'Ok', rows })
})

it('names the Analytics Engine message of a failed query', () => {
expect(parseAnalyticsEngineResponse(422, 'unknown column: blob9'))
.toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 422: unknown column: blob9' })
})

it('names the API error message of a rejected Analytics Engine request', () => {
const body = JSON.stringify({ success: false, errors: [{ code: 10000, message: 'Authentication error' }], messages: [], result: null })
expect(parseAnalyticsEngineResponse(401, body))
.toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 401: Authentication error' })
})

it('keeps only the status of an Analytics Engine error page', () => {
expect(parseAnalyticsEngineResponse(502, '<!DOCTYPE html><html><body>Bad gateway</body></html>'))
.toEqual({ _tag: 'Err', reason: 'Analytics Engine returned HTTP 502.' })
})

it('returns the Analytics Engine rows of a successful query', () => {
expect(parseAnalyticsEngineResponse(200, JSON.stringify({ data: [{ copies: 2 }] })))
.toEqual({ _tag: 'Ok', rows: [{ copies: 2 }] })
Expand Down
Loading