QA plan: observability logs and EMF metrics (#416)
Story: #416
Capability: Cost guardrails and observability (#31)
ADRs: RAG020 (observability); RAG009 (log scrub)
Security requirements: per-feature requirements, Feature 49; Feature 31/32/33/34 (log scrub); Feature 19 to 25 (log retention)
Test scope
Section titled “Test scope”| Type | Coverage |
|---|---|
| Unit | The API and authorizer handlers log through Powertools Logger. No print() call exists in any Lambda handler or module it imports (ruff rule T201). |
| Unit | No Lambda module imports the X-Ray SDK or Powertools Tracer. |
| Unit | Every record written during one request carries that request’s ID from requestContext.requestId. The check covers records written from helper modules, not only from the handler body. |
| Unit | Every record is a single JSON object with level and message keys. |
| Unit | A POST /query with a sentinel question and a stub LLM logs the question text and the key label. The answer text and the citation text appear in no record. |
| Unit | A request carrying a fake bearer token and key produces no record that contains the Authorization header under any casing, the token string, the key secret, or the key hash. |
| Unit | Bedrock call latency, Data API call latency, guardrail-check (ApplyGuardrail) latency when the guardrail check runs, and token-count metrics are emitted as EMF records. Every dimension in each record has an allowed value: service is the function’s service name, and Route is in the fixed route list. No other dimension appears. |
| Unit | No EMF dimension value is a request ID or a key label. |
| Unit | EMF metrics are flushed when the handler raises an exception. |
| IaC (plan review) | Every log group sets retention_in_days = 30. |
| IaC (plan review) | The API Gateway stage access log format includes $context.requestId. |
| IaC (plan review) | No Lambda function sets tracing_config mode to Active. No Lambda execution role grants an xray: action. |
| Integration (manual, QA) | After one POST /query, a Logs Insights query filtered on the request ID from the API access log returns app records from the API Lambda. Every returned record carries that ID. Lambda START, END, and REPORT lines are excluded, as they carry Lambda’s own request ID. |
| Integration (manual, QA) | A POST /query with an unknown bearer token is denied by the authorizer. A Logs Insights query on the request ID from the API access log returns at least one app record from the authorizer Lambda, and every returned record carries that ID. |
| Integration (manual, QA) | A POST /query without an Authorization header returns 401 and has an API access log entry. API Gateway rejects it before invoking the authorizer, so the authorizer writes no records for that request ID. |
| Integration (manual, QA) | After one POST /query, CloudWatch Metrics lists the Bedrock latency, Data API latency, guardrail-check latency (when the guardrail check runs), and token-count metrics with data points under the environment’s namespace. Their only dimensions are service and Route. |
| Integration (manual, QA) | The sentinel question and the QA key label appear in the logs. The answer text from the response body does not. |
Entry criteria
Section titled “Entry criteria”- #23 (Lambda and API Gateway) and #60 (authorizer) deployed to QA
- Story #34 (CloudWatch logging) complete, with Powertools Logger in use
Exit criteria
Section titled “Exit criteria”- Unit tests for every Unit row in Test scope pass.
- Terraform plan for each environment shows the access log format, log retention, and tracing settings listed in Test scope.
- Integration checks for every Integration row in Test scope pass against QA.
- Each requirement in Security requirements below is verified by the test named in its row.
Environments
Section titled “Environments”Unit and IaC plan review: CI. Integration: manual against QA.
Tooling
Section titled “Tooling”pytest, Lambda Powertools Logger and Metrics (version pinned in pyproject.toml), ruff, tfsec, Terraform plan output, AWS CLI (aws logs start-query, aws logs get-query-results, aws cloudwatch list-metrics), CloudWatch Logs Insights console.
How tests are written
Section titled “How tests are written”SERVICE_NAME and ROUTES are the values the application defines for the Metrics service name and the route list.
def test_request_id_on_every_record(lambda_log_capture): # Given an API event with a known API Gateway request ID event = build_apigw_event(question="What is RAG?", request_id="req-test-123") # When the handler processes the event, including the helper-module path that logs handler(event, {}) # Then every record written during the request carries the request ID records = lambda_log_capture.records assert records, "No records captured" assert all(r.get("correlation_id") == "req-test-123" for r in records)
def test_emf_dimensions_are_bounded(emf_capture): # Given a /query call with a known request ID and key label handler(build_apigw_event(question="What is RAG?", request_id="req-test-456", key_label="qa-key"), {}) # When the EMF records are read assert emf_capture.records, "No EMF records captured" allowed = {"service": {SERVICE_NAME}, "Route": set(ROUTES)} for record in emf_capture.records: dims = { name for group in record["_aws"]["CloudWatchMetrics"] for dimset in group["Dimensions"] for name in dimset } # Then every dimension is allowed and takes an allowed value, and no request ID or key label appears assert dims <= allowed.keys() assert all(record[name] in allowed[name] for name in dims) assert "req-test-456" not in json.dumps(record) assert "qa-key" not in json.dumps(record)Integration check, run in the Logs Insights console or with aws logs start-query, after log ingestion completes:
fields @timestamp, @message| filter correlation_id = "<requestId from the API access log entry>"| sort @timestamp asc- Records logged at module scope (cold start) run before the request ID is bound. Keep module-scope log calls out of Lambda code. The integration query returns any app record that lacks the ID.
- Each unique EMF dimension combination is a billed custom metric. A dimension added later, or a request ID used as a dimension, multiplies the cost. The dimension test fails on any name outside the allowed set.
- EMF metrics flush at handler exit. An exception path can skip the flush unless the decorator flushes on error. The flush test covers this path.
- Logs Insights ingestion lags by several minutes. Run the integration query after the lag, not immediately after the request.
- Question text is logged and retained for 30 days. The retention check is the only control on that data in this plan.
Security requirements
Section titled “Security requirements”| Requirement | Verified by | Source |
|---|---|---|
| The API Gateway request ID appears in the access log and on every Powertools Logger record written during the request, including code outside the handler. Lambda platform lines are excluded. | Unit request-ID test; integration Logs Insights check | Feature 49; RAG020 |
The Authorization header is absent from every log entry under any casing. | Unit scrub test | Feature 31/32/33/34; RAG009 |
| Logged content is question text and key label only. Answer and citation text are not logged. | Unit content test; integration sentinel check | Decisions log D-8 |
| Every EMF dimension takes values from a fixed set: the constant service name and a fixed list of routes. Request IDs and key labels are never dimensions. | Unit dimension test | RAG020 |
| Every log group has explicit, finite 30-day retention. | IaC plan review | Feature 19 to 25; Decisions log D-8 |
Capability acceptance criteria link
Section titled “Capability acceptance criteria link”RAG020: Powertools Logger records carry the API Gateway request ID, EMF metrics record per-hop latency and token counts under bounded dimensions, with no tracing. D-8: logs hold question text and key label only. Security requirements (Features 49, 31/32/33/34, 19 to 25): request-ID correlation, Authorization scrub, and 30-day retention. Exit criteria cover each.
© 2026 Benjamin Arunski