-
Notifications
You must be signed in to change notification settings - Fork 136
Google ADK Tool Instrumentation #1768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TimPansino
merged 6 commits into
develop-google-adk
from
feat-google-adk-tool-instrumentation
Jun 24, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a7b512
Google ADK Tool Instrumentation
TimPansino eb53c88
Expand Megalinter ignored files
TimPansino 9e66ee0
Apply suggestions from code review
TimPansino 0088aea
Gate subcomponent attrs to local tools
TimPansino 191c655
Merge remote-tracking branch 'origin/develop-google-adk' into feat-go…
TimPansino bf00477
Renaming according to code review
TimPansino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Copyright 2010 New Relic, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import functools | ||
|
|
||
| from _test_agent import AGENT_NAME | ||
| from google.adk.tools import FunctionTool | ||
|
|
||
| TOOL_NAME = "get_capital" | ||
|
|
||
|
|
||
| def get_capital(country: str) -> dict: | ||
| """Return the capital of a country.""" | ||
| capitals = {"France": "Paris", "Japan": "Tokyo"} | ||
| return {"output": capitals.get(country, "Unknown")} | ||
|
|
||
|
|
||
| @functools.wraps(get_capital) # Make function names match | ||
| def _raising_capital(country: str) -> dict: | ||
| """Return the capital of a country.""" | ||
| raise ValueError("intentional tool failure") | ||
|
|
||
|
|
||
| get_capital_tool = FunctionTool(func=get_capital) | ||
| raising_tool = FunctionTool(func=_raising_capital) | ||
|
|
||
| EXPECTED_TOOL_INPUT_STR = "{'country': 'France'}" | ||
| EXPECTED_TOOL_OUTPUT_STR = "{'output': 'Paris'}" | ||
|
|
||
|
|
||
| def tool_recorded_event(record_content: bool): | ||
| base = { | ||
| "id": None, | ||
| "run_id": None, | ||
| "name": TOOL_NAME, | ||
| "span_id": None, | ||
| "trace_id": "trace-id", | ||
| "agent_name": AGENT_NAME, | ||
| "vendor": "google_adk", | ||
| "ingest_source": "Python", | ||
| "duration": None, | ||
| } | ||
| if record_content: | ||
| base["input"] = EXPECTED_TOOL_INPUT_STR | ||
| base["output"] = EXPECTED_TOOL_OUTPUT_STR | ||
| return [({"type": "LlmTool"}, base)] | ||
|
|
||
|
|
||
| def tool_recorded_event_error(record_content: bool): | ||
| base = { | ||
| "id": None, | ||
| "run_id": None, | ||
| "name": TOOL_NAME, | ||
| "span_id": None, | ||
| "trace_id": "trace-id", | ||
| "agent_name": AGENT_NAME, | ||
| "vendor": "google_adk", | ||
| "ingest_source": "Python", | ||
| "duration": None, | ||
| "error": True, | ||
| } | ||
| if record_content: | ||
| base["input"] = EXPECTED_TOOL_INPUT_STR | ||
| return [({"type": "LlmTool"}, base)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Copyright 2010 New Relic, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from _test_agent import AGENT_NAME, PROMPT, agent_recorded_event, build_agent | ||
| from _test_tools import TOOL_NAME, get_capital_tool, tool_recorded_event | ||
| 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, | ||
| events_with_context_attrs, | ||
| ) | ||
| 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_span_events import validate_span_events | ||
| from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics | ||
|
|
||
| from newrelic.api.background_task import background_task | ||
| from newrelic.api.llm_custom_attributes import WithLlmCustomAttributes | ||
| from newrelic.hooks.mlmodel_googleadk import GOOGLEADK_VERSION | ||
|
|
||
| EXPECTED_AGENT_METRIC = (f"Llm/agent/GoogleADK/run_async/{AGENT_NAME}", 1) | ||
| EXPECTED_TOOL_METRIC = (f"Llm/tool/GoogleADK/execute_single_function_call_async/{TOOL_NAME}", 1) | ||
| EXPECTED_SUPPORTABILITY_METRIC = (f"Supportability/Python/ML/GoogleADK/{GOOGLEADK_VERSION}", 1) | ||
|
|
||
|
|
||
| def _validate_events(events): | ||
| assert len(events) == 3 | ||
| assert events[0].content.parts[0].function_call.name == "get_capital" | ||
| assert events[1].content.parts[0].function_response.response["output"] == "Paris" | ||
| assert events[2].content.parts[0].text == "Paris" | ||
|
|
||
|
|
||
| @dt_enabled | ||
| @reset_core_stats_engine() | ||
| @validate_custom_events(events_with_context_attrs(agent_recorded_event + tool_recorded_event(record_content=True))) | ||
| @validate_custom_event_count(count=8) # LlmAgent, LlmTool, 2x (Summary, Input, Output) for the two LLM calls. | ||
| @validate_transaction_metrics( | ||
| "test_tools:test_tool", | ||
| scoped_metrics=[EXPECTED_AGENT_METRIC, EXPECTED_TOOL_METRIC], | ||
| rollup_metrics=[EXPECTED_AGENT_METRIC, EXPECTED_TOOL_METRIC], | ||
| custom_metrics=[EXPECTED_SUPPORTABILITY_METRIC], | ||
| background_task=True, | ||
| ) | ||
| @validate_attributes("agent", ["llm"]) | ||
| @validate_span_events(count=1, exact_agents={"subcomponent": '{"type": "APM-AI_AGENT", "name": "my_agent"}'}) | ||
| @validate_span_events(count=1, exact_agents={"subcomponent": f'{{"type": "APM-AI_TOOL", "name": "{TOOL_NAME}"}}'}) | ||
| @background_task() | ||
| def test_tool(exercise_agent, set_trace_info): | ||
| set_trace_info() | ||
| agent = build_agent(tools=[get_capital_tool]) | ||
| with WithLlmCustomAttributes({"context": "attr"}): | ||
| events = exercise_agent(agent, PROMPT) | ||
|
|
||
| _validate_events(events) | ||
|
|
||
|
|
||
| @dt_enabled | ||
| @reset_core_stats_engine() | ||
| @disabled_ai_monitoring_record_content_settings | ||
| @validate_custom_events(agent_recorded_event + tool_recorded_event(record_content=False)) | ||
| @validate_custom_event_count(count=8) # LlmAgent, LlmTool, 2x (Summary, Input, Output) for the two LLM calls. | ||
| @validate_transaction_metrics( | ||
| "test_tools:test_tool_no_content", | ||
| scoped_metrics=[EXPECTED_AGENT_METRIC, EXPECTED_TOOL_METRIC], | ||
| rollup_metrics=[EXPECTED_AGENT_METRIC, EXPECTED_TOOL_METRIC], | ||
| background_task=True, | ||
| ) | ||
| @validate_attributes("agent", ["llm"]) | ||
| @background_task() | ||
| def test_tool_no_content(exercise_agent, set_trace_info): | ||
| set_trace_info() | ||
| agent = build_agent(tools=[get_capital_tool]) | ||
| events = exercise_agent(agent, PROMPT) | ||
|
|
||
| _validate_events(events) | ||
|
|
||
|
|
||
| @dt_enabled | ||
| @reset_core_stats_engine() | ||
| @disabled_ai_monitoring_settings | ||
| @validate_custom_event_count(count=0) | ||
| @validate_transaction_metrics("test_tools:test_tool_disabled_ai_monitoring", background_task=True) | ||
| @background_task() | ||
| def test_tool_disabled_ai_monitoring(exercise_agent, set_trace_info): | ||
| set_trace_info() | ||
| agent = build_agent(tools=[get_capital_tool]) | ||
| events = exercise_agent(agent, PROMPT) | ||
|
|
||
| _validate_events(events) | ||
|
|
||
|
|
||
| @reset_core_stats_engine() | ||
| @validate_custom_event_count(count=0) | ||
| def test_tool_outside_transaction(exercise_agent, set_trace_info): | ||
| set_trace_info() | ||
| agent = build_agent(tools=[get_capital_tool]) | ||
| events = exercise_agent(agent, PROMPT) | ||
|
|
||
| _validate_events(events) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@umaannamalai Was this part necessary for tools? I'm pretty sure this is the result of a merge conflict but not certain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually have been emitting this metric in the tools instrumentation also for other agentic frameworks. The idea was that we'd still get the AI Responses tab to appear if the tool instrumentation was hit independent of the agents instrumentation.