Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion agentops/instrumentation/crewai/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
from agentops.instrumentation.crewai.version import __version__
from agentops.semconv import SpanAttributes, AgentOpsSpanKindValues, Meters, ToolAttributes
from agentops.semconv import SpanAttributes, AgentOpsSpanKindValues, Meters, ToolAttributes, MessageAttributes

Check warning on line 15 in agentops/instrumentation/crewai/instrumentation.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/crewai/instrumentation.py#L15

Added line #L15 was not covered by tests
from .crewai_span_attributes import CrewAISpanAttributes, set_span_attribute

# Initialize logger
Expand Down Expand Up @@ -367,6 +367,30 @@

result = wrapped(*args, **kwargs)

# Set prompt attributes from args
if args and isinstance(args[0], list):
for i, message in enumerate(args[0]):
if isinstance(message, dict):
if 'role' in message:
span.set_attribute(MessageAttributes.PROMPT_ROLE.format(i=i), message['role'])
if 'content' in message:
span.set_attribute(MessageAttributes.PROMPT_CONTENT.format(i=i), message['content'])

Check warning on line 377 in agentops/instrumentation/crewai/instrumentation.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/crewai/instrumentation.py#L371-L377

Added lines #L371 - L377 were not covered by tests

# Set completion attributes from result
if result:
span.set_attribute(MessageAttributes.COMPLETION_CONTENT.format(i=0), str(result))
span.set_attribute(MessageAttributes.COMPLETION_ROLE.format(i=0), "assistant")

Check warning on line 382 in agentops/instrumentation/crewai/instrumentation.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/crewai/instrumentation.py#L380-L382

Added lines #L380 - L382 were not covered by tests

# Set token usage attributes from callbacks
if 'callbacks' in kwargs and kwargs['callbacks'] and hasattr(kwargs['callbacks'][0], 'token_cost_process'):
token_process = kwargs['callbacks'][0].token_cost_process
if hasattr(token_process, 'completion_tokens'):
span.set_attribute(SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, token_process.completion_tokens)
if hasattr(token_process, 'prompt_tokens'):
span.set_attribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, token_process.prompt_tokens)
if hasattr(token_process, 'total_tokens'):
span.set_attribute(SpanAttributes.LLM_USAGE_TOTAL_TOKENS, token_process.total_tokens)

Check warning on line 392 in agentops/instrumentation/crewai/instrumentation.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/crewai/instrumentation.py#L385-L392

Added lines #L385 - L392 were not covered by tests

if duration_histogram:
duration = time.time() - start_time
duration_histogram.record(
Expand Down
Loading