|
| 1 | +"""Unit tests for LaminarSpanExporter URL normalization.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | +from lmnr.opentelemetry_lib.tracing.exporter import LaminarSpanExporter |
| 6 | + |
| 7 | + |
| 8 | +class TestHttpEndpointNormalization: |
| 9 | + """Test cases for HTTP endpoint URL normalization.""" |
| 10 | + |
| 11 | + @pytest.fixture |
| 12 | + def mock_http_exporter(self): |
| 13 | + """Mock HTTPOTLPSpanExporter to avoid actual initialization.""" |
| 14 | + with patch( |
| 15 | + "lmnr.opentelemetry_lib.tracing.exporter.HTTPOTLPSpanExporter" |
| 16 | + ) as mock: |
| 17 | + mock.return_value = MagicMock() |
| 18 | + yield mock |
| 19 | + |
| 20 | + @pytest.fixture |
| 21 | + def mock_grpc_exporter(self): |
| 22 | + """Mock OTLPSpanExporter to avoid actual initialization.""" |
| 23 | + with patch("lmnr.opentelemetry_lib.tracing.exporter.OTLPSpanExporter") as mock: |
| 24 | + mock.return_value = MagicMock() |
| 25 | + yield mock |
| 26 | + |
| 27 | + def test_http_endpoint_without_path_adds_v1_traces( |
| 28 | + self, mock_http_exporter, mock_grpc_exporter |
| 29 | + ): |
| 30 | + """Test that endpoint without path gets /v1/traces added.""" |
| 31 | + exporter = LaminarSpanExporter( |
| 32 | + base_url="http://localhost:8080", |
| 33 | + api_key="test-key", |
| 34 | + force_http=True, |
| 35 | + ) |
| 36 | + assert exporter is not None |
| 37 | + |
| 38 | + mock_http_exporter.assert_called_once() |
| 39 | + call_kwargs = mock_http_exporter.call_args[1] |
| 40 | + assert call_kwargs["endpoint"] == "http://localhost:8080/v1/traces" |
| 41 | + |
| 42 | + def test_http_endpoint_with_trailing_slash_adds_v1_traces( |
| 43 | + self, mock_http_exporter, mock_grpc_exporter |
| 44 | + ): |
| 45 | + """Test that endpoint with trailing slash gets /v1/traces added.""" |
| 46 | + exporter = LaminarSpanExporter( |
| 47 | + base_url="http://localhost:8080/", |
| 48 | + api_key="test-key", |
| 49 | + force_http=True, |
| 50 | + ) |
| 51 | + assert exporter is not None |
| 52 | + |
| 53 | + mock_http_exporter.assert_called_once() |
| 54 | + call_kwargs = mock_http_exporter.call_args[1] |
| 55 | + assert call_kwargs["endpoint"] == "http://localhost:8080/v1/traces" |
| 56 | + |
| 57 | + def test_grpc_endpoint_not_normalized(self, mock_http_exporter, mock_grpc_exporter): |
| 58 | + """Test that gRPC endpoints are not normalized (only HTTP).""" |
| 59 | + exporter = LaminarSpanExporter( |
| 60 | + base_url="http://localhost:8443", |
| 61 | + api_key="test-key", |
| 62 | + force_http=False, |
| 63 | + ) |
| 64 | + assert exporter is not None |
| 65 | + |
| 66 | + mock_grpc_exporter.assert_called_once() |
| 67 | + mock_http_exporter.assert_not_called() |
| 68 | + call_kwargs = mock_grpc_exporter.call_args[1] |
| 69 | + assert call_kwargs["endpoint"] == "http://localhost:8443" |
| 70 | + |
| 71 | + def test_https_endpoint_without_path_adds_v1_traces( |
| 72 | + self, mock_http_exporter, mock_grpc_exporter |
| 73 | + ): |
| 74 | + """Test that HTTPS endpoint without path gets /v1/traces added.""" |
| 75 | + exporter = LaminarSpanExporter( |
| 76 | + base_url="https://api.example.com:443", |
| 77 | + api_key="test-key", |
| 78 | + force_http=True, |
| 79 | + ) |
| 80 | + assert exporter is not None |
| 81 | + |
| 82 | + mock_http_exporter.assert_called_once() |
| 83 | + call_kwargs = mock_http_exporter.call_args[1] |
| 84 | + assert call_kwargs["endpoint"] == "https://api.example.com:443/v1/traces" |
| 85 | + |
| 86 | + def test_info_logging_when_path_added(self, mock_http_exporter, mock_grpc_exporter): |
| 87 | + """Test that info message is logged when path is added.""" |
| 88 | + with patch("lmnr.opentelemetry_lib.tracing.exporter.logger") as mock_logger: |
| 89 | + exporter = LaminarSpanExporter( |
| 90 | + base_url="http://localhost:8080", |
| 91 | + api_key="test-key", |
| 92 | + force_http=True, |
| 93 | + ) |
| 94 | + assert exporter is not None |
| 95 | + |
| 96 | + mock_logger.info.assert_called_once() |
| 97 | + call_args = mock_logger.info.call_args[0][0] |
| 98 | + assert "No path found in HTTP endpoint URL" in call_args |
| 99 | + assert "Adding default path /v1/traces" in call_args |
| 100 | + |
| 101 | + def test_no_logging_when_path_exists( |
| 102 | + self, mock_http_exporter, mock_grpc_exporter, caplog |
| 103 | + ): |
| 104 | + """Test that no info message is logged when path already exists.""" |
| 105 | + import logging |
| 106 | + |
| 107 | + caplog.set_level(logging.INFO) |
| 108 | + |
| 109 | + exporter = LaminarSpanExporter( |
| 110 | + base_url="http://localhost:8080/custom/path", |
| 111 | + api_key="test-key", |
| 112 | + force_http=True, |
| 113 | + ) |
| 114 | + assert exporter is not None |
| 115 | + |
| 116 | + assert not any( |
| 117 | + "No path found in HTTP endpoint URL" in record.message |
| 118 | + for record in caplog.records |
| 119 | + ) |
| 120 | + |
| 121 | + def test_normalize_http_endpoint_directly(self): |
| 122 | + """Test the _normalize_http_endpoint method directly.""" |
| 123 | + with ( |
| 124 | + patch( |
| 125 | + "lmnr.opentelemetry_lib.tracing.exporter.HTTPOTLPSpanExporter" |
| 126 | + ) as mock_http, |
| 127 | + patch( |
| 128 | + "lmnr.opentelemetry_lib.tracing.exporter.OTLPSpanExporter" |
| 129 | + ) as mock_grpc, |
| 130 | + ): |
| 131 | + mock_http.return_value = MagicMock() |
| 132 | + mock_grpc.return_value = MagicMock() |
| 133 | + |
| 134 | + exporter = LaminarSpanExporter( |
| 135 | + base_url="http://localhost:8080", |
| 136 | + api_key="test-key", |
| 137 | + force_http=True, |
| 138 | + ) |
| 139 | + |
| 140 | + test_cases = [ |
| 141 | + ("http://example.com", "http://example.com/v1/traces"), |
| 142 | + ("http://example.com/", "http://example.com/v1/traces"), |
| 143 | + ("https://example.com:443", "https://example.com:443/v1/traces"), |
| 144 | + ("http://example.com/path", "http://example.com/path"), |
| 145 | + ("http://example.com/v1/traces", "http://example.com/v1/traces"), |
| 146 | + ] |
| 147 | + |
| 148 | + for input_url, expected_url in test_cases: |
| 149 | + result = exporter._normalize_http_endpoint(input_url) |
| 150 | + assert ( |
| 151 | + result == expected_url |
| 152 | + ), f"Expected {expected_url}, got {result} for input {input_url}" |
0 commit comments