[PR #20319] [Observability] Add type check and try-except in otel #29360

Closed
opened 2026-02-21 20:45:24 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/20319

State: closed
Merged: Yes


Summary

This pull request enhances error handling and logging in the api/extensions/ext_otel.py file to improve robustness and maintainability. The key changes include adding exception handling around telemetry operations, refining the emit method in the custom logging handler, and ensuring attributes are set conditionally to avoid potential errors.

Error handling improvements:

  • Added try-except blocks in on_user_loaded to handle exceptions when setting tenant and user attributes on the current span. Logs exceptions using logging.exception for better debugging. [1] [2]
  • Added try-except blocks in response_hook to handle errors when setting span status and attributes. Logs exceptions using logging.exception. [1] [2]

Logging enhancements:

  • Updated the emit method in the custom ExceptionLoggingHandler to use type annotations (record: logging.LogRecord) for clarity.
  • Improved the emit method to conditionally check and set attributes like exception.type and exception.message only when relevant parts of exc_info are available, preventing potential NoneType errors.

Follow-up #20303

Screenshots

Before After
... ...

Checklist

Important

Please review the checklist below before submitting your pull request.

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/20319 **State:** closed **Merged:** Yes --- # Summary This pull request enhances error handling and logging in the `api/extensions/ext_otel.py` file to improve robustness and maintainability. The key changes include adding exception handling around telemetry operations, refining the `emit` method in the custom logging handler, and ensuring attributes are set conditionally to avoid potential errors. ### Error handling improvements: * Added `try-except` blocks in `on_user_loaded` to handle exceptions when setting tenant and user attributes on the current span. Logs exceptions using `logging.exception` for better debugging. [[1]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R25) [[2]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R36-R38) * Added `try-except` blocks in `response_hook` to handle errors when setting span status and attributes. Logs exceptions using `logging.exception`. [[1]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R61) [[2]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R77-R79) ### Logging enhancements: * Updated the `emit` method in the custom `ExceptionLoggingHandler` to use type annotations (`record: logging.LogRecord`) for clarity. * Improved the `emit` method to conditionally check and set attributes like `exception.type` and `exception.message` only when relevant parts of `exc_info` are available, preventing potential `NoneType` errors. Follow-up #20303 # Screenshots | Before | After | |--------|-------| | ... | ... | # Checklist > [!IMPORTANT] > Please review the checklist below before submitting your pull request. - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:45:24 -05:00
yindo closed this issue 2026-02-21 20:45:24 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29360