-
Notifications
You must be signed in to change notification settings - Fork 219
feat(DATAGO-128305): instrument MCP connector with outbound.request.duration #1385
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
Closed
+149
−2
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,108 @@ | ||
| """Tests for MCP connector observability instrumentation. | ||
|
|
||
| Verifies outbound.request.duration metrics are recorded with correct labels | ||
| when MCP tool calls are executed through EmbedResolvingMCPTool. | ||
| """ | ||
|
|
||
| import pytest | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from solace_agent_mesh.agent.adk.embed_resolving_mcp_toolset import ( | ||
| EmbedResolvingMCPTool, | ||
| ) | ||
|
|
||
|
|
||
| def _make_embed_tool(): | ||
| mock_original_tool = Mock() | ||
| mock_original_tool.name = "test_mcp_tool" | ||
| mock_original_tool._mcp_tool = Mock() | ||
| mock_original_tool._mcp_tool.name = "test_mcp_tool" | ||
| mock_original_tool._mcp_tool.auth_scheme = None | ||
| mock_original_tool._mcp_tool.auth_credential = None | ||
| mock_original_tool._mcp_session_manager = Mock() | ||
| return EmbedResolvingMCPTool( | ||
| original_mcp_tool=mock_original_tool, | ||
| tool_config=None, | ||
| credential_manager=None, | ||
| ) | ||
|
|
||
|
|
||
| def _make_tool_context(): | ||
| mock_session = Mock() | ||
| mock_session.user_id = "user123" | ||
| mock_session.id = "session456" | ||
| mock_tool_context = Mock() | ||
| mock_tool_context.session = mock_session | ||
| mock_tool_context.agent_name = "test-agent" | ||
| return mock_tool_context | ||
|
|
||
|
|
||
| def _capture_metrics(): | ||
| recorded = [] | ||
|
|
||
| def capture_record(duration, labels): | ||
| recorded.append({"duration": duration, "labels": dict(labels)}) | ||
|
|
||
| mock_recorder = Mock() | ||
| mock_recorder.record = capture_record | ||
| mock_registry = Mock() | ||
| mock_registry.get_recorder.return_value = mock_recorder | ||
| return recorded, mock_registry | ||
|
|
||
|
|
||
| def _find_metric(recorded, **expected_labels): | ||
| for m in recorded: | ||
| if all(m["labels"].get(k) == v for k, v in expected_labels.items()): | ||
| return m | ||
| return None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| class TestMcpObservability: | ||
|
|
||
| async def test_successful_call_records_metric(self): | ||
| embed_tool = _make_embed_tool() | ||
| tool_context = _make_tool_context() | ||
|
|
||
| async def mock_tool_call(): | ||
| return {"result": "success"} | ||
|
|
||
| recorded, mock_registry = _capture_metrics() | ||
| with patch( | ||
| "solace_ai_connector.common.observability.api.MetricRegistry" | ||
| ) as mock_reg_cls, patch( | ||
| "solace_agent_mesh.agent.adk.embed_resolving_mcp_toolset._log_mcp_tool_call" | ||
| ), patch( | ||
| "solace_agent_mesh.agent.adk.embed_resolving_mcp_toolset._log_mcp_tool_success" | ||
| ): | ||
| mock_reg_cls.get_instance.return_value = mock_registry | ||
| result = await embed_tool._execute_tool_with_audit_logs(mock_tool_call, tool_context) | ||
|
|
||
| assert result == {"result": "success"} | ||
| metric = _find_metric(recorded, **{"service.peer.name": "mcp_server", "operation.name": "call_tool"}) | ||
| assert metric is not None, f"Expected metric not found in {recorded}" | ||
| assert metric["labels"]["error.type"] == "none" | ||
| assert metric["duration"] >= 0 | ||
|
|
||
| async def test_failed_call_records_error_metric(self): | ||
| embed_tool = _make_embed_tool() | ||
| tool_context = _make_tool_context() | ||
|
|
||
| async def mock_tool_call(): | ||
| raise ValueError("MCP server error") | ||
|
|
||
| recorded, mock_registry = _capture_metrics() | ||
| with patch( | ||
| "solace_ai_connector.common.observability.api.MetricRegistry" | ||
| ) as mock_reg_cls, patch( | ||
| "solace_agent_mesh.agent.adk.embed_resolving_mcp_toolset._log_mcp_tool_call" | ||
| ), patch( | ||
| "solace_agent_mesh.agent.adk.embed_resolving_mcp_toolset._log_mcp_tool_failure" | ||
| ): | ||
| mock_reg_cls.get_instance.return_value = mock_registry | ||
| with pytest.raises(ValueError): | ||
| await embed_tool._execute_tool_with_audit_logs(mock_tool_call, tool_context) | ||
|
|
||
| metric = _find_metric(recorded, **{"service.peer.name": "mcp_server", "operation.name": "call_tool"}) | ||
| assert metric is not None, f"Expected metric not found in {recorded}" | ||
| assert metric["labels"]["error.type"] == "ValueError" |
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.
it doesn't look like we can distinguish the tool_name being invoked here - unless there is just one ? - can you please confirm no matter what tool is being passed here we always hit same endpoint - if this is the case we could live with this solution