[PR #22623] feat(trace): support external trace id propagation #29994

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

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

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes [#<issue 22114>](https://github.com/langgenius/dify/issues/22114).

Summary

feat(trace): support external trace id propagation for chatflow and workflow

  • Support external trace id propagation for both chatflow and workflow types.
    • Chatflow: Accept and propagate external trace id via /v1/chat-messages endpoint.
    • Workflow: Accept and propagate external trace id via /v1/workflows/run endpoint.
  • Trace id is extracted and validated in the following priority order:
    1. HTTP headers (highest priority)
    2. Query parameters
    3. JSON body (lowest priority)
      The first valid trace id found in this order will be used throughout the request lifecycle.
  • If no valid external trace id is provided, Dify will generate a new one as before.
  • Adds/updates tests to cover trace id extraction and validation logic.

Motivation

  • Dify now empowers enterprise-grade observability by supporting the reception and propagation of external trace_ids (via HTTP header or request parameter) throughout its internal workflow. When an external trace_id is not provided, Dify will gracefully generate a new one to ensure trace continuity.
  • This enhancement enables seamless integration with industry-standard distributed tracing systems (such as OpenTelemetry, Jaeger, Zipkin, etc.), unlocking true end-to-end technical tracing across complex, multi-system architectures.
  • By associating the external trace_id with unique business system identifiers, Dify further enables comprehensive full-link tracing from a business perspective, allowing for precise correlation of requests and logs across the entire business chain and significantly improving both troubleshooting and business-level analytics.

Fixes #22098

Screenshots

Before After
Before 1 Before 2
After 1 After 2
image image
image image

External trace_id with no backend tracing configured

The API accepts and propagates an external trace_id even when no distributed tracing backend is configured. This ensures compatibility and graceful fallback.

No Tracing Backend Configured Request with trace_id & Normal Response
no-tracing-backend request-with-traceid

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/22623 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 2. Ensure there is an associated issue and you have been assigned to it > 3. Use the correct syntax to link this PR: `Fixes [#<issue 22114>](https://github.com/langgenius/dify/issues/22114)`. ## Summary > feat(trace): support external trace id propagation for chatflow and workflow - **Support external trace id propagation for both chatflow and workflow types.** - **Chatflow:** Accept and propagate external trace id via `/v1/chat-messages` endpoint. - **Workflow:** Accept and propagate external trace id via `/v1/workflows/run` endpoint. - Trace id is extracted and validated in the following priority order: 1. HTTP headers (highest priority) 2. Query parameters 3. JSON body (lowest priority) The first valid trace id found in this order will be used throughout the request lifecycle. - If no valid external trace id is provided, Dify will generate a new one as before. - Adds/updates tests to cover trace id extraction and validation logic. ### Motivation - Dify now empowers enterprise-grade observability by supporting the reception and propagation of external `trace_id`s (via HTTP header or request parameter) throughout its internal workflow. When an external `trace_id` is not provided, Dify will gracefully generate a new one to ensure trace continuity. - This enhancement enables seamless integration with industry-standard distributed tracing systems (such as OpenTelemetry, Jaeger, Zipkin, etc.), unlocking true end-to-end technical tracing across complex, multi-system architectures. - By associating the external `trace_id` with unique business system identifiers, Dify further enables comprehensive full-link tracing from a business perspective, allowing for precise correlation of requests and logs across the entire business chain and significantly improving both troubleshooting and business-level analytics. Fixes [#22098](https://github.com/langgenius/dify/issues/22114) ## Screenshots | Before | After | |--------|-------| | <img width="1360" height="508" alt="Before 1" src="https://github.com/user-attachments/assets/56494bbe-9605-4e30-9622-e707525f02ca" /> | <img width="1356" height="785" alt="Before 2" src="https://github.com/user-attachments/assets/075ae857-b01a-438b-9e56-103fbf8354dd" /> | |<img width="1910" height="243" alt="After 1" src="https://github.com/user-attachments/assets/e8326c9a-20f5-45eb-93d3-bc1996f6be38" /> |<img width="1918" height="324" alt="After 2" src="https://github.com/user-attachments/assets/abae3c28-4918-437f-99c3-2fc29a574e4e" /> | |<img width="1885" height="833" alt="image" src="https://github.com/user-attachments/assets/856180e3-7d05-4561-af89-e61ba5af2873" />|<img width="1920" height="951" alt="image" src="https://github.com/user-attachments/assets/cfe80ee8-91fa-4c3d-a218-636ebe7b5052" />| |<img width="1920" height="919" alt="image" src="https://github.com/user-attachments/assets/00461dfc-aff8-4ba4-ae28-c9673f398ade" />|<img width="1920" height="942" alt="image" src="https://github.com/user-attachments/assets/c47842e1-74e8-4f71-86c3-1ac7bd3b4d17" />| ### External trace_id with no backend tracing configured > The API accepts and propagates an external `trace_id` even when no distributed tracing backend is configured. This ensures compatibility and graceful fallback. | No Tracing Backend Configured | Request with trace_id & Normal Response | |------------------------------|-----------------------------------------| | ![no-tracing-backend](https://github.com/user-attachments/assets/3d29d51c-b174-4606-976d-b5565755c90b) | ![request-with-traceid](https://github.com/user-attachments/assets/969ad8c4-386d-4909-8f6b-a9ffe026f019) | ## 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:46:38 -05:00
yindo closed this issue 2026-02-21 20:46:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29994