-
Notifications
You must be signed in to change notification settings - Fork 626
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
Capture custom request/response headers for wsgi and change in passing response_headers in django, pyramid #925
Changes from 24 commits
10fd32f
12e5951
bd75167
d706f6f
059b9be
63ec660
e18dc94
0541ecc
88249d2
93fa969
191f1a7
9bd0864
5878aa0
aedc4e2
26695bd
c1011a1
f367e06
9b87b2e
f95859d
df33267
bd24673
d85650e
98ad58b
f9e40a3
c4a5998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,10 @@ | |
from opentelemetry.test.test_base import TestBase | ||
from opentelemetry.test.wsgitestutil import WsgiTestBase | ||
from opentelemetry.trace import StatusCode | ||
from opentelemetry.util.http import ( | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST, | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE, | ||
) | ||
|
||
|
||
class Response: | ||
|
@@ -82,6 +86,19 @@ def error_wsgi_unhandled(environ, start_response): | |
raise ValueError | ||
|
||
|
||
def wsgi_with_custom_response_headers(environ, start_response): | ||
assert isinstance(environ, dict) | ||
start_response( | ||
"200 OK", | ||
[ | ||
("content-type", "text/plain; charset=utf-8"), | ||
("content-length", "100"), | ||
("my-custom-header", "my-custom-value-1,my-custom-header-2"), | ||
], | ||
) | ||
return [b"*"] | ||
|
||
|
||
class TestWsgiApplication(WsgiTestBase): | ||
def validate_response( | ||
self, | ||
|
@@ -444,5 +461,119 @@ def test_mark_span_internal_in_presence_of_span_from_other_framework(self): | |
) | ||
|
||
|
||
class TestAdditionOfCustomRequestResponseHeaders(WsgiTestBase, TestBase): | ||
def setUp(self): | ||
super().setUp() | ||
tracer_provider, _ = TestBase.create_tracer_provider() | ||
self.tracer = tracer_provider.get_tracer(__name__) | ||
|
||
def iterate_response(self, response): | ||
while True: | ||
try: | ||
value = next(response) | ||
self.assertEqual(value, b"*") | ||
except StopIteration: | ||
break | ||
|
||
@mock.patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3" | ||
}, | ||
) | ||
def test_custom_request_headers_added_in_server_span(self): | ||
self.environ.update( | ||
{ | ||
"HTTP_CUSTOM_TEST_HEADER_1": "Test Value 1", | ||
"HTTP_CUSTOM_TEST_HEADER_2": "TestValue2,TestValue3", | ||
} | ||
) | ||
app = otel_wsgi.OpenTelemetryMiddleware(simple_wsgi) | ||
response = app(self.environ, self.start_response) | ||
self.iterate_response(response) | ||
span = self.memory_exporter.get_finished_spans()[0] | ||
expected = { | ||
"http.request.header.custom_test_header_1": ("Test Value 1",), | ||
"http.request.header.custom_test_header_2": ( | ||
"TestValue2,TestValue3", | ||
), | ||
} | ||
self.assertSpanHasAttributes(span, expected) | ||
|
||
@mock.patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1" | ||
}, | ||
) | ||
def test_custom_request_headers_not_added_in_internal_span(self): | ||
self.environ.update( | ||
{ | ||
"HTTP_CUSTOM_TEST_HEADER_1": "Test Value 1", | ||
} | ||
) | ||
|
||
with self.tracer.start_as_current_span( | ||
"test", kind=trace_api.SpanKind.SERVER | ||
): | ||
app = otel_wsgi.OpenTelemetryMiddleware(simple_wsgi) | ||
response = app(self.environ, self.start_response) | ||
self.iterate_response(response) | ||
span = self.memory_exporter.get_finished_spans()[0] | ||
not_expected = { | ||
"http.request.header.custom_test_header_1": ("Test Value 1",), | ||
} | ||
for key, _ in not_expected.items(): | ||
self.assertNotIn(key, span.attributes) | ||
|
||
@mock.patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "content-type,content-length,my-custom-header,invalid-header" | ||
}, | ||
) | ||
def test_custom_response_headers_added_in_server_span(self): | ||
app = otel_wsgi.OpenTelemetryMiddleware( | ||
wsgi_with_custom_response_headers | ||
) | ||
response = app(self.environ, self.start_response) | ||
self.iterate_response(response) | ||
span = self.memory_exporter.get_finished_spans()[0] | ||
expected = { | ||
"http.response.header.content_type": ( | ||
"text/plain; charset=utf-8", | ||
), | ||
"http.response.header.content_length": ("100",), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, why is everything a sequence? it doesn't make sense that content_length is list, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is part of the spec actually.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but that is for the multiple header values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its for single values also. There is an example in the spec.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's strange but not a blocker for me. |
||
"http.response.header.my_custom_header": ( | ||
"my-custom-value-1,my-custom-header-2", | ||
), | ||
} | ||
self.assertSpanHasAttributes(span, expected) | ||
|
||
@mock.patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "my-custom-header" | ||
}, | ||
) | ||
def test_custom_response_headers_not_added_in_internal_span(self): | ||
with self.tracer.start_as_current_span( | ||
"test", kind=trace_api.SpanKind.INTERNAL | ||
): | ||
app = otel_wsgi.OpenTelemetryMiddleware( | ||
wsgi_with_custom_response_headers | ||
) | ||
response = app(self.environ, self.start_response) | ||
self.iterate_response(response) | ||
span = self.memory_exporter.get_finished_spans()[0] | ||
not_expected = { | ||
"http.response.header.my_custom_header": ( | ||
"my-custom-value-1,my-custom-header-2", | ||
), | ||
} | ||
for key, _ in not_expected.items(): | ||
self.assertNotIn(key, span.attributes) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from unittest.mock import patch | ||
|
||
from opentelemetry.test.test_base import TestBase | ||
from opentelemetry.util.http import ( | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST, | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE, | ||
get_custom_headers, | ||
normalise_request_header_name, | ||
normalise_response_header_name, | ||
) | ||
|
||
|
||
class TestCaptureCustomHeaders(TestBase): | ||
@patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "User-Agent,Test-Header" | ||
}, | ||
) | ||
def test_get_custom_request_header(self): | ||
custom_headers_to_capture = get_custom_headers( | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST | ||
) | ||
self.assertEqual( | ||
custom_headers_to_capture, ["User-Agent", "Test-Header"] | ||
) | ||
|
||
@patch.dict( | ||
"os.environ", | ||
{ | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "content-type,content-length,test-header" | ||
}, | ||
) | ||
def test_get_custom_response_header(self): | ||
custom_headers_to_capture = get_custom_headers( | ||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE | ||
) | ||
self.assertEqual( | ||
custom_headers_to_capture, | ||
[ | ||
"content-type", | ||
"content-length", | ||
"test-header", | ||
], | ||
) | ||
|
||
def test_normalise_request_header_name(self): | ||
key = normalise_request_header_name("Test-Header") | ||
self.assertEqual(key, "http.request.header.test_header") | ||
|
||
def test_normalise_response_header_name(self): | ||
key = normalise_response_header_name("Test-Header") | ||
self.assertEqual(key, "http.response.header.test_header") |
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.
Is this safe for all supported version of django?
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.
I guess you are referring to
response.items()
in.../instrumentation/django/middleware.py
Yes, I have tested it with lowest supported version of django by running the unit tests (as docs for django dont' say anything about this property below 4.0) and also done same for pyramid.
Do we run tests for different versions of a framework in the build jobs?
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.
I was planning to add tests for all wsgi based frameworks in different PR.
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.
I was referring to replacing
headers
withheaderlist
. Isheaderlist
attribute available on all supported versions of django?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.
I think there is some confusion, the change
header
toheaderlist
is for pyramid :)Yes, I checked pyramid docs,
headerlist
attribute is available in all versions of pyramid we support (>=1.7).