|
23 | 23 | from newrelic.common.llm_utils import AsyncLLMStreamProxy, _get_llm_metadata |
24 | 24 | from newrelic.common.object_wrapper import wrap_function_wrapper |
25 | 25 | from newrelic.common.package_version_utils import get_package_version |
| 26 | +from newrelic.common.signature import bind_args |
26 | 27 | from newrelic.core.config import global_settings |
27 | 28 |
|
28 | 29 | _logger = logging.getLogger(__name__) |
29 | 30 | GOOGLEADK_VERSION = get_package_version("google-adk") |
30 | 31 |
|
31 | 32 | RECORD_EVENTS_FAILURE_LOG_MESSAGE = "Exception occurred in Google ADK instrumentation: Failed to record LLM events. Please report this issue to New Relic Support." |
32 | 33 | 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 | +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" |
33 | 35 |
|
34 | 36 |
|
35 | 37 | def wrap_llm_agent__run_async_impl(wrapped, instance, args, kwargs): |
@@ -145,6 +147,147 @@ def _construct_base_agent_event_dict(agent_name, agent_id, transaction, linking_ |
145 | 147 | return agent_event_dict |
146 | 148 |
|
147 | 149 |
|
| 150 | +async def wrap__execute_single_function_call_async(wrapped, instance, args, kwargs): |
| 151 | + transaction = current_transaction() |
| 152 | + if not transaction: |
| 153 | + return await wrapped(*args, **kwargs) |
| 154 | + |
| 155 | + settings = transaction.settings or global_settings() |
| 156 | + if not settings.ai_monitoring.enabled: |
| 157 | + return await wrapped(*args, **kwargs) |
| 158 | + |
| 159 | + transaction.add_ml_model_info("GoogleADK", GOOGLEADK_VERSION) |
| 160 | + transaction._add_agent_attribute("llm", True) |
| 161 | + |
| 162 | + tool_name = "tool" |
| 163 | + run_id = "" |
| 164 | + tool_input = None |
| 165 | + agent_name = "agent" |
| 166 | + is_local_tool = False |
| 167 | + try: |
| 168 | + bound_args = bind_args(wrapped, args, kwargs) |
| 169 | + function_call = bound_args.get("function_call") |
| 170 | + agent = bound_args.get("agent") |
| 171 | + tools_dict = bound_args.get("tools_dict") |
| 172 | + if function_call is not None: |
| 173 | + tool_name = getattr(function_call, "name", "tool") or "tool" |
| 174 | + run_id = getattr(function_call, "id", "") or "" |
| 175 | + tool_input = getattr(function_call, "args", None) |
| 176 | + if tools_dict is not None: |
| 177 | + from google.adk.tools.function_tool import FunctionTool |
| 178 | + |
| 179 | + is_local_tool = isinstance(tools_dict.get(tool_name), FunctionTool) |
| 180 | + if agent is not None: |
| 181 | + agent_name = getattr(agent, "name", "agent") or "agent" |
| 182 | + except Exception: |
| 183 | + _logger.warning(TOOL_EXTRACTOR_FAILURE_LOG_MESSAGE, exc_info=True) |
| 184 | + |
| 185 | + function_trace_name = f"execute_single_function_call_async/{tool_name}" |
| 186 | + |
| 187 | + ft = FunctionTrace(name=function_trace_name, group="Llm/tool/GoogleADK") |
| 188 | + ft.__enter__() |
| 189 | + if is_local_tool: |
| 190 | + agentic_subcomponent_data = {"type": "APM-AI_TOOL", "name": tool_name} |
| 191 | + ft._add_agent_attribute("subcomponent", json.dumps(agentic_subcomponent_data)) |
| 192 | + linking_metadata = get_trace_linking_metadata() |
| 193 | + tool_id = str(uuid.uuid4()) |
| 194 | + |
| 195 | + try: |
| 196 | + tool_output = await wrapped(*args, **kwargs) |
| 197 | + except Exception: |
| 198 | + ft.notice_error(attributes={"tool_id": tool_id}) |
| 199 | + ft.__exit__(*sys.exc_info()) |
| 200 | + try: |
| 201 | + tool_event_dict = _construct_base_tool_event_dict( |
| 202 | + tool_name=tool_name, |
| 203 | + tool_id=tool_id, |
| 204 | + run_id=run_id, |
| 205 | + tool_input=tool_input, |
| 206 | + tool_output=None, |
| 207 | + agent_name=agent_name, |
| 208 | + error=True, |
| 209 | + transaction=transaction, |
| 210 | + linking_metadata=linking_metadata, |
| 211 | + ) |
| 212 | + if tool_event_dict: |
| 213 | + tool_event_dict["duration"] = ft.duration * 1000 |
| 214 | + transaction.record_custom_event("LlmTool", tool_event_dict) |
| 215 | + except Exception: |
| 216 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 217 | + raise |
| 218 | + |
| 219 | + ft.__exit__(None, None, None) |
| 220 | + try: |
| 221 | + response_dict = _extract_tool_response_dict(tool_output) |
| 222 | + tool_event_dict = _construct_base_tool_event_dict( |
| 223 | + tool_name=tool_name, |
| 224 | + tool_id=tool_id, |
| 225 | + run_id=run_id, |
| 226 | + tool_input=tool_input, |
| 227 | + tool_output=response_dict, |
| 228 | + agent_name=agent_name, |
| 229 | + error=False, |
| 230 | + transaction=transaction, |
| 231 | + linking_metadata=linking_metadata, |
| 232 | + ) |
| 233 | + if tool_event_dict: |
| 234 | + tool_event_dict["duration"] = ft.duration * 1000 |
| 235 | + transaction.record_custom_event("LlmTool", tool_event_dict) |
| 236 | + except Exception: |
| 237 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 238 | + |
| 239 | + return tool_output |
| 240 | + |
| 241 | + |
| 242 | +def _extract_tool_response_dict(tool_output): |
| 243 | + """Return the dict at content.parts[*].function_response.response, or None.""" |
| 244 | + try: |
| 245 | + parts = tool_output.content.parts |
| 246 | + for part in parts: |
| 247 | + function_response = getattr(part, "function_response", None) |
| 248 | + if function_response is not None: |
| 249 | + return getattr(function_response, "response", None) |
| 250 | + except (AttributeError, TypeError): |
| 251 | + pass |
| 252 | + return None |
| 253 | + |
| 254 | + |
| 255 | +def _construct_base_tool_event_dict( |
| 256 | + tool_name, tool_id, run_id, tool_input, tool_output, agent_name, error, transaction, linking_metadata |
| 257 | +): |
| 258 | + try: |
| 259 | + settings = transaction.settings or global_settings() |
| 260 | + |
| 261 | + tool_event_dict = { |
| 262 | + "id": tool_id, |
| 263 | + "run_id": run_id, |
| 264 | + "name": tool_name, |
| 265 | + "span_id": linking_metadata.get("span.id"), |
| 266 | + "trace_id": linking_metadata.get("trace.id"), |
| 267 | + "agent_name": agent_name, |
| 268 | + "vendor": "google_adk", |
| 269 | + "ingest_source": "Python", |
| 270 | + } |
| 271 | + if error: |
| 272 | + tool_event_dict["error"] = True |
| 273 | + |
| 274 | + if settings.ai_monitoring.record_content.enabled: |
| 275 | + tool_event_dict["input"] = str(tool_input) if tool_input else None |
| 276 | + tool_event_dict["output"] = str(tool_output) if tool_output else None |
| 277 | + |
| 278 | + tool_event_dict.update(_get_llm_metadata(transaction)) |
| 279 | + except Exception: |
| 280 | + tool_event_dict = {} |
| 281 | + _logger.warning(RECORD_EVENTS_FAILURE_LOG_MESSAGE, exc_info=True) |
| 282 | + |
| 283 | + return tool_event_dict |
| 284 | + |
| 285 | + |
148 | 286 | def instrument_googleadk_agents_llm_agent(module): |
149 | 287 | if hasattr(module, "LlmAgent") and hasattr(module.LlmAgent, "_run_async_impl"): |
150 | 288 | wrap_function_wrapper(module, "LlmAgent._run_async_impl", wrap_llm_agent__run_async_impl) |
| 289 | + |
| 290 | + |
| 291 | +def instrument_googleadk_flows_llm_flows_functions(module): |
| 292 | + if hasattr(module, "_execute_single_function_call_async"): |
| 293 | + wrap_function_wrapper(module, "_execute_single_function_call_async", wrap__execute_single_function_call_async) |
0 commit comments