Skip to content

Commit

Permalink
[DO NOT MERGE] Provide inputs on crew creation (#898)
Browse files Browse the repository at this point in the history
* Provide inputs on crew creation

* Better naming

* Add crew id and task index to tasks

* Fix type again
  • Loading branch information
gvieira authored Jul 15, 2024
1 parent 0993864 commit b93632a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/crewai/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def set_private_attrs(self) -> "Crew":
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
self._telemetry = Telemetry()
self._telemetry.set_tracer()
self._telemetry.crew_creation(self)
return self

@model_validator(mode="after")
Expand Down
25 changes: 20 additions & 5 deletions src/crewai/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def set_tracer(self):
self.ready = False
self.trace_set = False

def crew_creation(self, crew):
def crew_creation(self, crew: Crew, inputs: dict[str, Any] | None):
"""Records the creation of a crew."""
if self.ready:
try:
Expand All @@ -93,6 +93,12 @@ def crew_creation(self, crew):
)
self._add_attribute(span, "python_version", platform.python_version())
self._add_attribute(span, "crew_id", str(crew.id))

if crew.share_crew:
self._add_attribute(
span, "crew_inputs", json.dumps(inputs) if inputs else None
)

self._add_attribute(span, "crew_process", crew.process)
self._add_attribute(span, "crew_memory", crew.memory)
self._add_attribute(span, "crew_number_of_tasks", len(crew.tasks))
Expand All @@ -114,7 +120,7 @@ def crew_creation(self, crew):
"llm": json.dumps(self._safe_llm_attributes(agent.llm)),
"delegation_enabled?": agent.allow_delegation,
"tools_names": [
tool.name.casefold() for tool in agent.tools
tool.name.casefold() for tool in agent.tools or []
],
}
for agent in crew.agents
Expand All @@ -139,7 +145,7 @@ def crew_creation(self, crew):
else None
),
"tools_names": [
tool.name.casefold() for tool in task.tools
tool.name.casefold() for tool in task.tools or []
],
}
for task in crew.tasks
Expand All @@ -161,10 +167,11 @@ def task_started(self, crew: Crew, task: Task) -> Span | None:
if self.ready:
try:
tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Task Execution")

created_span = tracer.start_span("Task Created")

self._add_attribute(created_span, "crew_id", str(crew.id))
self._add_attribute(created_span, "task_index", crew.tasks.index(task))
self._add_attribute(created_span, "task_id", str(task.id))

if crew.share_crew:
Expand All @@ -178,6 +185,10 @@ def task_started(self, crew: Crew, task: Task) -> Span | None:
created_span.set_status(Status(StatusCode.OK))
created_span.end()

span = tracer.start_span("Task Execution")

self._add_attribute(span, "crew_id", str(crew.id))
self._add_attribute(span, "task_index", crew.tasks.index(task))
self._add_attribute(span, "task_id", str(task.id))

if crew.share_crew:
Expand Down Expand Up @@ -275,6 +286,8 @@ def crew_execution_span(self, crew: Crew, inputs: dict[str, Any] | None):
"""
if (self.ready) and (crew.share_crew):
try:
self.crew_creation(crew, inputs)

tracer = trace.get_tracer("crewai.telemetry")
span = tracer.start_span("Crew Execution")
self._add_attribute(
Expand All @@ -283,7 +296,9 @@ def crew_execution_span(self, crew: Crew, inputs: dict[str, Any] | None):
pkg_resources.get_distribution("crewai").version,
)
self._add_attribute(span, "crew_id", str(crew.id))
self._add_attribute(span, "inputs", json.dumps(inputs))
self._add_attribute(
span, "crew_inputs", json.dumps(inputs) if inputs else None
)
self._add_attribute(
span,
"crew_agents",
Expand Down

0 comments on commit b93632a

Please sign in to comment.