refactor(api): type celery sqlcommenter tags with CelerySqlcommenterTagsDict TypedDict (#34526)

This commit is contained in:
YBoy
2026-04-03 14:06:15 +02:00
committed by GitHub
parent e85d9a0d72
commit 0f051d5886

View File

@@ -11,7 +11,7 @@ SQLAlchemy instrumentor appends comments to SQL statements.
""" """
import logging import logging
from typing import Any from typing import Any, TypedDict
from celery.signals import task_postrun, task_prerun from celery.signals import task_postrun, task_prerun
from opentelemetry import context from opentelemetry import context
@@ -24,9 +24,17 @@ _SQLCOMMENTER_CONTEXT_KEY = "SQLCOMMENTER_ORM_TAGS_AND_VALUES"
_TOKEN_ATTR = "_dify_sqlcommenter_context_token" _TOKEN_ATTR = "_dify_sqlcommenter_context_token"
def _build_celery_sqlcommenter_tags(task: Any) -> dict[str, str | int]: class CelerySqlcommenterTagsDict(TypedDict, total=False):
framework: str
task_name: str
traceparent: str
celery_retries: int
routing_key: str
def _build_celery_sqlcommenter_tags(task: Any) -> CelerySqlcommenterTagsDict:
"""Build SQL commenter tags from the current Celery task and OpenTelemetry context.""" """Build SQL commenter tags from the current Celery task and OpenTelemetry context."""
tags: dict[str, str | int] = {} tags: CelerySqlcommenterTagsDict = {}
try: try:
tags["framework"] = f"celery:{_get_celery_version()}" tags["framework"] = f"celery:{_get_celery_version()}"