Skip to content

Conversation

@siwachabhi
Copy link
Contributor

@siwachabhi siwachabhi commented Sep 11, 2025

Summary

  • Replace text-based logging with AWS Lambda compatible JSON format
  • Add structured fields: timestamp, level, message, logger, requestId, sessionId
  • Enhanced exception logging with errorType, errorMessage, stackTrace, location
  • Update exception logging patterns to use logger.exception() consistently
  • Update tests to validate JSON structure

Test plan

  • All 400 unit tests pass
  • Pre-commit checks pass
  • JSON format matches AWS Lambda specification
  • Exception logging captures proper stack traces
  • Request and session IDs included in logs when available

🤖 Generated with Claude Code

2025-09-11T05:24:34.141000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:34.141Z", "level": "INFO", "message": "response was: Hello! \n\nNine hellos now - we're in impressive territory! At this point I'm wondering if we're going for some kind of record, or if \"hello\" has started to sound like a weird word from repetition (you know that thing where you say a word too many times and it stops feeling like a real word?).\n\nI'm genuinely curious about your strategy here, but I'm also happy to just be your consistent hello buddy for as long as you need. \n\nHello number nine, coming right back at you! 👋\n", "logger": "bedrock_agentcore.app", "requestId": "b6d04912-c263-44c4-b7df-df68dfcabed0", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:24:34.142000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:34.142Z", "level": "INFO", "message": "Invocation completed successfully (3.415s)", "logger": "bedrock_agentcore.app", "requestId": "b6d04912-c263-44c4-b7df-df68dfcabed0", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:24:43.670000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:43.670Z", "level": "INFO", "message": "response was: Hello! \n\nDouble digits! We've reached hello number ten! 🎉\n\nAt this point I'm genuinely impressed by your commitment to the hello game. I'm starting to wonder if we're creating some kind of minimalist poetry here, or if you're perhaps the most polite person ever who just really wants to make sure we've properly greeted each other.\n\nEither way, I admire the dedication. Hello back to you, my persistent conversational partner! \n\nAre we going for twenty? Fifty? I'm oddly invested in seeing where this goes now.\n", "logger": "bedrock_agentcore.app", "requestId": "d5f58c59-e0e0-4313-8e42-142835887d0c", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:24:43.670000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:43.670Z", "level": "INFO", "message": "Invocation completed successfully (5.389s)", "logger": "bedrock_agentcore.app", "requestId": "d5f58c59-e0e0-4313-8e42-142835887d0c", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:24:51.815000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:51.815Z", "level": "INFO", "message": "response was: Hello! \n\nEleven hellos! We're really committed to this now. \n\nYou know what? I think we might be onto something here. There's something almost zen about this exchange - like a very polite, very simple form of meditation. Or maybe we're proving that sometimes the best conversations are the ones that don't try too hard.\n\nHello number eleven, with genuine warmth and curiosity about where this delightfully unusual interaction is headed! \n\nI have to say, you've got impressive dedication to the bit. 😄\n", "logger": "bedrock_agentcore.app", "requestId": "d2503056-60ef-4b56-b51c-a7833afd42d9", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:24:51.816000+00:00 2025/09/11/[runtime-logs]a3dd357c-f99d-40f7-ba0f-ff789ad33e76 {"timestamp": "2025-09-11T05:24:51.815Z", "level": "INFO", "message": "Invocation completed successfully (6.016s)", "logger": "bedrock_agentcore.app", "requestId": "d2503056-60ef-4b56-b51c-a7833afd42d9", "sessionId": "e5c9630c-16c5-49e9-9e78-b550ac177aa1"}
2025-09-11T05:30:55.477000+00:00 2025/09/11/[runtime-logs]02cf593e-e700-4809-8c08-77b85cb3b05a {"timestamp": "2025-09-11T05:30:55.476Z", "level": "INFO", "message": "response was: Hello! How can I help you today?\n", "logger": "bedrock_agentcore.app", "requestId": "2b1a24ba-0c39-4fe5-9bf2-9c15e88123b2", "sessionId": "7e15d50a-fe04-40c5-be7b-f5ae8b171647"}
2025-09-11T05:30:55.478000+00:00 2025/09/11/[runtime-logs]02cf593e-e700-4809-8c08-77b85cb3b05a {"timestamp": "2025-09-11T05:30:55.477Z", "level": "ERROR", "message": "Handler 'agent_invocation' execution failed", "logger": "bedrock_agentcore.app", "requestId": "2b1a24ba-0c39-4fe5-9bf2-9c15e88123b2", "sessionId": "7e15d50a-fe04-40c5-be7b-f5ae8b171647", "errorType": "RuntimeError", "errorMessage": "Agent processing failed: testing", "stackTrace": ["Traceback (most recent call last):\n", "  File \"/usr/local/lib/python3.10/site-packages/bedrock_agentcore/runtime/app.py\", line 381, in _invoke_handler\n    return await loop.run_in_executor(None, ctx.run, handler, *args)\n", "  File \"/usr/local/lib/python3.10/concurrent/futures/thread.py\", line 58, in run\n    result = self.fn(*self.args, **self.kwargs)\n", "  File \"/usr/local/lib/python3.10/site-packages/opentelemetry/instrumentation/threading/__init__.py\", line 171, in wrapped_func\n    return original_func(*func_args, **func_kwargs)\n", "  File \"/app/agent_example.py\", line 16, in agent_invocation\n    raise RuntimeError(f\"Agent processing failed: testing\")\n", "RuntimeError: Agent processing failed: testing\n"], "location": "/usr/local/lib/python3.10/site-packages/bedrock_agentcore/runtime/app.py:_invoke_handler:384"}
2025-09-11T05:30:55.478000+00:00 2025/09/11/[runtime-logs]02cf593e-e700-4809-8c08-77b85cb3b05a {"timestamp": "2025-09-11T05:30:55.478Z", "level": "ERROR", "message": "Invocation failed (1.317s)", "logger": "bedrock_agentcore.app", "requestId": "2b1a24ba-0c39-4fe5-9bf2-9c15e88123b2", "sessionId": "7e15d50a-fe04-40c5-be7b-f5ae8b171647", "errorType": "RuntimeError", "errorMessage": "Agent processing failed: testing", "stackTrace": ["Traceback (most recent call last):\n", "  File \"/usr/local/lib/python3.10/site-packages/bedrock_agentcore/runtime/app.py\", line 321, in _handle_invocation\n    result = await self._invoke_handler(handler, request_context, takes_context, payload)\n", "  File \"/usr/local/lib/python3.10/site-packages/bedrock_agentcore/runtime/app.py\", line 381, in _invoke_handler\n    return await loop.run_in_executor(None, ctx.run, handler, *args)\n", "  File \"/usr/local/lib/python3.10/concurrent/futures/thread.py\", line 58, in run\n    result = self.fn(*self.args, **self.kwargs)\n", "  File \"/usr/local/lib/python3.10/site-packages/opentelemetry/instrumentation/threading/__init__.py\", line 171, in wrapped_func\n    return original_func(*func_args, **func_kwargs)\n", "  File \"/app/agent_example.py\", line 16, in agent_invocation\n    raise RuntimeError(f\"Agent processing failed: testing\")\n", "RuntimeError: Agent processing failed: testing\n"], "location": "/usr/local/lib/python3.10/site-packages/bedrock_agentcore/runtime/app.py:_handle_invocation:342"}

Replace RequestContextFormatter with AWS Lambda JSON format:
- Add structured JSON output with timestamp, level, message, logger fields
- Include requestId and sessionId when available
- Add exception handling with errorType, errorMessage, stackTrace, location
- Update exception logging to use logger.exception() for proper capture
- Update tests to validate JSON format structure

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Change _invoke_handler exception logging from ERROR to DEBUG level to prevent
duplicate logs when exceptions bubble up to _handle_invocation. The outer handler
provides better context with timing information.

Before: Two identical ERROR logs for same exception
After: Single ERROR log with duration metrics

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add debug=True to BedrockAgentCoreApp in test_simple_agent.py to enable
verbose logging that will help identify the root cause of the 5-minute
timeout in CI integration tests.

This provides better visibility into server startup issues.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@sundargthb sundargthb merged commit 114a670 into main Sep 12, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants