mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-01 07:13:00 +00:00
chore: add tracing message suppression via ContextVar
This commit is contained in:
@@ -2026,7 +2026,13 @@ class Crew(FlowTrackable, BaseModel):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _show_tracing_disabled_message() -> None:
|
def _show_tracing_disabled_message() -> None:
|
||||||
"""Show a message when tracing is disabled."""
|
"""Show a message when tracing is disabled."""
|
||||||
from crewai.events.listeners.tracing.utils import has_user_declined_tracing
|
from crewai.events.listeners.tracing.utils import (
|
||||||
|
has_user_declined_tracing,
|
||||||
|
should_suppress_tracing_messages,
|
||||||
|
)
|
||||||
|
|
||||||
|
if should_suppress_tracing_messages():
|
||||||
|
return
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
|
|||||||
@@ -797,7 +797,13 @@ class TraceCollectionListener(BaseEventListener):
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
|
|
||||||
from crewai.events.listeners.tracing.utils import has_user_declined_tracing
|
from crewai.events.listeners.tracing.utils import (
|
||||||
|
has_user_declined_tracing,
|
||||||
|
should_suppress_tracing_messages,
|
||||||
|
)
|
||||||
|
|
||||||
|
if should_suppress_tracing_messages():
|
||||||
|
return
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,31 @@ _first_time_trace_hook: ContextVar[Callable[[], bool] | None] = ContextVar(
|
|||||||
"_first_time_trace_hook", default=None
|
"_first_time_trace_hook", default=None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_suppress_tracing_messages: ContextVar[bool] = ContextVar(
|
||||||
|
"_suppress_tracing_messages", default=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def set_suppress_tracing_messages(suppress: bool) -> object:
|
||||||
|
"""Set whether to suppress tracing-related console messages.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
suppress: True to suppress messages, False to show them.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A token that can be used to restore the previous value.
|
||||||
|
"""
|
||||||
|
return _suppress_tracing_messages.set(suppress)
|
||||||
|
|
||||||
|
|
||||||
|
def should_suppress_tracing_messages() -> bool:
|
||||||
|
"""Check if tracing messages should be suppressed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if messages should be suppressed, False otherwise.
|
||||||
|
"""
|
||||||
|
return _suppress_tracing_messages.get()
|
||||||
|
|
||||||
|
|
||||||
def should_enable_tracing(*, override: bool | None = None) -> bool:
|
def should_enable_tracing(*, override: bool | None = None) -> bool:
|
||||||
"""Determine if tracing should be enabled.
|
"""Determine if tracing should be enabled.
|
||||||
@@ -440,6 +465,9 @@ def prompt_user_for_trace_viewing(timeout_seconds: int = 20) -> bool:
|
|||||||
if _is_test_environment():
|
if _is_test_environment():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if should_suppress_tracing_messages():
|
||||||
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,31 @@ _disable_version_check: ContextVar[bool] = ContextVar(
|
|||||||
"_disable_version_check", default=False
|
"_disable_version_check", default=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_suppress_console_output: ContextVar[bool] = ContextVar(
|
||||||
|
"_suppress_console_output", default=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def set_suppress_console_output(suppress: bool) -> object:
|
||||||
|
"""Set whether to suppress all console output.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
suppress: True to suppress output, False to show it.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A token that can be used to restore the previous value.
|
||||||
|
"""
|
||||||
|
return _suppress_console_output.set(suppress)
|
||||||
|
|
||||||
|
|
||||||
|
def should_suppress_console_output() -> bool:
|
||||||
|
"""Check if console output should be suppressed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if output should be suppressed, False otherwise.
|
||||||
|
"""
|
||||||
|
return _suppress_console_output.get()
|
||||||
|
|
||||||
|
|
||||||
class ConsoleFormatter:
|
class ConsoleFormatter:
|
||||||
tool_usage_counts: ClassVar[dict[str, int]] = {}
|
tool_usage_counts: ClassVar[dict[str, int]] = {}
|
||||||
@@ -88,8 +113,12 @@ To update, run: uv sync --upgrade-package crewai"""
|
|||||||
from crewai.events.listeners.tracing.utils import (
|
from crewai.events.listeners.tracing.utils import (
|
||||||
has_user_declined_tracing,
|
has_user_declined_tracing,
|
||||||
is_tracing_enabled_in_context,
|
is_tracing_enabled_in_context,
|
||||||
|
should_suppress_tracing_messages,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if should_suppress_tracing_messages():
|
||||||
|
return
|
||||||
|
|
||||||
if not is_tracing_enabled_in_context():
|
if not is_tracing_enabled_in_context():
|
||||||
if has_user_declined_tracing():
|
if has_user_declined_tracing():
|
||||||
message = """Info: Tracing is disabled.
|
message = """Info: Tracing is disabled.
|
||||||
@@ -141,6 +170,8 @@ To enable tracing, do any one of these:
|
|||||||
|
|
||||||
def print(self, *args: Any, **kwargs: Any) -> None:
|
def print(self, *args: Any, **kwargs: Any) -> None:
|
||||||
"""Print to console. Simplified to only handle panel-based output."""
|
"""Print to console. Simplified to only handle panel-based output."""
|
||||||
|
if should_suppress_console_output():
|
||||||
|
return
|
||||||
# Skip blank lines during streaming
|
# Skip blank lines during streaming
|
||||||
if len(args) == 0 and self._is_streaming:
|
if len(args) == 0 and self._is_streaming:
|
||||||
return
|
return
|
||||||
@@ -497,6 +528,9 @@ To enable tracing, do any one of these:
|
|||||||
if not self.verbose:
|
if not self.verbose:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if should_suppress_console_output():
|
||||||
|
return
|
||||||
|
|
||||||
self._is_streaming = True
|
self._is_streaming = True
|
||||||
self._last_stream_call_type = call_type
|
self._last_stream_call_type = call_type
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ from crewai.events.listeners.tracing.utils import (
|
|||||||
has_user_declined_tracing,
|
has_user_declined_tracing,
|
||||||
set_tracing_enabled,
|
set_tracing_enabled,
|
||||||
should_enable_tracing,
|
should_enable_tracing,
|
||||||
|
should_suppress_tracing_messages,
|
||||||
)
|
)
|
||||||
from crewai.events.types.flow_events import (
|
from crewai.events.types.flow_events import (
|
||||||
FlowCreatedEvent,
|
FlowCreatedEvent,
|
||||||
@@ -2628,6 +2629,8 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _show_tracing_disabled_message() -> None:
|
def _show_tracing_disabled_message() -> None:
|
||||||
"""Show a message when tracing is disabled."""
|
"""Show a message when tracing is disabled."""
|
||||||
|
if should_suppress_tracing_messages():
|
||||||
|
return
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import TYPE_CHECKING, Final, Literal, NamedTuple
|
from typing import TYPE_CHECKING, Final, Literal, NamedTuple
|
||||||
|
|
||||||
|
from crewai.events.utils.console_formatter import should_suppress_console_output
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from _typeshed import SupportsWrite
|
from _typeshed import SupportsWrite
|
||||||
@@ -77,6 +79,8 @@ class Printer:
|
|||||||
file: A file-like object (stream); defaults to the current sys.stdout.
|
file: A file-like object (stream); defaults to the current sys.stdout.
|
||||||
flush: Whether to forcibly flush the stream.
|
flush: Whether to forcibly flush the stream.
|
||||||
"""
|
"""
|
||||||
|
if should_suppress_console_output():
|
||||||
|
return
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
content = [ColoredText(content, color)]
|
content = [ColoredText(content, color)]
|
||||||
print(
|
print(
|
||||||
|
|||||||
Reference in New Issue
Block a user