|
20 | 20 | from newrelic.api.function_trace import FunctionTrace |
21 | 21 | from newrelic.api.time_trace import get_trace_linking_metadata |
22 | 22 | from newrelic.api.transaction import current_transaction |
23 | | -from newrelic.common.llm_utils import _get_llm_metadata |
| 23 | +from newrelic.common.llm_utils import AsyncLLMStreamProxy, _get_llm_metadata |
24 | 24 | from newrelic.common.object_names import callable_name |
25 | 25 | from newrelic.common.object_wrapper import wrap_function_wrapper |
26 | 26 | from newrelic.common.package_version_utils import get_package_version |
|
31 | 31 | GOOGLEADK_VERSION = get_package_version("google-adk") |
32 | 32 |
|
33 | 33 | RECORD_EVENTS_FAILURE_LOG_MESSAGE = "Exception occurred in Google ADK instrumentation: Failed to record LLM events. Please report this issue to New Relic Support." |
| 34 | +AGENT_EVENT_FAILURE_LOG_MESSAGE = "Exception occurred in Google ADK instrumentation: Failed to record agent data. Please report this issue to New Relic Support." |
34 | 35 | TOOL_EXTRACTOR_FAILURE_LOG_MESSAGE = "Exception occurred in Google ADK instrumentation: Failed to extract tool information. If the issue persists, report this issue to New Relic support.\n" |
35 | 36 |
|
36 | 37 |
|
| 38 | +def wrap_llm_agent__run_async_impl(wrapped, instance, args, kwargs): |
| 39 | + transaction = current_transaction() |
| 40 | + if not transaction: |
| 41 | + return wrapped(*args, **kwargs) |
| 42 | + |
| 43 | + settings = transaction.settings or global_settings() |
| 44 | + if not settings.ai_monitoring.enabled: |
| 45 | + return wrapped(*args, **kwargs) |
| 46 | + |
| 47 | + agent_name = getattr(instance, "name", "agent") |
| 48 | + function_trace_name = f"run_async/{agent_name}" |
| 49 | + agentic_subcomponent_data = {"type": "APM-AI_AGENT", "name": agent_name} |
| 50 | + |
| 51 | + ft = FunctionTrace(name=function_trace_name, group="Llm/agent/GoogleADK") |
| 52 | + ft.__enter__() |
| 53 | + ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data)) |
| 54 | + |
| 55 | + linking_metadata = get_trace_linking_metadata() |
| 56 | + agent_id = str(uuid.uuid4()) |
| 57 | + |
| 58 | + try: |
| 59 | + return_val = wrapped(*args, **kwargs) |
| 60 | + except Exception: |
| 61 | + ft.__exit__(*sys.exc_info()) |
| 62 | + raise |
| 63 | + |
| 64 | + try: |
| 65 | + proxied_return_val = AsyncLLMStreamProxy( |
| 66 | + wrapped=return_val, |
| 67 | + on_stop_iteration=_record_agent_event_on_stop_iteration, |
| 68 | + on_error=_handle_agent_streaming_completion_error, |
| 69 | + ) |
| 70 | + proxied_return_val._nr_ft = ft |
| 71 | + proxied_return_val._nr_metadata = linking_metadata |
| 72 | + proxied_return_val._nr_adk_attrs = {"agent_name": agent_name, "agent_id": agent_id} |
| 73 | + return proxied_return_val |
| 74 | + except Exception: |
| 75 | + ft.__exit__(*sys.exc_info()) |
| 76 | + return return_val |
| 77 | + |
| 78 | + |
| 79 | +def _record_agent_event_on_stop_iteration(self, transaction): |
| 80 | + if hasattr(self, "_nr_ft"): |
| 81 | + linking_metadata = self._nr_metadata or get_trace_linking_metadata() |
| 82 | + self._nr_ft.__exit__(None, None, None) |
| 83 | + try: |
| 84 | + adk_attrs = getattr(self, "_nr_adk_attrs", {}) |
| 85 | + if not adk_attrs: |
| 86 | + return |
| 87 | + |
| 88 | + agent_name = adk_attrs.get("agent_name", "agent") |
| 89 | + agent_id = adk_attrs.get("agent_id") |
| 90 | + agent_event_dict = _construct_base_agent_event_dict( |
| 91 | + agent_name=agent_name, agent_id=agent_id, transaction=transaction, linking_metadata=linking_metadata |
| 92 | + ) |
| 93 | + agent_event_dict["duration"] = self._nr_ft.duration * 1000 |
| 94 | + transaction.record_custom_event("LlmAgent", agent_event_dict) |
| 95 | + except Exception: |
| 96 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 97 | + finally: |
| 98 | + if hasattr(self, "_nr_adk_attrs"): |
| 99 | + self._nr_adk_attrs.clear() |
| 100 | + |
| 101 | + |
| 102 | +def _handle_agent_streaming_completion_error(self, transaction): |
| 103 | + if hasattr(self, "_nr_ft"): |
| 104 | + adk_attrs = getattr(self, "_nr_adk_attrs", {}) |
| 105 | + if not adk_attrs: |
| 106 | + self._nr_ft.__exit__(*sys.exc_info()) |
| 107 | + return |
| 108 | + |
| 109 | + linking_metadata = self._nr_metadata or get_trace_linking_metadata() |
| 110 | + |
| 111 | + try: |
| 112 | + agent_name = adk_attrs.get("agent_name", "agent") |
| 113 | + agent_id = adk_attrs.get("agent_id") |
| 114 | + |
| 115 | + self._nr_ft.notice_error(attributes={"agent_id": agent_id}) |
| 116 | + self._nr_ft.__exit__(*sys.exc_info()) |
| 117 | + |
| 118 | + agent_event_dict = _construct_base_agent_event_dict( |
| 119 | + agent_name=agent_name, agent_id=agent_id, transaction=transaction, linking_metadata=linking_metadata |
| 120 | + ) |
| 121 | + agent_event_dict.update({"duration": self._nr_ft.duration * 1000, "error": True}) |
| 122 | + transaction.record_custom_event("LlmAgent", agent_event_dict) |
| 123 | + except Exception: |
| 124 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 125 | + finally: |
| 126 | + if hasattr(self, "_nr_adk_attrs"): |
| 127 | + self._nr_adk_attrs.clear() |
| 128 | + |
| 129 | + |
| 130 | +def _construct_base_agent_event_dict(agent_name, agent_id, transaction, linking_metadata): |
| 131 | + try: |
| 132 | + agent_event_dict = { |
| 133 | + "id": agent_id, |
| 134 | + "name": agent_name, |
| 135 | + "span_id": linking_metadata.get("span.id"), |
| 136 | + "trace_id": linking_metadata.get("trace.id"), |
| 137 | + "vendor": "google_adk", |
| 138 | + "ingest_source": "Python", |
| 139 | + } |
| 140 | + agent_event_dict.update(_get_llm_metadata(transaction)) |
| 141 | + except Exception: |
| 142 | + _logger.warning(AGENT_EVENT_FAILURE_LOG_MESSAGE, exc_info=True) |
| 143 | + agent_event_dict = {} |
| 144 | + |
| 145 | + return agent_event_dict |
| 146 | + |
| 147 | + |
37 | 148 | async def wrap__execute_single_function_call_async(wrapped, instance, args, kwargs): |
38 | 149 | transaction = current_transaction() |
39 | 150 | if not transaction: |
@@ -171,6 +282,11 @@ def _construct_base_tool_event_dict( |
171 | 282 | return tool_event_dict |
172 | 283 |
|
173 | 284 |
|
| 285 | +def instrument_googleadk_agents_llm_agent(module): |
| 286 | + if hasattr(module, "LlmAgent") and hasattr(module.LlmAgent, "_run_async_impl"): |
| 287 | + wrap_function_wrapper(module, "LlmAgent._run_async_impl", wrap_llm_agent__run_async_impl) |
| 288 | + |
| 289 | + |
174 | 290 | def instrument_googleadk_flows_llm_flows_functions(module): |
175 | 291 | if hasattr(module, "_execute_single_function_call_async"): |
176 | 292 | wrap_function_wrapper(module, "_execute_single_function_call_async", wrap__execute_single_function_call_async) |
0 commit comments