Skip to content

Commit c47ff15

Browse files
committed
set tracing if user enables it
1 parent 270e0b6 commit c47ff15

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/crewai/events/listeners/tracing/first_time_trace_handler.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
2+
import os
23
import uuid
34
import webbrowser
5+
from pathlib import Path
46

57
from rich.console import Console
68
from rich.panel import Panel
@@ -15,6 +17,47 @@
1517
logger = logging.getLogger(__name__)
1618

1719

20+
def _update_or_create_env_file():
21+
"""Update or create .env file with CREWAI_TRACING_ENABLED=true."""
22+
env_path = Path(".env")
23+
env_content = ""
24+
variable_name = "CREWAI_TRACING_ENABLED"
25+
variable_value = "true"
26+
27+
# Read existing content if file exists
28+
if env_path.exists():
29+
with open(env_path, "r") as f:
30+
env_content = f.read()
31+
32+
# Check if CREWAI_TRACING_ENABLED is already set
33+
lines = env_content.splitlines()
34+
variable_exists = False
35+
updated_lines = []
36+
37+
for line in lines:
38+
if line.strip().startswith(f"{variable_name}="):
39+
# Update existing variable
40+
updated_lines.append(f"{variable_name}={variable_value}")
41+
variable_exists = True
42+
else:
43+
updated_lines.append(line)
44+
45+
# Add variable if it doesn't exist
46+
if not variable_exists:
47+
if updated_lines and not updated_lines[-1].strip():
48+
# If last line is empty, replace it
49+
updated_lines[-1] = f"{variable_name}={variable_value}"
50+
else:
51+
# Add new line and then the variable
52+
updated_lines.append(f"{variable_name}={variable_value}")
53+
54+
# Write updated content
55+
with open(env_path, "w") as f:
56+
f.write("\n".join(updated_lines))
57+
if updated_lines: # Add final newline if there's content
58+
f.write("\n")
59+
60+
1861
class FirstTimeTraceHandler:
1962
"""Handles the first-time user trace collection and display flow."""
2063

@@ -49,6 +92,12 @@ def handle_execution_completion(self):
4992
if user_wants_traces:
5093
self._initialize_backend_and_send_events()
5194

95+
# Enable tracing for future runs by updating .env file
96+
try:
97+
_update_or_create_env_file()
98+
except Exception as e:
99+
pass
100+
52101
if self.ephemeral_url:
53102
self._display_ephemeral_trace_link()
54103

@@ -129,7 +178,8 @@ def _display_ephemeral_trace_link(self):
129178
• Tool usage and results
130179
• LLM calls and responses
131180
132-
To use traces add tracing=True to your Crew(tracing=True) / Flow(tracing=True)
181+
✅ Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env)
182+
You can also add tracing=True to your Crew(tracing=True) / Flow(tracing=True) for more control.
133183
134184
📝 Note: This link will expire in 24 hours.
135185
""".strip()
@@ -164,8 +214,8 @@ def _show_local_trace_message(self):
164214
• Execution duration: {self.batch_manager.calculate_duration("execution")}ms
165215
• Batch ID: {self.batch_manager.trace_batch_id}
166216
217+
Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env)
167218
The traces include agent decisions, task execution, and tool usage.
168-
Try running with CREWAI_TRACING_ENABLED=true next time for persistent traces.
169219
""".strip()
170220

171221
panel = Panel(

0 commit comments

Comments
 (0)