Skip to content

Commit

Permalink
Addressing review feedback
Browse files Browse the repository at this point in the history
Fixing incorrect type annotation for WSGI getter for
HTTPTextFormatter.

Using setdefautl to enable more atomic and simple
setting of headers.

Using UnitTest style assertions vs PyTest.
  • Loading branch information
toumorokoshi committed Sep 16, 2019
1 parent 384496c commit 4e9f075
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ def instrumented_request(self, method, url, *args, **kwargs):
# TODO: Propagate the trace context via headers once we have a way
# to access propagators.

headers = kwargs.get("headers", {})
headers = kwargs.setdefault("headers", {})
propagator.get_global_propagator().inject(
tracer, type(headers).__setitem__, headers
)
kwargs["headers"] = headers
result = wrapped(self, method, url, *args, **kwargs) # *** PROCEED

span.set_attribute("http.status_code", result.status_code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ def __call__(self, environ, start_response):

def get_header_from_environ(
environ: dict, header_name: str
) -> typing.Optional[str]:
) -> typing.List[str]:
"""Retrieve the header value from the wsgi environ dictionary.
Returns:
A string with the header value if it exists, else None.
"""
environ_key = "HTTP_" + header_name.upper().replace("-", "_")
return [environ.get(environ_key)]
value = environ.get(environ_key)
if value:
return [value]
return []
6 changes: 4 additions & 2 deletions opentelemetry-example-app/tests/test_flask_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ def test_full_path(self):
prepared_request = self.send.call_args[0][1]
headers = prepared_request.headers
for required_header in {"x-b3-traceid", "x-b3-spanid", "x-b3-sampled"}:
assert required_header in headers
assert headers["x-b3-traceid"] == b3_format.format_trace_id(trace_id)
self.assertIn(required_header, headers)
self.assertEqual(
headers["x-b3-traceid"], b3_format.format_trace_id(trace_id)
)

0 comments on commit 4e9f075

Please sign in to comment.