From a278d21741652434d9b05086405a7fd420a72cc6 Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Wed, 8 Jul 2026 17:24:31 +0900 Subject: [PATCH] test: more caplog (#38452) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../telemetry/test_enterprise_trace.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/api/tests/unit_tests/enterprise/telemetry/test_enterprise_trace.py b/api/tests/unit_tests/enterprise/telemetry/test_enterprise_trace.py index 24c905c75c2..29ddf73e24d 100644 --- a/api/tests/unit_tests/enterprise/telemetry/test_enterprise_trace.py +++ b/api/tests/unit_tests/enterprise/telemetry/test_enterprise_trace.py @@ -3,8 +3,9 @@ from __future__ import annotations import json +import logging from datetime import UTC, datetime -from typing import Any +from typing import Any, cast from unittest.mock import MagicMock, patch import pytest @@ -475,13 +476,24 @@ class TestWorkflowTrace: assert span_call[1]["start_time"] == _T0 assert span_call[1]["end_time"] == _T1 - def test_emits_companion_log_with_event_name(self, trace_handler: EnterpriseOtelTrace, mock_exporter): - with patch("enterprise.telemetry.enterprise_trace.emit_telemetry_log") as mock_log: + def test_emits_companion_log_with_event_name( + self, + trace_handler: EnterpriseOtelTrace, + caplog: pytest.LogCaptureFixture, + ): + with caplog.at_level(logging.INFO, logger="dify.telemetry"): trace_handler._workflow_trace(make_workflow_info()) - mock_log.assert_called_once() - assert mock_log.call_args[1]["event_name"] == EnterpriseTelemetryEvent.WORKFLOW_RUN - assert mock_log.call_args[1]["tenant_id"] == "tenant-abc" + records = [record for record in caplog.records if record.name == "dify.telemetry"] + + assert len(records) == 1 + record = records[0] + + attrs = cast(dict[str, Any], record.__dict__["attributes"]) + + assert attrs["dify.event.name"] == EnterpriseTelemetryEvent.WORKFLOW_RUN + assert attrs["dify.event.signal"] == "span_detail" + assert record.__dict__["tenant_id"] == "tenant-abc" def test_companion_log_includes_content_when_enabled(self, trace_handler: EnterpriseOtelTrace, mock_exporter): mock_exporter.include_content = True