[PR #19297] [Observability] Update counter to include http method and target #29123

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

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

State: closed
Merged: Yes


Summary

This pull request enhances OpenTelemetry instrumentation in the api/extensions/ext_otel.py file, focusing on improving HTTP response metrics and adding support for additional attributes such as HTTP method and target. It also includes minor refactoring and new imports to support these changes.

Improvements to HTTP response metrics:

  • Updated the _http_response_counter description to include HTTP method and target, making the metrics more detailed and informative. ([api/extensions/ext_otel.pyL40-R46](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6L40-R46))
  • Enhanced the response_hook function to conditionally add SpanAttributes.HTTP_TARGET and SpanAttributes.HTTP_METHOD to the metric attributes, using data from the flask.request object. ([api/extensions/ext_otel.pyL53-R64](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6L53-R64))

Supporting changes:

  • Added the flask import to enable access to the flask.request object. ([api/extensions/ext_otel.pyR9](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R9))
  • Imported SpanAttributes from opentelemetry.semconv.trace to use predefined attribute keys for OpenTelemetry spans. ([api/extensions/ext_otel.pyR31-R32](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R31-R32))

Screenshots

Before After
image image

Checklist

  • 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/19297 **State:** closed **Merged:** Yes --- # Summary This pull request enhances OpenTelemetry instrumentation in the `api/extensions/ext_otel.py` file, focusing on improving HTTP response metrics and adding support for additional attributes such as HTTP method and target. It also includes minor refactoring and new imports to support these changes. ### Improvements to HTTP response metrics: * Updated the `_http_response_counter` description to include HTTP method and target, making the metrics more detailed and informative. (`[api/extensions/ext_otel.pyL40-R46](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6L40-R46)`) * Enhanced the `response_hook` function to conditionally add `SpanAttributes.HTTP_TARGET` and `SpanAttributes.HTTP_METHOD` to the metric attributes, using data from the `flask.request` object. (`[api/extensions/ext_otel.pyL53-R64](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6L53-R64)`) ### Supporting changes: * Added the `flask` import to enable access to the `flask.request` object. (`[api/extensions/ext_otel.pyR9](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R9)`) * Imported `SpanAttributes` from `opentelemetry.semconv.trace` to use predefined attribute keys for OpenTelemetry spans. (`[api/extensions/ext_otel.pyR31-R32](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R31-R32)`) # Screenshots | Before | After | |--------|-------| | ![image](https://github.com/user-attachments/assets/ab3d7ee9-b4c0-4d8a-9e50-e0834fdee3fd) | ![image](https://github.com/user-attachments/assets/11ac4bb8-5925-41bf-a0d6-332a2902f289) | # Checklist - [ ] 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:44:54 -05:00
yindo closed this issue 2026-02-21 20:44:55 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29123