Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions newrelic/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"response.headers.contentType",
"response.status",
"server.address",
"subcomponent",
"zeebe.client.bpmnProcessId",
"zeebe.client.messageName",
"zeebe.client.correlationKey",
Expand Down
11 changes: 8 additions & 3 deletions newrelic/hooks/mlmodel_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import json
import logging
import sys
import uuid
Expand Down Expand Up @@ -73,8 +73,11 @@ def wrap_on_messages_stream(wrapped, instance, args, kwargs):
func_name = callable_name(wrapped)
function_trace_name = f"{func_name}/{agent_name}"

agentic_subcomponent_data = {"type": "APM-AI_AGENT", "name": agent_name}

ft = FunctionTrace(name=function_trace_name, group="Llm/agent/Autogen")
ft.__enter__()
ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data))

try:
return_val = wrapped(*args, **kwargs)
Expand Down Expand Up @@ -180,12 +183,14 @@ async def wrap__execute_tool_call(wrapped, instance, args, kwargs):
bound_args = bind_args(wrapped, args, kwargs)
tool_call_data = bound_args.get("tool_call")
tool_event_dict = _construct_base_tool_event_dict(bound_args, tool_call_data, tool_id, transaction, settings)

tool_name = getattr(tool_call_data, "name", "tool")

func_name = callable_name(wrapped)

agentic_subcomponent_data = {"type": "APM-AI_TOOL", "name": tool_name}

ft = FunctionTrace(name=f"{func_name}/{tool_name}", group="Llm/tool/Autogen")
ft.__enter__()
ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data))

try:
return_val = await wrapped(*args, **kwargs)
Expand Down
17 changes: 16 additions & 1 deletion tests/mlmodel_autogen/test_assistant_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pytest
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.base import TaskResult
from testing_support.fixtures import reset_core_stats_engine, validate_attributes
from testing_support.fixtures import dt_enabled, reset_core_stats_engine, validate_attributes
from testing_support.ml_testing_utils import (
disabled_ai_monitoring_record_content_settings,
disabled_ai_monitoring_settings,
Expand All @@ -27,6 +27,7 @@
from testing_support.validators.validate_custom_event import validate_custom_event_count
from testing_support.validators.validate_custom_events import validate_custom_events
from testing_support.validators.validate_error_trace_attributes import validate_error_trace_attributes
from testing_support.validators.validate_span_events import validate_span_events
from testing_support.validators.validate_transaction_error_event_count import validate_transaction_error_event_count
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics

Expand Down Expand Up @@ -97,6 +98,7 @@ def add_exclamation(message: str) -> str:
return f"{message}!"


@dt_enabled
@reset_core_stats_engine()
@validate_custom_events(
events_with_context_attrs(tool_recorded_event) + events_with_context_attrs(agent_recorded_event)
Expand Down Expand Up @@ -127,6 +129,8 @@ def add_exclamation(message: str) -> str:
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_run_assistant_agent(loop, set_trace_info, single_tool_model_client):
set_trace_info()
Expand All @@ -142,6 +146,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@validate_custom_events(tool_recorded_event + agent_recorded_event)
@validate_custom_event_count(count=2)
Expand Down Expand Up @@ -170,6 +175,8 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_run_stream_assistant_agent(loop, set_trace_info, single_tool_model_client):
set_trace_info()
Expand All @@ -192,6 +199,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_custom_events(tool_events_sans_content(tool_recorded_event) + agent_recorded_event)
Expand Down Expand Up @@ -221,6 +229,8 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_run_assistant_agent_no_content(loop, set_trace_info, single_tool_model_client):
set_trace_info()
Expand All @@ -235,6 +245,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@disabled_ai_monitoring_settings
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
Expand All @@ -259,6 +270,7 @@ async def _test():


@SKIP_IF_AUTOGEN_062
@dt_enabled
@reset_core_stats_engine()
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(callable_name(TypeError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}})
Expand Down Expand Up @@ -289,6 +301,8 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@background_task()
def test_run_assistant_agent_error(loop, set_trace_info, single_tool_model_client_error):
set_trace_info()
Expand All @@ -306,6 +320,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
def test_run_assistant_agent_outside_txn(loop, single_tool_model_client):
Expand Down
25 changes: 24 additions & 1 deletion tests/mlmodel_autogen/test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from autogen_agentchat.base import TaskResult
from autogen_agentchat.teams import RoundRobinGroupChat
from test_assistant_agent import SKIP_IF_AUTOGEN_062
from testing_support.fixtures import reset_core_stats_engine, validate_attributes
from testing_support.fixtures import dt_enabled, reset_core_stats_engine, validate_attributes
from testing_support.ml_testing_utils import (
disabled_ai_monitoring_record_content_settings,
disabled_ai_monitoring_settings,
Expand All @@ -28,6 +28,7 @@
from testing_support.validators.validate_custom_event import validate_custom_event_count
from testing_support.validators.validate_custom_events import validate_custom_events
from testing_support.validators.validate_error_trace_attributes import validate_error_trace_attributes
from testing_support.validators.validate_span_events import validate_span_events
from testing_support.validators.validate_transaction_error_event_count import validate_transaction_error_event_count
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics

Expand Down Expand Up @@ -175,6 +176,7 @@ def compute_sum(a: int, b: int) -> int:
return a + b


@dt_enabled
@reset_core_stats_engine()
@validate_custom_event_count(count=8)
@validate_transaction_metrics(
Expand Down Expand Up @@ -213,6 +215,10 @@ def compute_sum(a: int, b: int) -> int:
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "robot_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "compute_sum"}'})
@background_task()
def test_run_stream_round_robin_group(loop, set_trace_info, multi_tool_model_client):
set_trace_info()
Expand Down Expand Up @@ -250,6 +256,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@validate_custom_event_count(count=8)
@validate_transaction_metrics(
Expand Down Expand Up @@ -287,6 +294,10 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "robot_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "compute_sum"}'})
@background_task()
def test_run_round_robin_group(loop, set_trace_info, multi_tool_model_client):
set_trace_info()
Expand Down Expand Up @@ -319,6 +330,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_custom_events(tool_events_sans_content(team_tools_recorded_events) + team_agent_recorded_events)
Expand Down Expand Up @@ -358,6 +370,10 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "robot_agent"}'})
@validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "compute_sum"}'})
@background_task()
def test_run_round_robin_group_no_content(loop, set_trace_info, multi_tool_model_client):
set_trace_info()
Expand Down Expand Up @@ -385,6 +401,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@disabled_ai_monitoring_settings
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
Expand Down Expand Up @@ -416,6 +433,7 @@ async def _test():


@SKIP_IF_AUTOGEN_062
@dt_enabled
@reset_core_stats_engine()
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(callable_name(TypeError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}})
Expand Down Expand Up @@ -456,6 +474,10 @@ async def _test():
background_task=True,
)
@validate_attributes("agent", ["llm"])
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "pirate_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "add_exclamation"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "robot_agent"}'})
@validate_span_events(count=2, exact_agents={"subcomponent": '{"type": "APM-AI_TOOL", "name": "compute_sum"}'})
@background_task()
def test_run_round_robin_group_error(loop, set_trace_info, multi_tool_model_client_error):
set_trace_info()
Expand Down Expand Up @@ -485,6 +507,7 @@ async def _test():
loop.run_until_complete(_test())


@dt_enabled
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
def test_run_round_robin_group_outside_txn(loop, multi_tool_model_client):
Expand Down