From 349d2d8e4e8827b5fb2ef5229afbffde457b36a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E4=BA=86=E7=9D=A1=E5=A4=A7=E8=A7=89?= <64798754+stakeswky@users.noreply.github.com> Date: Fri, 27 Feb 2026 07:53:45 +0800 Subject: [PATCH] fix: replace deprecated SpanAttributes and ResourceAttributes with new semconv imports (#32661) Co-authored-by: User Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- api/extensions/ext_otel.py | 41 +++++++++++++++++++------- api/extensions/otel/instrumentation.py | 9 ++++-- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/api/extensions/ext_otel.py b/api/extensions/ext_otel.py index 40a915e68c..a5baa21018 100644 --- a/api/extensions/ext_otel.py +++ b/api/extensions/ext_otel.py @@ -26,7 +26,26 @@ def init_app(app: DifyApp): ConsoleSpanExporter, ) from opentelemetry.sdk.trace.sampling import ParentBasedTraceIdRatio - from opentelemetry.semconv.resource import ResourceAttributes + from opentelemetry.semconv._incubating.attributes.deployment_attributes import ( # type: ignore[import-untyped] + DEPLOYMENT_ENVIRONMENT_NAME, + ) + from opentelemetry.semconv._incubating.attributes.host_attributes import ( # type: ignore[import-untyped] + HOST_ARCH, + HOST_ID, + HOST_NAME, + ) + from opentelemetry.semconv._incubating.attributes.os_attributes import ( # type: ignore[import-untyped] + OS_DESCRIPTION, + OS_TYPE, + OS_VERSION, + ) + from opentelemetry.semconv._incubating.attributes.process_attributes import ( # type: ignore[import-untyped] + PROCESS_PID, + ) + from opentelemetry.semconv.attributes.service_attributes import ( # type: ignore[import-untyped] + SERVICE_NAME, + SERVICE_VERSION, + ) from opentelemetry.trace import set_tracer_provider from extensions.otel.instrumentation import init_instruments @@ -37,17 +56,17 @@ def init_app(app: DifyApp): # Follow Semantic Convertions 1.32.0 to define resource attributes resource = Resource( attributes={ - ResourceAttributes.SERVICE_NAME: dify_config.APPLICATION_NAME, - ResourceAttributes.SERVICE_VERSION: f"dify-{dify_config.project.version}-{dify_config.COMMIT_SHA}", - ResourceAttributes.PROCESS_PID: os.getpid(), - ResourceAttributes.DEPLOYMENT_ENVIRONMENT: f"{dify_config.DEPLOY_ENV}-{dify_config.EDITION}", - ResourceAttributes.HOST_NAME: socket.gethostname(), - ResourceAttributes.HOST_ARCH: platform.machine(), + SERVICE_NAME: dify_config.APPLICATION_NAME, + SERVICE_VERSION: f"dify-{dify_config.project.version}-{dify_config.COMMIT_SHA}", + PROCESS_PID: os.getpid(), + DEPLOYMENT_ENVIRONMENT_NAME: f"{dify_config.DEPLOY_ENV}-{dify_config.EDITION}", + HOST_NAME: socket.gethostname(), + HOST_ARCH: platform.machine(), "custom.deployment.git_commit": dify_config.COMMIT_SHA, - ResourceAttributes.HOST_ID: platform.node(), - ResourceAttributes.OS_TYPE: platform.system().lower(), - ResourceAttributes.OS_DESCRIPTION: platform.platform(), - ResourceAttributes.OS_VERSION: platform.version(), + HOST_ID: platform.node(), + OS_TYPE: platform.system().lower(), + OS_DESCRIPTION: platform.platform(), + OS_VERSION: platform.version(), } ) sampler = ParentBasedTraceIdRatio(dify_config.OTEL_SAMPLING_RATE) diff --git a/api/extensions/otel/instrumentation.py b/api/extensions/otel/instrumentation.py index 6617f69513..b73ba8df8c 100644 --- a/api/extensions/otel/instrumentation.py +++ b/api/extensions/otel/instrumentation.py @@ -7,7 +7,10 @@ from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor from opentelemetry.instrumentation.redis import RedisInstrumentor from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.metrics import get_meter, get_meter_provider -from opentelemetry.semconv.trace import SpanAttributes +from opentelemetry.semconv.attributes.http_attributes import ( # type: ignore[import-untyped] + HTTP_REQUEST_METHOD, + HTTP_ROUTE, +) from opentelemetry.trace import Span, get_tracer_provider from opentelemetry.trace.status import StatusCode @@ -85,9 +88,9 @@ def init_flask_instrumentor(app: DifyApp) -> None: attributes: dict[str, str | int] = {"status_code": status_code, "status_class": status_class} request = flask.request if request and request.url_rule: - attributes[SpanAttributes.HTTP_TARGET] = str(request.url_rule.rule) + attributes[HTTP_ROUTE] = str(request.url_rule.rule) if request and request.method: - attributes[SpanAttributes.HTTP_METHOD] = str(request.method) + attributes[HTTP_REQUEST_METHOD] = str(request.method) _http_response_counter.add(1, attributes) except Exception: logger.exception("Error setting status and attributes")