[improve/#64] Gemini 사용량 및 요청 로그 진단 보강#65
Conversation
Constraint: 운영에서 토큰 사용량 급증 원인을 원문 없이 확인해야 함 Confidence: high Scope-risk: narrow Directive: Gemini 호출 추가 시 generate_content_with_retry를 경유해 usage 로그를 유지할 것 Tested: uv run pytest tests/test_logging_middleware.py tests/test_gemini_retry.py; uv run ruff check targeted files Not-tested: 실제 Gemini API 과금 대시보드 반영 지연
Constraint: usage metadata 로그는 비용 추적 근거라 회귀 테스트가 필요함 Confidence: high Scope-risk: narrow Directive: usage 로그 필드명을 바꿀 때 테스트 기대 문자열도 함께 갱신할 것 Tested: uv run pytest tests/test_logging_middleware.py tests/test_gemini_retry.py; uv run ruff check targeted files Not-tested: 실제 외부 Gemini 응답 객체 전체 필드 변형
Constraint: 운영 로그에서 diff, 발화 원문, 토큰류 비밀이 그대로 노출되면 안 됨 Rejected: 전체 body 원문 로깅 | 민감정보와 긴 diff가 로그 파일에 남을 수 있음 Confidence: high Scope-risk: moderate Directive: 새 요청 필드에 원문/비밀이 담기면 _SENSITIVE_KEY_PARTS 또는 _VERBOSE_KEY_PARTS에 추가할 것 Tested: uv run pytest tests/test_logging_middleware.py tests/test_gemini_retry.py; uv run ruff check targeted files Not-tested: 외부 로그 수집기에서의 필드별 인덱싱 동작
Constraint: 로깅 미들웨어는 운영 진단용이면서 개인정보와 비밀을 남기면 안 됨 Confidence: high Scope-risk: narrow Directive: body 로깅 정책을 바꿀 때 JSON, multipart, request body 재사용 테스트를 유지할 것 Tested: uv run pytest tests/test_logging_middleware.py tests/test_gemini_retry.py; uv run ruff check targeted files Not-tested: 대용량 실제 오디오 업로드 전체 경로
Constraint: 전사 보정 비용과 실패 원인을 segment 수와 화자 수 기준으로 추적해야 함 Confidence: high Scope-risk: narrow Directive: 전사 보정 입력 원문은 로그에 남기지 말고 집계 맥락만 남길 것 Tested: uv run pytest tests/test_logging_middleware.py tests/test_gemini_retry.py; uv run ruff check targeted files Not-tested: 운영 회의 녹음 기반 실제 전사 보정 호출
|
Warning Review limit reached
More reviews will be available in 36 minutes and 27 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough이 PR은 Gemini API 호출 사용량과 API 요청 본문을 진단 목적으로 안전하게 로깅하는 기능을 추가합니다. usage_metadata 구조화 로깅, 요청 본문 민감정보 마스킹, 전사 보정 서비스 컨텍스트 메타데이터 기록이 구현되었습니다. ChangesLogging Instrumentation for Gemini & Requests
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/core/logging_middleware.py`:
- Around line 106-117: The handler currently decodes and logs any UTF-8 body
when content_type doesn't contain "application/json"; change the logic to treat
any content_type containing "+json" or "application/json" as JSON (so pass
through json.loads -> _sanitize_json -> _truncate via existing code paths) and
for all other content types avoid returning raw body_text — instead return a
safe placeholder like "(omitted body, content_type={content_type},
content_length={len(body_bytes)})" or a short summary/omitted string using
_truncate; update the branch that uses content_type and body_bytes and keep
using _sanitize_json and _truncate for JSON variants; add regression tests that
assert application/x-www-form-urlencoded and text/plain do not return raw
sensitive fields and that application/*+json is sanitized via _sanitize_json.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 7d856152-81d1-4167-a0df-18d11b4b95f2
📒 Files selected for processing (5)
app/core/gemini.pyapp/core/logging_middleware.pyapp/domains/transcribe/services/transcript_correction.pytests/test_gemini_retry.pytests/test_logging_middleware.py
Constraint: text/plain과 form-urlencoded 본문에도 비밀 값이 포함될 수 있음 Rejected: UTF-8 본문 원문 로깅 | JSON 외 요청의 password/token 값이 로그에 남을 수 있음 Confidence: high Scope-risk: narrow Directive: JSON 계열은 +json까지 sanitize하고 그 외 body는 길이 요약만 남길 것 Tested: uv run pytest; uv run ruff check Not-tested: 실제 외부 프록시가 변형한 content-type 조합
✨ 작업 개요
Gemini 호출 비용/실패 원인을 운영 로그에서 추적할 수 있도록 usage metadata와 요청 body 진단 로그를 보강했습니다.
📄 작업 내용
📌 관련 이슈
🔌 API 변경사항 (해당 시)
💬 기타 사항
uv run pytest110 passed,uv run ruff check통과Summary by CodeRabbit
릴리스 노트
새 기능
보안 강화
테스트