Skip to content

Commit 6800198

Browse files
Fix URL construction for domain with port
- Updated URL construction logic to check for '://' to distinguish between real protocols and domain:port format - Added test for domain with port (example.com:8080) to ensure https:// is prepended correctly - All 28 tests pass Co-authored-by: sergioescalera <[email protected]>
1 parent d9dae93 commit 6800198

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/exporters/agent365_exporter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
102102
else f"/maven/agent365/agents/{agent_id}/traces"
103103
)
104104

105-
# Construct URL - if endpoint already has a scheme (http:// or https://), use it as-is
105+
# Construct URL - if endpoint has a scheme (http:// or https://), use it as-is
106106
# Otherwise, prepend https://
107+
# Note: Check for "://" to distinguish between real protocols and domain:port format
107108
parsed = urlparse(endpoint)
108-
if parsed.scheme:
109+
if parsed.scheme and "://" in endpoint:
109110
# Endpoint is a full URL, append path
110111
url = f"{endpoint}{endpoint_path}?api-version=1"
111112
else:
112-
# Endpoint is just a domain, prepend https://
113+
# Endpoint is just a domain (possibly with port), prepend https://
113114
url = f"https://{endpoint}{endpoint_path}?api-version=1"
114115

115116
# Debug: Log endpoint being used

tests/observability/core/test_agent365_exporter.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,41 @@ def test_export_uses_valid_url_override_with_http(self):
577577
# Verify PowerPlatformApiDiscovery was not called
578578
mock_discovery_class.assert_not_called()
579579

580+
def test_export_uses_valid_domain_override_with_port(self):
581+
"""Test that domain override with port (no protocol) is accepted and https:// is prepended."""
582+
# Arrange
583+
os.environ["A365_OBSERVABILITY_DOMAIN_OVERRIDE"] = "example.com:8080"
584+
585+
# Create exporter after setting environment variable
586+
exporter = _Agent365Exporter(
587+
token_resolver=self.mock_token_resolver, cluster_category="test"
588+
)
589+
590+
spans = [self._create_mock_span("test_span")]
591+
592+
# Mock the PowerPlatformApiDiscovery class (should NOT be called since override is valid)
593+
with patch(
594+
"microsoft_agents_a365.observability.core.exporters.agent365_exporter.PowerPlatformApiDiscovery"
595+
) as mock_discovery_class:
596+
# Mock the _post_with_retries method
597+
with patch.object(exporter, "_post_with_retries", return_value=True) as mock_post:
598+
# Act
599+
result = exporter.export(spans)
600+
601+
# Assert
602+
self.assertEqual(result, SpanExportResult.SUCCESS)
603+
mock_post.assert_called_once()
604+
605+
# Verify the call arguments - should prepend https:// to domain with port
606+
args, kwargs = mock_post.call_args
607+
url, body, headers = args
608+
609+
expected_url = "https://example.com:8080/maven/agent365/agents/test-agent-456/traces?api-version=1"
610+
self.assertEqual(url, expected_url)
611+
612+
# Verify PowerPlatformApiDiscovery was not called
613+
mock_discovery_class.assert_not_called()
614+
580615
def test_export_ignores_invalid_domain_with_protocol(self):
581616
"""Test that domain override with invalid protocol is ignored."""
582617
# Arrange

0 commit comments

Comments
 (0)