From 4e9f07551401e62a1e61f6611dd5daa86c9e6aa1 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Mon, 16 Sep 2019 16:26:37 -0700 Subject: [PATCH] Addressing review feedback 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. --- .../src/opentelemetry/ext/http_requests/__init__.py | 3 +-- .../src/opentelemetry/ext/wsgi/__init__.py | 7 +++++-- opentelemetry-example-app/tests/test_flask_example.py | 6 ++++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py b/ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py index 6be8bc65197..576d13d9221 100644 --- a/ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py +++ b/ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py @@ -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) diff --git a/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py b/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py index 1a8a6e07996..1577ae546c6 100644 --- a/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py +++ b/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py @@ -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 [] diff --git a/opentelemetry-example-app/tests/test_flask_example.py b/opentelemetry-example-app/tests/test_flask_example.py index 82bfa725ff4..d33e2b9ac07 100644 --- a/opentelemetry-example-app/tests/test_flask_example.py +++ b/opentelemetry-example-app/tests/test_flask_example.py @@ -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) + )