Skip to content

Commit f306438

Browse files
samuelcolvinocelotl
authored andcommitted
add more tests
1 parent 2e31d87 commit f306438

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def _custom_response_headers():
8888
resp.headers["my-secret-header"] = "my-secret-value"
8989
return resp
9090

91+
@staticmethod
92+
def _repeat_custom_response_headers():
93+
headers = {
94+
"content-type": "text/plain; charset=utf-8",
95+
"my-custom-header": [
96+
"my-custom-value-1", "my-custom-header-2"
97+
],
98+
}
99+
return flask.Response("test response", headers=headers)
100+
91101
def _common_initialization(self):
92102
def excluded_endpoint():
93103
return "excluded"
@@ -106,6 +116,9 @@ def excluded2_endpoint():
106116
self.app.route("/test_custom_response_headers")(
107117
self._custom_response_headers
108118
)
119+
self.app.route("/test_repeat_custom_response_headers")(
120+
self._repeat_custom_response_headers
121+
)
109122

110123
# pylint: disable=attribute-defined-outside-init
111124
self.client = Client(self.app, Response)

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

+31
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,22 @@ def test_custom_request_header_added_in_server_span(self):
671671
self.assertEqual(span.kind, trace.SpanKind.SERVER)
672672
self.assertSpanHasAttributes(span, expected)
673673

674+
def test_repeat_custom_request_header_added_in_server_span(self):
675+
headers = [
676+
("Custom-Test-Header-1", "Test Value 1"),
677+
("Custom-Test-Header-1", "Test Value 2"),
678+
]
679+
resp = self.client.get("/hello/123", headers=headers)
680+
self.assertEqual(200, resp.status_code)
681+
span = self.memory_exporter.get_finished_spans()[0]
682+
expected = {
683+
"http.request.header.custom_test_header_1": (
684+
"Test Value 1, Test Value 2",
685+
),
686+
}
687+
self.assertEqual(span.kind, trace.SpanKind.SERVER)
688+
self.assertSpanHasAttributes(span, expected)
689+
674690
def test_custom_request_header_not_added_in_internal_span(self):
675691
tracer = trace.get_tracer(__name__)
676692
with tracer.start_as_current_span("test", kind=trace.SpanKind.SERVER):
@@ -724,6 +740,21 @@ def test_custom_response_header_added_in_server_span(self):
724740
self.assertEqual(span.kind, trace.SpanKind.SERVER)
725741
self.assertSpanHasAttributes(span, expected)
726742

743+
def test_repeat_custom_response_header_added_in_server_span(self):
744+
resp = self.client.get("/test_repeat_custom_response_headers")
745+
self.assertEqual(resp.status_code, 200)
746+
span = self.memory_exporter.get_finished_spans()[0]
747+
expected = {
748+
"http.response.header.content_type": (
749+
"text/plain; charset=utf-8",
750+
),
751+
"http.response.header.my_custom_header": (
752+
"my-custom-value-1,my-custom-header-2",
753+
),
754+
}
755+
self.assertEqual(span.kind, trace.SpanKind.SERVER)
756+
self.assertSpanHasAttributes(span, expected)
757+
727758
def test_custom_response_header_not_added_in_internal_span(self):
728759
tracer = trace.get_tracer(__name__)
729760
with tracer.start_as_current_span("test", kind=trace.SpanKind.SERVER):

instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py

+32
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ def wsgi_with_custom_response_headers(environ, start_response):
115115
return [b"*"]
116116

117117

118+
def wsgi_with_repeat_custom_response_headers(environ, start_response):
119+
assert isinstance(environ, dict)
120+
start_response(
121+
"200 OK",
122+
[
123+
("my-custom-header", "my-custom-value-1"),
124+
("my-custom-header", "my-custom-value-2"),
125+
],
126+
)
127+
return [b"*"]
128+
129+
118130
_expected_metric_names = [
119131
"http.server.active_requests",
120132
"http.server.duration",
@@ -711,6 +723,26 @@ def test_custom_response_headers_not_added_in_internal_span(self):
711723
for key, _ in not_expected.items():
712724
self.assertNotIn(key, span.attributes)
713725

726+
@mock.patch.dict(
727+
"os.environ",
728+
{
729+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "my-custom-header",
730+
},
731+
)
732+
def test_custom_response_headers_added_in_server_span(self):
733+
app = otel_wsgi.OpenTelemetryMiddleware(
734+
wsgi_with_repeat_custom_response_headers
735+
)
736+
response = app(self.environ, self.start_response)
737+
self.iterate_response(response)
738+
span = self.memory_exporter.get_finished_spans()[0]
739+
expected = {
740+
"http.response.header.my_custom_header": (
741+
"my-custom-value-1,my-custom-value-2",
742+
),
743+
}
744+
self.assertSpanHasAttributes(span, expected)
745+
714746

715747
if __name__ == "__main__":
716748
unittest.main()

0 commit comments

Comments
 (0)