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
8 changes: 6 additions & 2 deletions docs/account-archive-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ default-off `snapshot_request_logs_only` mode. It requires the exact current
main SHA, the approved main ref, `run_readonly_diagnostic=true`, and a past UTC
window of at most 30 minutes. It cannot be combined with a schema inspection.
It reads bounded Scheduler completion and Cloud Run request logs for the fixed
account-facts sync target, then reads application logs only for a uniquely
identified request trace. Truncated or inaccessible reads remain explicit; the
account-facts sync target, then reads application logs for a uniquely
identified request trace. If none match, it also reads bounded text logs with
the fixed account-facts marker from the same service and window. These are
unassociated candidates: even a single candidate does not establish that it
belongs to the request. Multiple candidates are ambiguous and truncated or
inaccessible reads remain explicit; the
Scheduler-to-request association is based only on target and time, not a
proven common invocation. Only fixed categories, HTTP status codes and counts
are printed. No Scheduler job, broker request or sync is invoked.
Expand Down
55 changes: 41 additions & 14 deletions scripts/diagnose_snapshot_request_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def _resource(row, kind, project, service, location):
== (service if kind == 'cloud_run_revision' else service + '-account-facts-sync-scheduler'))


def _classification(row):
msg = row.get('textPayload')
if not isinstance(msg, str):
return None
client = re.search(r'account_facts_sync client failed exc_type=\w+ reason=([a-z_]+) stage=([a-z_]+) http_status=(None|[1-5][0-9]{2})(?:\s|$)', msg)
other = re.search(r'account_facts_sync (?:collect|build|publish) failed reason=([a-z_]+)(?:\s|$)', msg)
if client and client.group(1) in REASONS and client.group(2) in STAGES:
return (client.group(1), client.group(2),
None if client.group(3) == 'None' else int(client.group(3)))
if other and other.group(1) in REASONS:
return (other.group(1), 'unclassified', None)
return None


def summarize(rows, app_rows, project, service, region, run_region=None):
run_region = run_region or region
job = f'projects/{project}/locations/{region}/jobs/{service}-account-facts-sync-scheduler'
Expand Down Expand Up @@ -94,18 +108,7 @@ def summarize(rows, app_rows, project, service, region, run_region=None):
matched = [row for row in app_rows if _resource(row, 'cloud_run_revision', project, service, run_region)
and row.get('trace') == trace and not row.get('httpRequest')]
result['app_match_count'] = len(matched)
classifications = []
for row in matched:
msg = row.get('textPayload')
if not isinstance(msg, str):
continue
client = re.search(r'account_facts_sync client failed exc_type=\w+ reason=([a-z_]+) stage=([a-z_]+) http_status=(None|[1-5][0-9]{2})(?:\s|$)', msg)
other = re.search(r'account_facts_sync (?:collect|build|publish) failed reason=([a-z_]+)(?:\s|$)', msg)
if client and client.group(1) in REASONS and client.group(2) in STAGES:
classifications.append((client.group(1), client.group(2),
None if client.group(3) == 'None' else int(client.group(3))))
elif other and other.group(1) in REASONS:
classifications.append((other.group(1), 'unclassified', None))
classifications = [value for row in matched if (value := _classification(row)) is not None]
if len(matched) == 1 and len(classifications) == 1:
result['app_reason'], result['app_stage'], result['app_http_status'] = classifications[0]
return result
Expand Down Expand Up @@ -164,7 +167,30 @@ def run(project, service, region, run_region, start, end, *, read=_read_logs):
output['app_read_status'] = app_status
if app_status != 'ok':
return {**output, **partial}
return {**output, **summarize(rows, app_rows, project, service, region, run_region)}
result = {**output, **summarize(rows, app_rows, project, service, region, run_region)}
if result['app_match_count']:
return result
candidate_filter = (f'{window} AND resource.type="cloud_run_revision" '
f'AND resource.labels.project_id="{project}" AND resource.labels.location="{run_region}" '
f'AND resource.labels.service_name="{service}" AND textPayload:"account_facts_sync"')
candidate_status, candidates = read(candidate_filter, project)
result.update(candidate_read_status=candidate_status, candidate_count=None,
candidate_relation='unknown', candidate_reason='unknown',
candidate_stage='unknown', candidate_http_status=None)
if candidate_status != 'ok':
return result
candidates = [row for row in candidates if _resource(row, 'cloud_run_revision', project, service, run_region)
and not row.get('httpRequest') and isinstance(row.get('textPayload'), str)
and 'account_facts_sync' in row['textPayload']]
result['candidate_count'] = len(candidates)
if len(candidates) == 1:
result['candidate_relation'] = 'same_service_and_window_only'
classification = _classification(candidates[0])
if classification is not None:
result['candidate_reason'], result['candidate_stage'], result['candidate_http_status'] = classification
elif len(candidates) > 1:
result['candidate_relation'] = 'ambiguous'
return result


def main():
Expand All @@ -177,7 +203,8 @@ def main():
except Exception:
output = {'status': 'unavailable'}
print(json.dumps(output, sort_keys=True))
return 0 if output['status'] == 'ok' and output.get('app_read_status') not in {'permission_denied', 'unavailable', 'truncated'} else 1
failures = {'permission_denied', 'unavailable', 'truncated'}
return 0 if output['status'] == 'ok' and output.get('app_read_status') not in failures and output.get('candidate_read_status') not in failures else 1


if __name__ == '__main__':
Expand Down
35 changes: 35 additions & 0 deletions tests/test_snapshot_log_diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ def forbidden(*_args):
run(PROJECT, SERVICE, 'us-central1', 'us-central1',
'2026-10-30T01:00:00Z', '2026-10-30T02:00:01Z', read=forbidden)

def test_untraced_app_log_is_only_an_unassociated_candidate(self):
calls = []
untraced = app(); del untraced['trace']
def read(query, project):
calls.append(query)
return ('ok', [[scheduler(), request()], [], [untraced]][len(calls) - 1])
result = run(PROJECT, SERVICE, 'us-central1', 'us-central1',
'2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read)
self.assertEqual(result['app_reason'], 'unknown')
self.assertEqual(result['candidate_relation'], 'same_service_and_window_only')
self.assertEqual(result['candidate_reason'], 'cache_missing')
self.assertEqual(result['candidate_count'], 1)
self.assertIn('textPayload', calls[2])

def test_multiple_or_truncated_candidates_never_classified(self):
for status, rows in [('ok', [app(), app()]), ('truncated', [app()])]:
calls = []
def read(query, project):
calls.append(query)
return [('ok', [scheduler(), request()]), ('ok', []), (status, rows)][len(calls) - 1]
result = run(PROJECT, SERVICE, 'us-central1', 'us-central1',
'2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read)
self.assertEqual(result['candidate_reason'], 'unknown')

def test_unrecognized_candidate_never_exposes_payload(self):
calls = []
unsafe = app('secret-account-123'); del unsafe['trace']
def read(query, project):
calls.append(query)
return ('ok', [[scheduler(), request()], [], [unsafe]][len(calls) - 1])
result = run(PROJECT, SERVICE, 'us-central1', 'us-central1',
'2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read)
self.assertEqual(result['candidate_reason'], 'unknown')
self.assertNotIn('secret-account-123', str(result))


if __name__ == '__main__':
unittest.main()
Loading