Emit task created (#875)

* Emit task created

* Limit data to shared crews
This commit is contained in:
Gui Vieira
2024-07-07 12:58:24 -03:00
committed by GitHub
parent 3dd6249f1e
commit b75b0b5552
2 changed files with 35 additions and 19 deletions

View File

@@ -13,8 +13,7 @@ from pydantic_core import PydanticCustomError
from crewai.agents.agent_builder.base_agent import BaseAgent from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.tasks.task_output import TaskOutput from crewai.tasks.task_output import TaskOutput
from crewai.telemetry.telemetry import Telemetry from crewai.telemetry.telemetry import Telemetry
from crewai.utilities.converter import ConverterError from crewai.utilities.converter import Converter, ConverterError
from crewai.utilities.converter import Converter
from crewai.utilities.i18n import I18N from crewai.utilities.i18n import I18N
from crewai.utilities.printer import Printer from crewai.utilities.printer import Printer
from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser
@@ -186,8 +185,6 @@ class Task(BaseModel):
Output of the task. Output of the task.
""" """
self._execution_span = self._telemetry.task_started(self)
agent = agent or self.agent agent = agent or self.agent
if not agent: if not agent:
raise Exception( raise Exception(
@@ -195,6 +192,8 @@ class Task(BaseModel):
"and should be executed in a Crew using a specific process that support that, like hierarchical." "and should be executed in a Crew using a specific process that support that, like hierarchical."
) )
self._execution_span = self._telemetry.task_started(crew=agent.crew, task=self)
if self.context: if self.context:
internal_context = [] internal_context = []
for task in self.context: for task in self.context:
@@ -310,15 +309,15 @@ class Task(BaseModel):
return copied_task return copied_task
def _create_converter(self, *args, **kwargs) -> Converter: # type: ignore def _create_converter(self, *args, **kwargs) -> Converter: # type: ignore
converter = self.agent.get_output_converter( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter" converter = self.agent.get_output_converter( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter"
*args, **kwargs *args, **kwargs
) )
if self.converter_cls: if self.converter_cls:
converter = self.converter_cls( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter" converter = self.converter_cls( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter"
*args, **kwargs *args, **kwargs
) )
return converter return converter
def _export_output(self, result: str) -> Any: def _export_output(self, result: str) -> Any:
exported_result = result exported_result = result
@@ -350,7 +349,7 @@ class Task(BaseModel):
model_schema = PydanticSchemaParser(model=model).get_schema() # type: ignore # Argument "model" to "PydanticSchemaParser" has incompatible type "type[BaseModel] | None"; expected "type[BaseModel]" model_schema = PydanticSchemaParser(model=model).get_schema() # type: ignore # Argument "model" to "PydanticSchemaParser" has incompatible type "type[BaseModel] | None"; expected "type[BaseModel]"
instructions = f"{instructions}\n\nThe json should have the following structure, with the following keys:\n{model_schema}" instructions = f"{instructions}\n\nThe json should have the following structure, with the following keys:\n{model_schema}"
converter = self._create_converter( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter" converter = self._create_converter( # type: ignore # Item "None" of "BaseAgent | None" has no attribute "get_output_converter"
llm=llm, text=result, model=model, instructions=instructions llm=llm, text=result, model=model, instructions=instructions
) )

View File

@@ -156,18 +156,35 @@ class Telemetry:
except Exception: except Exception:
pass pass
def task_started(self, 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: if self.ready:
try: try:
tracer = trace.get_tracer("crewai.telemetry") tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Task Execution") span = tracer.start_span("Task Execution")
created_span = tracer.start_span("Task Created")
self._add_attribute(created_span, "task_id", str(task.id))
if crew.share_crew:
self._add_attribute(
created_span, "formatted_description", task.description
)
self._add_attribute(
created_span, "formatted_expected_output", task.expected_output
)
created_span.set_status(Status(StatusCode.OK))
created_span.end()
self._add_attribute(span, "task_id", str(task.id)) self._add_attribute(span, "task_id", str(task.id))
self._add_attribute(span, "formatted_description", task.description)
self._add_attribute( if crew.share_crew:
span, "formatted_expected_output", task.expected_output self._add_attribute(span, "formatted_description", task.description)
) self._add_attribute(
span, "formatted_expected_output", task.expected_output
)
return span return span
except Exception: except Exception: