|
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 | + transaction.add_ml_model_info("GoogleADK", GOOGLEADK_VERSION) |
| 48 | + transaction._add_agent_attribute("llm", True) |
| 49 | + |
| 50 | + agent_name = getattr(instance, "name", "agent") |
| 51 | + function_trace_name = f"run_async/{agent_name}" |
| 52 | + agentic_subcomponent_data = {"type": "APM-AI_AGENT", "name": agent_name} |
| 53 | + |
| 54 | + ft = FunctionTrace(name=function_trace_name, group="Llm/agent/GoogleADK") |
| 55 | + ft.__enter__() |
| 56 | + ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data)) |
| 57 | + |
| 58 | + linking_metadata = get_trace_linking_metadata() |
| 59 | + agent_id = str(uuid.uuid4()) |
| 60 | + |
| 61 | + try: |
| 62 | + return_val = wrapped(*args, **kwargs) |
| 63 | + except Exception: |
| 64 | + ft.__exit__(*sys.exc_info()) |
| 65 | + raise |
| 66 | + |
| 67 | + try: |
| 68 | + proxied_return_val = AsyncLLMStreamProxy( |
| 69 | + wrapped=return_val, |
| 70 | + on_stop_iteration=_record_agent_event_on_stop_iteration, |
| 71 | + on_error=_handle_agent_streaming_completion_error, |
| 72 | + ) |
| 73 | + proxied_return_val._nr_ft = ft |
| 74 | + proxied_return_val._nr_metadata = linking_metadata |
| 75 | + proxied_return_val._nr_adk_attrs = {"agent_name": agent_name, "agent_id": agent_id} |
| 76 | + return proxied_return_val |
| 77 | + except Exception: |
| 78 | + ft.__exit__(*sys.exc_info()) |
| 79 | + return return_val |
| 80 | + |
| 81 | + |
| 82 | +def _record_agent_event_on_stop_iteration(self, transaction): |
| 83 | + if hasattr(self, "_nr_ft"): |
| 84 | + linking_metadata = self._nr_metadata or get_trace_linking_metadata() |
| 85 | + self._nr_ft.__exit__(None, None, None) |
| 86 | + try: |
| 87 | + adk_attrs = getattr(self, "_nr_adk_attrs", {}) |
| 88 | + if not adk_attrs: |
| 89 | + return |
| 90 | + |
| 91 | + agent_name = adk_attrs.get("agent_name", "agent") |
| 92 | + agent_id = adk_attrs.get("agent_id") |
| 93 | + agent_event_dict = _construct_base_agent_event_dict( |
| 94 | + agent_name=agent_name, agent_id=agent_id, transaction=transaction, linking_metadata=linking_metadata |
| 95 | + ) |
| 96 | + agent_event_dict["duration"] = self._nr_ft.duration * 1000 |
| 97 | + transaction.record_custom_event("LlmAgent", agent_event_dict) |
| 98 | + except Exception: |
| 99 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 100 | + finally: |
| 101 | + if hasattr(self, "_nr_adk_attrs"): |
| 102 | + self._nr_adk_attrs.clear() |
| 103 | + |
| 104 | + |
| 105 | +def _handle_agent_streaming_completion_error(self, transaction): |
| 106 | + if hasattr(self, "_nr_ft"): |
| 107 | + adk_attrs = getattr(self, "_nr_adk_attrs", {}) |
| 108 | + if not adk_attrs: |
| 109 | + self._nr_ft.__exit__(*sys.exc_info()) |
| 110 | + return |
| 111 | + |
| 112 | + linking_metadata = self._nr_metadata or get_trace_linking_metadata() |
| 113 | + |
| 114 | + try: |
| 115 | + agent_name = adk_attrs.get("agent_name", "agent") |
| 116 | + agent_id = adk_attrs.get("agent_id") |
| 117 | + |
| 118 | + self._nr_ft.notice_error(attributes={"agent_id": agent_id}) |
| 119 | + self._nr_ft.__exit__(*sys.exc_info()) |
| 120 | + |
| 121 | + agent_event_dict = _construct_base_agent_event_dict( |
| 122 | + agent_name=agent_name, agent_id=agent_id, transaction=transaction, linking_metadata=linking_metadata |
| 123 | + ) |
| 124 | + agent_event_dict.update({"duration": self._nr_ft.duration * 1000, "error": True}) |
| 125 | + transaction.record_custom_event("LlmAgent", agent_event_dict) |
| 126 | + except Exception: |
| 127 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 128 | + finally: |
| 129 | + if hasattr(self, "_nr_adk_attrs"): |
| 130 | + self._nr_adk_attrs.clear() |
| 131 | + |
| 132 | + |
| 133 | +def _construct_base_agent_event_dict(agent_name, agent_id, transaction, linking_metadata): |
| 134 | + try: |
| 135 | + agent_event_dict = { |
| 136 | + "id": agent_id, |
| 137 | + "name": agent_name, |
| 138 | + "span_id": linking_metadata.get("span.id"), |
| 139 | + "trace_id": linking_metadata.get("trace.id"), |
| 140 | + "vendor": "google_adk", |
| 141 | + "ingest_source": "Python", |
| 142 | + } |
| 143 | + agent_event_dict.update(_get_llm_metadata(transaction)) |
| 144 | + except Exception: |
| 145 | + _logger.warning(AGENT_EVENT_FAILURE_LOG_MESSAGE, exc_info=True) |
| 146 | + agent_event_dict = {} |
| 147 | + |
| 148 | + return agent_event_dict |
| 149 | + |
| 150 | + |
37 | 151 | async def wrap__execute_single_function_call_async(wrapped, instance, args, kwargs): |
38 | 152 | transaction = current_transaction() |
39 | 153 | if not transaction: |
@@ -171,6 +285,11 @@ def _construct_base_tool_event_dict( |
171 | 285 | return tool_event_dict |
172 | 286 |
|
173 | 287 |
|
| 288 | +def instrument_googleadk_agents_llm_agent(module): |
| 289 | + if hasattr(module, "LlmAgent") and hasattr(module.LlmAgent, "_run_async_impl"): |
| 290 | + wrap_function_wrapper(module, "LlmAgent._run_async_impl", wrap_llm_agent__run_async_impl) |
| 291 | + |
| 292 | + |
174 | 293 | def instrument_googleadk_flows_llm_flows_functions(module): |
175 | 294 | if hasattr(module, "_execute_single_function_call_async"): |
176 | 295 | wrap_function_wrapper(module, "_execute_single_function_call_async", wrap__execute_single_function_call_async) |
0 commit comments