[PR #18029] [Observability] Instrument with celery #28823

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

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

State: closed
Merged: Yes


Summary

This pull request introduces OpenTelemetry instrumentation for Celery workers and refactors the initialization code for Flask and SQLAlchemy instrumentors. The changes aim to enhance observability and ensure proper instrumentation across different components of the application.

Instrumentation and initialization improvements:

  • api/extensions/ext_otel.py: Added imports for logging, sys, and CeleryInstrumentor, and introduced new functions is_celery_worker, init_flask_instrumentor, and init_sqlalchemy_instrumentor to modularize and streamline the initialization process. The init_app function now conditionally initializes the Flask and Celery instrumentors based on whether the process is a Celery worker. [1] [2] [3] [4] [5]

  • api/pyproject.toml: Added the opentelemetry-instrumentation-celery dependency to support Celery instrumentation.

Screenshots

Example span:

2025-04-13 14:37:07 2025-04-13T06:37:07.949Z    info    Traces  {"resource spans": 1, "spans": 1}
2025-04-13 14:37:07 2025-04-13T06:37:07.949Z    info    ResourceSpans #0
2025-04-13 14:37:07 Resource SchemaURL: 
2025-04-13 14:37:07 Resource attributes:
2025-04-13 14:37:07      -> service.name: Str(langgenius/dify)
2025-04-13 14:37:07      -> service.version: Str(dify-1.2.0-)
2025-04-13 14:37:07      -> process.pid: Int(44347)
2025-04-13 14:37:07      -> deployment.environment: Str(PRODUCTION-SELF_HOSTED)
2025-04-13 14:37:07      -> host.name: Str(192.168.1.5)
2025-04-13 14:37:07      -> host.arch: Str(x86_64)
2025-04-13 14:37:07      -> custom.deployment.git_commit: Str()
2025-04-13 14:37:07      -> host.id: Str(192.168.1.5)
2025-04-13 14:37:07      -> os.type: Str(darwin)
2025-04-13 14:37:07      -> os.description: Str(macOS-14.6-x86_64-i386-64bit)
2025-04-13 14:37:07      -> os.version: Str(Darwin Kernel Version 23.6.0: Fri Jul  5 17:56:41 PDT 2024; root:xnu-10063.141.1~2/RELEASE_ARM64_T6000)
2025-04-13 14:37:07 ScopeSpans #0
2025-04-13 14:37:07 ScopeSpans SchemaURL: 
2025-04-13 14:37:07 InstrumentationScope opentelemetry.instrumentation.celery 0.48b0
2025-04-13 14:37:07 Span #0
2025-04-13 14:37:07     Trace ID       : a9c66456625277519828a515778eab7d
2025-04-13 14:37:07     Parent ID      : 6c4bd4aa3c7785d8
2025-04-13 14:37:07     ID             : 52cda13bdf364979
2025-04-13 14:37:07     Name           : run/tasks.document_indexing_task.document_indexing_task
2025-04-13 14:37:07     Kind           : Consumer
2025-04-13 14:37:07     Start time     : 2025-04-13 06:37:01.90138 +0000 UTC
2025-04-13 14:37:07     End time       : 2025-04-13 06:37:04.532533 +0000 UTC
2025-04-13 14:37:07     Status code    : Unset
2025-04-13 14:37:07     Status message : 
2025-04-13 14:37:07 Attributes:
2025-04-13 14:37:07      -> celery.action: Str(run)
2025-04-13 14:37:07      -> celery.state: Str(SUCCESS)
2025-04-13 14:37:07      -> messaging.conversation_id: Str(544db3c1-0886-410c-80d7-ead70c61cb81)
2025-04-13 14:37:07      -> messaging.destination: Str(dataset)
2025-04-13 14:37:07      -> celery.delivery_info: Str({'exchange': '', 'routing_key': 'dataset', 'priority': 0, 'redelivered': False})
2025-04-13 14:37:07      -> messaging.message.id: Str(544db3c1-0886-410c-80d7-ead70c61cb81)
2025-04-13 14:37:07      -> celery.reply_to: Str(4fc09415-7f27-36cc-8e2d-af4f2483dbe0)
2025-04-13 14:37:07      -> celery.hostname: Str(gen43885@192.168.1.5)
2025-04-13 14:37:07      -> celery.task_name: Str(tasks.document_indexing_task.document_indexing_task)
2025-04-13 14:37:02 2025-04-13T06:37:02.071Z    info    Traces  {"resource spans": 1, "spans": 1}
2025-04-13 14:37:02 2025-04-13T06:37:02.071Z    info    ResourceSpans #0
2025-04-13 14:37:02 Resource SchemaURL: 
2025-04-13 14:37:02 Resource attributes:
2025-04-13 14:37:02      -> service.name: Str(langgenius/dify)
2025-04-13 14:37:02      -> service.version: Str(dify-1.2.0-)
2025-04-13 14:37:02      -> process.pid: Int(43885)
2025-04-13 14:37:02      -> deployment.environment: Str(PRODUCTION-SELF_HOSTED)
2025-04-13 14:37:02      -> host.name: Str(192.168.1.5)
2025-04-13 14:37:02      -> host.arch: Str(x86_64)
2025-04-13 14:37:02      -> custom.deployment.git_commit: Str()
2025-04-13 14:37:02      -> host.id: Str(192.168.1.5)
2025-04-13 14:37:02      -> os.type: Str(darwin)
2025-04-13 14:37:02      -> os.description: Str(macOS-14.6-x86_64-i386-64bit)
2025-04-13 14:37:02      -> os.version: Str(Darwin Kernel Version 23.6.0: Fri Jul  5 17:56:41 PDT 2024; root:xnu-10063.141.1~2/RELEASE_ARM64_T6000)
2025-04-13 14:37:02 ScopeSpans #0
2025-04-13 14:37:02 ScopeSpans SchemaURL: 
2025-04-13 14:37:02 InstrumentationScope opentelemetry.instrumentation.celery 0.48b0
2025-04-13 14:37:02 Span #0
2025-04-13 14:37:02     Trace ID       : a9c66456625277519828a515778eab7d
2025-04-13 14:37:02     Parent ID      : 
2025-04-13 14:37:02     ID             : 6c4bd4aa3c7785d8
2025-04-13 14:37:02     Name           : apply_async/tasks.document_indexing_task.document_indexing_task
2025-04-13 14:37:02     Kind           : Producer
2025-04-13 14:37:02     Start time     : 2025-04-13 06:37:01.881846 +0000 UTC
2025-04-13 14:37:02     End time       : 2025-04-13 06:37:01.898741 +0000 UTC
2025-04-13 14:37:02     Status code    : Unset
2025-04-13 14:37:02     Status message : 
2025-04-13 14:37:02 Attributes:
2025-04-13 14:37:02      -> celery.action: Str(apply_async)
2025-04-13 14:37:02      -> messaging.message.id: Str(544db3c1-0886-410c-80d7-ead70c61cb81)
2025-04-13 14:37:02      -> celery.task_name: Str(tasks.document_indexing_task.document_indexing_task)
2025-04-13 14:37:02      -> messaging.destination_kind: Str(queue)
2025-04-13 14:37:02      -> messaging.destination: Str(dataset)
2025-04-13 14:37:02 

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/18029 **State:** closed **Merged:** Yes --- # Summary This pull request introduces OpenTelemetry instrumentation for Celery workers and refactors the initialization code for Flask and SQLAlchemy instrumentors. The changes aim to enhance observability and ensure proper instrumentation across different components of the application. Instrumentation and initialization improvements: * [`api/extensions/ext_otel.py`](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R2-R17): Added imports for `logging`, `sys`, and `CeleryInstrumentor`, and introduced new functions `is_celery_worker`, `init_flask_instrumentor`, and `init_sqlalchemy_instrumentor` to modularize and streamline the initialization process. The `init_app` function now conditionally initializes the Flask and Celery instrumentors based on whether the process is a Celery worker. [[1]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R2-R17) [[2]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6L27-R31) [[3]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R103-R113) [[4]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R122-L113) [[5]](diffhunk://#diff-d9b99e6e386de2f18f686daf1e6fcb87ca02c20900826cbdc5892a5e81f362b6R143-R149) * [`api/pyproject.toml`](diffhunk://#diff-40f8f985c548e2b57b803d17f42833f8a4d94603ca5b5e0bf48de51872809400R59): Added the `opentelemetry-instrumentation-celery` dependency to support Celery instrumentation. # Screenshots Example span: ``` 2025-04-13 14:37:07 2025-04-13T06:37:07.949Z info Traces {"resource spans": 1, "spans": 1} 2025-04-13 14:37:07 2025-04-13T06:37:07.949Z info ResourceSpans #0 2025-04-13 14:37:07 Resource SchemaURL: 2025-04-13 14:37:07 Resource attributes: 2025-04-13 14:37:07 -> service.name: Str(langgenius/dify) 2025-04-13 14:37:07 -> service.version: Str(dify-1.2.0-) 2025-04-13 14:37:07 -> process.pid: Int(44347) 2025-04-13 14:37:07 -> deployment.environment: Str(PRODUCTION-SELF_HOSTED) 2025-04-13 14:37:07 -> host.name: Str(192.168.1.5) 2025-04-13 14:37:07 -> host.arch: Str(x86_64) 2025-04-13 14:37:07 -> custom.deployment.git_commit: Str() 2025-04-13 14:37:07 -> host.id: Str(192.168.1.5) 2025-04-13 14:37:07 -> os.type: Str(darwin) 2025-04-13 14:37:07 -> os.description: Str(macOS-14.6-x86_64-i386-64bit) 2025-04-13 14:37:07 -> os.version: Str(Darwin Kernel Version 23.6.0: Fri Jul 5 17:56:41 PDT 2024; root:xnu-10063.141.1~2/RELEASE_ARM64_T6000) 2025-04-13 14:37:07 ScopeSpans #0 2025-04-13 14:37:07 ScopeSpans SchemaURL: 2025-04-13 14:37:07 InstrumentationScope opentelemetry.instrumentation.celery 0.48b0 2025-04-13 14:37:07 Span #0 2025-04-13 14:37:07 Trace ID : a9c66456625277519828a515778eab7d 2025-04-13 14:37:07 Parent ID : 6c4bd4aa3c7785d8 2025-04-13 14:37:07 ID : 52cda13bdf364979 2025-04-13 14:37:07 Name : run/tasks.document_indexing_task.document_indexing_task 2025-04-13 14:37:07 Kind : Consumer 2025-04-13 14:37:07 Start time : 2025-04-13 06:37:01.90138 +0000 UTC 2025-04-13 14:37:07 End time : 2025-04-13 06:37:04.532533 +0000 UTC 2025-04-13 14:37:07 Status code : Unset 2025-04-13 14:37:07 Status message : 2025-04-13 14:37:07 Attributes: 2025-04-13 14:37:07 -> celery.action: Str(run) 2025-04-13 14:37:07 -> celery.state: Str(SUCCESS) 2025-04-13 14:37:07 -> messaging.conversation_id: Str(544db3c1-0886-410c-80d7-ead70c61cb81) 2025-04-13 14:37:07 -> messaging.destination: Str(dataset) 2025-04-13 14:37:07 -> celery.delivery_info: Str({'exchange': '', 'routing_key': 'dataset', 'priority': 0, 'redelivered': False}) 2025-04-13 14:37:07 -> messaging.message.id: Str(544db3c1-0886-410c-80d7-ead70c61cb81) 2025-04-13 14:37:07 -> celery.reply_to: Str(4fc09415-7f27-36cc-8e2d-af4f2483dbe0) 2025-04-13 14:37:07 -> celery.hostname: Str(gen43885@192.168.1.5) 2025-04-13 14:37:07 -> celery.task_name: Str(tasks.document_indexing_task.document_indexing_task) ``` ``` 2025-04-13 14:37:02 2025-04-13T06:37:02.071Z info Traces {"resource spans": 1, "spans": 1} 2025-04-13 14:37:02 2025-04-13T06:37:02.071Z info ResourceSpans #0 2025-04-13 14:37:02 Resource SchemaURL: 2025-04-13 14:37:02 Resource attributes: 2025-04-13 14:37:02 -> service.name: Str(langgenius/dify) 2025-04-13 14:37:02 -> service.version: Str(dify-1.2.0-) 2025-04-13 14:37:02 -> process.pid: Int(43885) 2025-04-13 14:37:02 -> deployment.environment: Str(PRODUCTION-SELF_HOSTED) 2025-04-13 14:37:02 -> host.name: Str(192.168.1.5) 2025-04-13 14:37:02 -> host.arch: Str(x86_64) 2025-04-13 14:37:02 -> custom.deployment.git_commit: Str() 2025-04-13 14:37:02 -> host.id: Str(192.168.1.5) 2025-04-13 14:37:02 -> os.type: Str(darwin) 2025-04-13 14:37:02 -> os.description: Str(macOS-14.6-x86_64-i386-64bit) 2025-04-13 14:37:02 -> os.version: Str(Darwin Kernel Version 23.6.0: Fri Jul 5 17:56:41 PDT 2024; root:xnu-10063.141.1~2/RELEASE_ARM64_T6000) 2025-04-13 14:37:02 ScopeSpans #0 2025-04-13 14:37:02 ScopeSpans SchemaURL: 2025-04-13 14:37:02 InstrumentationScope opentelemetry.instrumentation.celery 0.48b0 2025-04-13 14:37:02 Span #0 2025-04-13 14:37:02 Trace ID : a9c66456625277519828a515778eab7d 2025-04-13 14:37:02 Parent ID : 2025-04-13 14:37:02 ID : 6c4bd4aa3c7785d8 2025-04-13 14:37:02 Name : apply_async/tasks.document_indexing_task.document_indexing_task 2025-04-13 14:37:02 Kind : Producer 2025-04-13 14:37:02 Start time : 2025-04-13 06:37:01.881846 +0000 UTC 2025-04-13 14:37:02 End time : 2025-04-13 06:37:01.898741 +0000 UTC 2025-04-13 14:37:02 Status code : Unset 2025-04-13 14:37:02 Status message : 2025-04-13 14:37:02 Attributes: 2025-04-13 14:37:02 -> celery.action: Str(apply_async) 2025-04-13 14:37:02 -> messaging.message.id: Str(544db3c1-0886-410c-80d7-ead70c61cb81) 2025-04-13 14:37:02 -> celery.task_name: Str(tasks.document_indexing_task.document_indexing_task) 2025-04-13 14:37:02 -> messaging.destination_kind: Str(queue) 2025-04-13 14:37:02 -> messaging.destination: Str(dataset) 2025-04-13 14:37:02 ``` # 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:44:11 -05:00
yindo closed this issue 2026-02-21 20:44:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#28823