Fix import sorting issues for linter

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-13 21:27:09 +00:00
parent c709e3365a
commit 083eb3987d
2 changed files with 23 additions and 11 deletions

View File

@@ -5,8 +5,8 @@ This patch addresses issue #2366 where OpenTelemetry logs only store
input.value field for agent calls but no output.value.
"""
import importlib
import sys
import logging
import sys
from typing import Any, Optional
# Setup logging
@@ -30,9 +30,9 @@ def patch_crewai_instrumentor():
try:
# Try to import OpenInference
from openinference.instrumentation.crewai import CrewAIInstrumentor
from wrapt import wrap_function_wrapper
from opentelemetry import trace as trace_api
from opentelemetry import context as context_api
from opentelemetry import trace as trace_api
from wrapt import wrap_function_wrapper
# Define the wrapper class
class _AgentExecuteTaskWrapper:
@@ -55,7 +55,9 @@ def patch_crewai_instrumentor():
# Get attributes module if available
try:
from openinference.instrumentation import get_attributes_from_context
from openinference.instrumentation import (
get_attributes_from_context,
)
from openinference.semconv.trace import OpenInferenceSpanKindValues
has_attributes = True
except ImportError:
@@ -104,7 +106,9 @@ def patch_crewai_instrumentor():
# Add additional attributes if available
if has_attributes:
from openinference.instrumentation import get_attributes_from_context
from openinference.instrumentation import (
get_attributes_from_context,
)
span.set_attributes(dict(get_attributes_from_context()))
return response

View File

@@ -3,17 +3,21 @@ Test for the OpenInference Agent wrapper patch.
This test verifies that our patch is properly applied.
"""
import pytest
import sys
import importlib
from unittest.mock import patch, MagicMock, call
import sys
from unittest.mock import MagicMock, call, patch
import pytest
from crewai import Agent, Task
from crewai.utilities.events import AgentExecutionCompletedEvent
def test_patch_function_exists():
"""Test that the patch function exists and is callable."""
from crewai.telemetry.patches.openinference_agent_wrapper import patch_crewai_instrumentor
from crewai.telemetry.patches.openinference_agent_wrapper import (
patch_crewai_instrumentor,
)
# Verify the patch function exists
assert callable(patch_crewai_instrumentor)
@@ -22,7 +26,9 @@ def test_patch_function_exists():
def test_patch_handles_missing_openinference():
"""Test that the patch function handles missing OpenInference gracefully."""
# Import the patch module
from crewai.telemetry.patches.openinference_agent_wrapper import patch_crewai_instrumentor
from crewai.telemetry.patches.openinference_agent_wrapper import (
patch_crewai_instrumentor,
)
# Mock sys.modules to simulate OpenInference not being installed
original_modules = sys.modules.copy()
@@ -57,7 +63,9 @@ def test_agent_execute_task_emits_event():
# 4. Verify that the span has both input.value and output.value attributes
# For now, we'll just verify that our patch exists and is callable
from crewai.telemetry.patches.openinference_agent_wrapper import patch_crewai_instrumentor
from crewai.telemetry.patches.openinference_agent_wrapper import (
patch_crewai_instrumentor,
)
assert callable(patch_crewai_instrumentor)
# And that the patch handles missing OpenInference gracefully