Avoiding exceptions

This commit is contained in:
João Moura
2024-10-18 08:32:06 -03:00
parent 6fa2b89831
commit 53a9f107ca

View File

@@ -65,7 +65,7 @@ class Telemetry:
self.provider.add_span_processor(processor) self.provider.add_span_processor(processor)
self.ready = True self.ready = True
except BaseException as e: except Exception as e:
if isinstance( if isinstance(
e, e,
(SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError), (SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError),
@@ -83,10 +83,18 @@ class Telemetry:
self.ready = False self.ready = False
self.trace_set = False self.trace_set = False
def _safe_telemetry_operation(self, operation):
if not self.ready:
return
try:
operation()
except Exception:
pass
def crew_creation(self, crew: Crew, inputs: dict[str, Any] | None): def crew_creation(self, crew: Crew, inputs: dict[str, Any] | None):
"""Records the creation of a crew.""" """Records the creation of a crew."""
if self.ready:
try: def operation():
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Crew Created") span = tracer.start_span("Crew Created")
self._add_attribute( self._add_attribute(
@@ -127,8 +135,7 @@ class Telemetry:
"allow_code_execution?": agent.allow_code_execution, "allow_code_execution?": agent.allow_code_execution,
"max_retry_limit": agent.max_retry_limit, "max_retry_limit": agent.max_retry_limit,
"tools_names": [ "tools_names": [
tool.name.casefold() tool.name.casefold() for tool in agent.tools or []
for tool in agent.tools or []
], ],
} }
for agent in crew.agents for agent in crew.agents
@@ -157,8 +164,7 @@ class Telemetry:
else None else None
), ),
"tools_names": [ "tools_names": [
tool.name.casefold() tool.name.casefold() for tool in task.tools or []
for tool in task.tools or []
], ],
} }
for task in crew.tasks for task in crew.tasks
@@ -196,8 +202,7 @@ class Telemetry:
"allow_code_execution?": agent.allow_code_execution, "allow_code_execution?": agent.allow_code_execution,
"max_retry_limit": agent.max_retry_limit, "max_retry_limit": agent.max_retry_limit,
"tools_names": [ "tools_names": [
tool.name.casefold() tool.name.casefold() for tool in agent.tools or []
for tool in agent.tools or []
], ],
} }
for agent in crew.agents for agent in crew.agents
@@ -219,8 +224,7 @@ class Telemetry:
), ),
"agent_key": task.agent.key if task.agent else None, "agent_key": task.agent.key if task.agent else None,
"tools_names": [ "tools_names": [
tool.name.casefold() tool.name.casefold() for tool in task.tools or []
for tool in task.tools or []
], ],
} }
for task in crew.tasks for task in crew.tasks
@@ -229,13 +233,13 @@ class Telemetry:
) )
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def task_started(self, crew: Crew, task: Task) -> Span | None: def task_started(self, crew: Crew, task: Task) -> Span | None:
"""Records task started in a crew.""" """Records task started in a crew."""
if self.ready:
try: def operation():
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
created_span = tracer.start_span("Task Created") created_span = tracer.start_span("Task Created")
@@ -270,15 +274,13 @@ class Telemetry:
) )
return span return span
except Exception:
pass
return None return self._safe_telemetry_operation(operation)
def task_ended(self, span: Span, task: Task, crew: Crew): def task_ended(self, span: Span, task: Task, crew: Crew):
"""Records task execution in a crew.""" """Records task execution in a crew."""
if self.ready:
try: def operation():
if crew.share_crew: if crew.share_crew:
self._add_attribute( self._add_attribute(
span, span,
@@ -288,13 +290,13 @@ class Telemetry:
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def tool_repeated_usage(self, llm: Any, tool_name: str, attempts: int): def tool_repeated_usage(self, llm: Any, tool_name: str, attempts: int):
"""Records the repeated usage 'error' of a tool by an agent.""" """Records the repeated usage 'error' of a tool by an agent."""
if self.ready:
try: def operation():
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Tool Repeated Usage") span = tracer.start_span("Tool Repeated Usage")
self._add_attribute( self._add_attribute(
@@ -308,13 +310,13 @@ class Telemetry:
self._add_attribute(span, "llm", llm.model) self._add_attribute(span, "llm", llm.model)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def tool_usage(self, llm: Any, tool_name: str, attempts: int): def tool_usage(self, llm: Any, tool_name: str, attempts: int):
"""Records the usage of a tool by an agent.""" """Records the usage of a tool by an agent."""
if self.ready:
try: def operation():
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Tool Usage") span = tracer.start_span("Tool Usage")
self._add_attribute( self._add_attribute(
@@ -328,13 +330,13 @@ class Telemetry:
self._add_attribute(span, "llm", llm.model) self._add_attribute(span, "llm", llm.model)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def tool_usage_error(self, llm: Any): def tool_usage_error(self, llm: Any):
"""Records the usage of a tool by an agent.""" """Records the usage of a tool by an agent."""
if self.ready:
try: def operation():
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Tool Usage Error") span = tracer.start_span("Tool Usage Error")
self._add_attribute( self._add_attribute(
@@ -346,14 +348,13 @@ class Telemetry:
self._add_attribute(span, "llm", llm.model) self._add_attribute(span, "llm", llm.model)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def individual_test_result_span( def individual_test_result_span(
self, crew: Crew, quality: float, exec_time: int, model_name: str self, crew: Crew, quality: float, exec_time: int, model_name: str
): ):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Crew Individual Test Result") span = tracer.start_span("Crew Individual Test Result")
@@ -369,8 +370,8 @@ class Telemetry:
self._add_attribute(span, "model_name", model_name) self._add_attribute(span, "model_name", model_name)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def test_execution_span( def test_execution_span(
self, self,
@@ -379,8 +380,7 @@ class Telemetry:
inputs: dict[str, Any] | None, inputs: dict[str, Any] | None,
model_name: str, model_name: str,
): ):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Crew Test Execution") span = tracer.start_span("Crew Test Execution")
@@ -401,44 +401,40 @@ class Telemetry:
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def deploy_signup_error_span(self): def deploy_signup_error_span(self):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Deploy Signup Error") span = tracer.start_span("Deploy Signup Error")
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def start_deployment_span(self, uuid: Optional[str] = None): def start_deployment_span(self, uuid: Optional[str] = None):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Start Deployment") span = tracer.start_span("Start Deployment")
if uuid: if uuid:
self._add_attribute(span, "uuid", uuid) self._add_attribute(span, "uuid", uuid)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def create_crew_deployment_span(self): def create_crew_deployment_span(self):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Create Crew Deployment") span = tracer.start_span("Create Crew Deployment")
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def get_crew_logs_span(self, uuid: Optional[str], log_type: str = "deployment"): def get_crew_logs_span(self, uuid: Optional[str], log_type: str = "deployment"):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Get Crew Logs") span = tracer.start_span("Get Crew Logs")
self._add_attribute(span, "log_type", log_type) self._add_attribute(span, "log_type", log_type)
@@ -446,20 +442,19 @@ class Telemetry:
self._add_attribute(span, "uuid", uuid) self._add_attribute(span, "uuid", uuid)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def remove_crew_span(self, uuid: Optional[str] = None): def remove_crew_span(self, uuid: Optional[str] = None):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Remove Crew") span = tracer.start_span("Remove Crew")
if uuid: if uuid:
self._add_attribute(span, "uuid", uuid) self._add_attribute(span, "uuid", uuid)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def crew_execution_span(self, crew: Crew, inputs: dict[str, Any] | None): def crew_execution_span(self, crew: Crew, inputs: dict[str, Any] | None):
"""Records the complete execution of a crew. """Records the complete execution of a crew.
@@ -467,8 +462,7 @@ class Telemetry:
""" """
self.crew_creation(crew, inputs) self.crew_creation(crew, inputs)
if (self.ready) and (crew.share_crew): def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Crew Execution") span = tracer.start_span("Crew Execution")
self._add_attribute( self._add_attribute(
@@ -533,12 +527,13 @@ class Telemetry:
), ),
) )
return span return span
except Exception:
pass if crew.share_crew:
return self._safe_telemetry_operation(operation)
return None
def end_crew(self, crew, final_string_output): def end_crew(self, crew, final_string_output):
if (self.ready) and (crew.share_crew): def operation():
try:
self._add_attribute( self._add_attribute(
crew._execution_span, crew._execution_span,
"crewai_version", "crewai_version",
@@ -563,47 +558,46 @@ class Telemetry:
) )
crew._execution_span.set_status(Status(StatusCode.OK)) crew._execution_span.set_status(Status(StatusCode.OK))
crew._execution_span.end() crew._execution_span.end()
except Exception:
pass if crew.share_crew:
self._safe_telemetry_operation(operation)
def _add_attribute(self, span, key, value): def _add_attribute(self, span, key, value):
"""Add an attribute to a span.""" """Add an attribute to a span."""
try:
def operation():
return span.set_attribute(key, value) return span.set_attribute(key, value)
except Exception:
pass self._safe_telemetry_operation(operation)
def flow_creation_span(self, flow_name: str): def flow_creation_span(self, flow_name: str):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Flow Creation") span = tracer.start_span("Flow Creation")
self._add_attribute(span, "flow_name", flow_name) self._add_attribute(span, "flow_name", flow_name)
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def flow_plotting_span(self, flow_name: str, node_names: list[str]): def flow_plotting_span(self, flow_name: str, node_names: list[str]):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Flow Plotting") span = tracer.start_span("Flow Plotting")
self._add_attribute(span, "flow_name", flow_name) self._add_attribute(span, "flow_name", flow_name)
self._add_attribute(span, "node_names", json.dumps(node_names)) self._add_attribute(span, "node_names", json.dumps(node_names))
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)
def flow_execution_span(self, flow_name: str, node_names: list[str]): def flow_execution_span(self, flow_name: str, node_names: list[str]):
if self.ready: def operation():
try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Flow Execution") span = tracer.start_span("Flow Execution")
self._add_attribute(span, "flow_name", flow_name) self._add_attribute(span, "flow_name", flow_name)
self._add_attribute(span, "node_names", json.dumps(node_names)) self._add_attribute(span, "node_names", json.dumps(node_names))
span.set_status(Status(StatusCode.OK)) span.set_status(Status(StatusCode.OK))
span.end() span.end()
except Exception:
pass self._safe_telemetry_operation(operation)