|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +from unittest.mock import patch |
| 16 | + |
15 | 17 | from pyramid.config import Configurator
|
16 | 18 |
|
| 19 | +from opentelemetry import trace |
17 | 20 | from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
|
| 21 | +from opentelemetry.test.globals_test import reset_trace_globals |
18 | 22 | from opentelemetry.test.test_base import TestBase
|
19 | 23 | from opentelemetry.test.wsgitestutil import WsgiTestBase
|
20 | 24 | from opentelemetry.trace import SpanKind
|
| 25 | +from opentelemetry.util.http import ( |
| 26 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST, |
| 27 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE, |
| 28 | +) |
21 | 29 |
|
22 | 30 | # pylint: disable=import-error
|
23 | 31 | from .pyramid_base_test import InstrumentationTest
|
@@ -109,3 +117,146 @@ def test_with_existing_span(self):
|
109 | 117 | parent_span.get_span_context().span_id,
|
110 | 118 | span_list[0].parent.span_id,
|
111 | 119 | )
|
| 120 | + |
| 121 | + |
| 122 | +class TestCustomRequestResponseHeaders( |
| 123 | + InstrumentationTest, TestBase, WsgiTestBase |
| 124 | +): |
| 125 | + def setUp(self): |
| 126 | + super().setUp() |
| 127 | + PyramidInstrumentor().instrument() |
| 128 | + self.config = Configurator() |
| 129 | + self._common_initialization(self.config) |
| 130 | + self.env_patch = patch.dict( |
| 131 | + "os.environ", |
| 132 | + { |
| 133 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,invalid-header", |
| 134 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "content-type,content-length,my-custom-header,invalid-header", |
| 135 | + }, |
| 136 | + ) |
| 137 | + self.env_patch.start() |
| 138 | + |
| 139 | + def tearDown(self) -> None: |
| 140 | + super().tearDown() |
| 141 | + self.env_patch.stop() |
| 142 | + with self.disable_logging(): |
| 143 | + PyramidInstrumentor().uninstrument() |
| 144 | + |
| 145 | + def test_custom_request_header_added_in_server_span(self): |
| 146 | + headers = { |
| 147 | + "Custom-Test-Header-1": "Test Value 1", |
| 148 | + "Custom-Test-Header-2": "TestValue2,TestValue3", |
| 149 | + "Custom-Test-Header-3": "TestValue4", |
| 150 | + } |
| 151 | + resp = self.client.get("/hello/123", headers=headers) |
| 152 | + self.assertEqual(200, resp.status_code) |
| 153 | + span = self.memory_exporter.get_finished_spans()[0] |
| 154 | + expected = { |
| 155 | + "http.request.header.custom_test_header_1": ("Test Value 1",), |
| 156 | + "http.request.header.custom_test_header_2": ( |
| 157 | + "TestValue2,TestValue3", |
| 158 | + ), |
| 159 | + } |
| 160 | + not_expected = { |
| 161 | + "http.request.header.custom_test_header_3": ("TestValue4",), |
| 162 | + } |
| 163 | + self.assertEqual(span.kind, SpanKind.SERVER) |
| 164 | + self.assertSpanHasAttributes(span, expected) |
| 165 | + for key, _ in not_expected.items(): |
| 166 | + self.assertNotIn(key, span.attributes) |
| 167 | + |
| 168 | + def test_custom_request_header_not_added_in_internal_span(self): |
| 169 | + tracer = trace.get_tracer(__name__) |
| 170 | + with tracer.start_as_current_span("test", kind=SpanKind.SERVER): |
| 171 | + headers = { |
| 172 | + "Custom-Test-Header-1": "Test Value 1", |
| 173 | + "Custom-Test-Header-2": "TestValue2,TestValue3", |
| 174 | + } |
| 175 | + resp = self.client.get("/hello/123", headers=headers) |
| 176 | + self.assertEqual(200, resp.status_code) |
| 177 | + span = self.memory_exporter.get_finished_spans()[0] |
| 178 | + not_expected = { |
| 179 | + "http.request.header.custom_test_header_1": ("Test Value 1",), |
| 180 | + "http.request.header.custom_test_header_2": ( |
| 181 | + "TestValue2,TestValue3", |
| 182 | + ), |
| 183 | + } |
| 184 | + self.assertEqual(span.kind, SpanKind.INTERNAL) |
| 185 | + for key, _ in not_expected.items(): |
| 186 | + self.assertNotIn(key, span.attributes) |
| 187 | + |
| 188 | + def test_custom_response_header_added_in_server_span(self): |
| 189 | + resp = self.client.get("/test_custom_response_headers") |
| 190 | + self.assertEqual(200, resp.status_code) |
| 191 | + span = self.memory_exporter.get_finished_spans()[0] |
| 192 | + expected = { |
| 193 | + "http.response.header.content_type": ( |
| 194 | + "text/plain; charset=utf-8", |
| 195 | + ), |
| 196 | + "http.response.header.content_length": ("7",), |
| 197 | + "http.response.header.my_custom_header": ( |
| 198 | + "my-custom-value-1,my-custom-header-2", |
| 199 | + ), |
| 200 | + } |
| 201 | + not_expected = { |
| 202 | + "http.response.header.dont_capture_me": ("test-value",) |
| 203 | + } |
| 204 | + self.assertEqual(span.kind, SpanKind.SERVER) |
| 205 | + self.assertSpanHasAttributes(span, expected) |
| 206 | + for key, _ in not_expected.items(): |
| 207 | + self.assertNotIn(key, span.attributes) |
| 208 | + |
| 209 | + def test_custom_response_header_not_added_in_internal_span(self): |
| 210 | + tracer = trace.get_tracer(__name__) |
| 211 | + with tracer.start_as_current_span("test", kind=SpanKind.SERVER): |
| 212 | + resp = self.client.get("/test_custom_response_headers") |
| 213 | + self.assertEqual(200, resp.status_code) |
| 214 | + span = self.memory_exporter.get_finished_spans()[0] |
| 215 | + not_expected = { |
| 216 | + "http.response.header.content_type": ( |
| 217 | + "text/plain; charset=utf-8", |
| 218 | + ), |
| 219 | + "http.response.header.content_length": ("7",), |
| 220 | + "http.response.header.my_custom_header": ( |
| 221 | + "my-custom-value-1,my-custom-header-2", |
| 222 | + ), |
| 223 | + } |
| 224 | + self.assertEqual(span.kind, SpanKind.INTERNAL) |
| 225 | + for key, _ in not_expected.items(): |
| 226 | + self.assertNotIn(key, span.attributes) |
| 227 | + |
| 228 | + |
| 229 | +class TestCustomHeadersNonRecordingSpan( |
| 230 | + InstrumentationTest, TestBase, WsgiTestBase |
| 231 | +): |
| 232 | + def setUp(self): |
| 233 | + super().setUp() |
| 234 | + # This is done because set_tracer_provider cannot override the |
| 235 | + # current tracer provider. |
| 236 | + reset_trace_globals() |
| 237 | + tracer_provider = trace.NoOpTracerProvider() |
| 238 | + trace.set_tracer_provider(tracer_provider) |
| 239 | + PyramidInstrumentor().instrument() |
| 240 | + self.config = Configurator() |
| 241 | + self._common_initialization(self.config) |
| 242 | + self.env_patch = patch.dict( |
| 243 | + "os.environ", |
| 244 | + { |
| 245 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,invalid-header", |
| 246 | + OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "content-type,content-length,my-custom-header,invalid-header", |
| 247 | + }, |
| 248 | + ) |
| 249 | + self.env_patch.start() |
| 250 | + |
| 251 | + def tearDown(self) -> None: |
| 252 | + super().tearDown() |
| 253 | + self.env_patch.stop() |
| 254 | + with self.disable_logging(): |
| 255 | + PyramidInstrumentor().uninstrument() |
| 256 | + |
| 257 | + def test_custom_header_non_recording_span(self): |
| 258 | + try: |
| 259 | + resp = self.client.get("/hello/123") |
| 260 | + self.assertEqual(200, resp.status_code) |
| 261 | + except Exception as exc: # pylint: disable=W0703 |
| 262 | + self.fail(f"Exception raised with NonRecordingSpan {exc}") |
0 commit comments