-
Notifications
You must be signed in to change notification settings - Fork 609
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
Merged
lzchen
merged 25 commits into
open-telemetry:main
from
ashu658:capture-custom-header-wsgi
Mar 7, 2022
Merged
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
10fd32f
Capture custom request/response headers for wsgi
ashu658 12e5951
Adding unit tests and refactoring
ashu658 bd75167
Formatting changes for fixing lint errors
ashu658 d706f6f
-Adding changelog
ashu658 059b9be
Fixing lint errors
ashu658 63ec660
Formatting correction
ashu658 e18dc94
Passing response headers in correct format in pyramid
ashu658 0541ecc
Resolving review comment
ashu658 88249d2
Adding type hints
ashu658 93fa969
Fixing build errors
ashu658 191f1a7
resolving review comments:
ashu658 9bd0864
Fixing unit tests
ashu658 5878aa0
Fixing lint errors
ashu658 aedc4e2
Update CHANGELOG.md
ashu658 26695bd
Resolving review comments: Improving readability
ashu658 c1011a1
Changing env variable for consistentcy with java sdk
ashu658 f367e06
Fixing lint failure
ashu658 9b87b2e
Merge branch 'main' into capture-custom-header-wsgi
srikanthccv f95859d
Merge branch 'main' into capture-custom-header-wsgi
lzchen df33267
Adding custom header only if span is SERVER span
ashu658 bd24673
Fixing lint errors
ashu658 d85650e
Removing unused variables
ashu658 98ad58b
Addressing comments
ashu658 f9e40a3
Fixing pytests
ashu658 c4a5998
Merge branch 'main' into capture-custom-header-wsgi
lzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
util/opentelemetry-util-http/tests/test_capture_custom_headers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).