Add structured logging and CloudWatch EMF metrics#20
Open
nandanadileep wants to merge 1 commit into
Open
Conversation
logger.py (new):
- log_invocation() emits a structured JSON line per request with intent,
userId, latency_ms, and error for CloudWatch Logs Insights queries
- emit_metrics() emits a CloudWatch Embedded Metrics Format line that
CloudWatch extracts as custom metrics (Invocations, Errors, Latency)
under the AlexaLLM namespace with Intent as dimension — no boto3 API
call, no added latency
lambda_function.py:
- lambda_handler timed with time.time(); log_invocation and emit_metrics
called in a finally block so logs fire even on unhandled exceptions
- intent_name resolved before routing so all request types are captured
cloudwatch_dashboard.json (new):
- Four widgets: invocations by intent, average latency by intent,
error rate (%), P99 latency for AskClaudeIntent
- Import via: aws cloudwatch put-dashboard --dashboard-name AlexaLLM
--dashboard-body file://cloudwatch_dashboard.json
test_lambda.py:
- TestLogger: 10 cases covering log_invocation field correctness,
emit_metrics EMF structure, error flag, latency, and namespace
- 3 handler-level tests asserting log_invocation and emit_metrics are
called with the correct intent name
Closes #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #6
Summary
Intentas the dimension:Invocations,Errors,Latencycloudwatch_dashboard.jsonprovides a ready-to-import dashboard with 4 widgetsFiles changed
logger.pylog_invocation()+emit_metrics()using EMFlambda_function.pyfinallyblockcloudwatch_dashboard.jsontest_lambda.pyTestLogger(13 cases) covering JSON structure, EMF format, error flag, and handler integrationOne-time setup
Import the dashboard:
Logs Insights query to see all invocations:
Test plan
invocationJSON log appears in CloudWatch LogsAlexaLLMnamespace appears in CloudWatch Metrics within ~5 minErrorsmetric increments