From c3c8a1d2ecbcf9a412e914294962a84504327ab7 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 14 Jan 2020 14:41:22 -0800 Subject: [PATCH 1/8] flask: using string keys for wsgi environ values Some WSGI servers (such as Gunicorn) expect keys in the environ object to be strings. --- .../src/opentelemetry/ext/flask/__init__.py | 6 +++--- .../tests/test_flask_integration.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index ce11b18d635..4bf0cc8c681 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -12,9 +12,9 @@ logger = logging.getLogger(__name__) -_ENVIRON_STARTTIME_KEY = object() -_ENVIRON_SPAN_KEY = object() -_ENVIRON_ACTIVATION_KEY = object() +_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key" +_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key" +_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key" def instrument_app(flask): diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 09e62b7ba19..f320a92da04 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -57,6 +57,27 @@ def hello_endpoint(helloid): otel_flask.instrument_app(self.app) self.client = Client(self.app, BaseResponse) + def test_only_strings_in_environ(self): + """ + Some WSGI servers (such as Gunicorn) expect keys in the environ object + to be strings + + OpenTelemetry should adhere to this convention. + """ + nonstring_keys = set() + + def assert_environ(): + from flask import request + + for k in request.environ: + if not isinstance(k, str): + nonstring_keys.add(k) + return "hi" + + self.app.route("/assert_environ")(assert_environ) + self.client.get("/assert_environ") + self.assertEqual(nonstring_keys, set()) + def test_simple(self): expected_attrs = expected_attributes( { From d5b2b2fe39692ecd8a633e14f3060a9fb4926d2f Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 14 Jan 2020 20:01:55 -0800 Subject: [PATCH 2/8] fixing lint errors --- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index f320a92da04..1a44e04c805 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -76,7 +76,7 @@ def assert_environ(): self.app.route("/assert_environ")(assert_environ) self.client.get("/assert_environ") - self.assertEqual(nonstring_keys, set()) + self.assertEqual(nonstring_keys, set()) def test_simple(self): expected_attrs = expected_attributes( From 72e20cf998455439cc911e4e372457e784e2eb26 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 14 Jan 2020 20:12:00 -0800 Subject: [PATCH 3/8] addressing lint errors. --- .../src/opentelemetry/ext/flask/__init__.py | 3 +-- .../tests/test_flask_integration.py | 9 ++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index 4bf0cc8c681..ae9a355c99e 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -3,9 +3,8 @@ import logging -from flask import request as flask_request - import opentelemetry.ext.wsgi as otel_wsgi +from flask import request as flask_request from opentelemetry import propagators, trace from opentelemetry.ext.flask.version import __version__ from opentelemetry.util import time_ns diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 1a44e04c805..b64db794f1a 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -14,13 +14,12 @@ import unittest -from flask import Flask -from werkzeug.test import Client -from werkzeug.wrappers import BaseResponse - import opentelemetry.ext.flask as otel_flask +from flask import Flask from opentelemetry import trace as trace_api from opentelemetry.ext.testutil.wsgitestutil import WsgiTestBase +from werkzeug.test import Client +from werkzeug.wrappers import BaseResponse def expected_attributes(override_attributes): @@ -59,7 +58,7 @@ def hello_endpoint(helloid): def test_only_strings_in_environ(self): """ - Some WSGI servers (such as Gunicorn) expect keys in the environ object + Some WSGI servers (such as Gunicorn) expect keys in the environ object to be strings OpenTelemetry should adhere to this convention. From 4a0ec11798c81396e2507abce3c7fbadbd05dcbe Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 16 Jan 2020 21:15:21 -0800 Subject: [PATCH 4/8] addressing lint errors. --- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index b64db794f1a..2778d42983c 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -58,7 +58,7 @@ def hello_endpoint(helloid): def test_only_strings_in_environ(self): """ - Some WSGI servers (such as Gunicorn) expect keys in the environ object + Some WSGI servers (such as Gunicorn) expect keys in the environ object to be strings OpenTelemetry should adhere to this convention. @@ -75,7 +75,7 @@ def assert_environ(): self.app.route("/assert_environ")(assert_environ) self.client.get("/assert_environ") - self.assertEqual(nonstring_keys, set()) + self.assertEqual(nonstring_keys, set()) def test_simple(self): expected_attrs = expected_attributes( From 48539827a851cf428452349ef2ef93a67ca59ee9 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 16 Jan 2020 21:21:58 -0800 Subject: [PATCH 5/8] addressing lint errors --- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 2778d42983c..1e0a020e2ac 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -75,7 +75,7 @@ def assert_environ(): self.app.route("/assert_environ")(assert_environ) self.client.get("/assert_environ") - self.assertEqual(nonstring_keys, set()) + self.assertEqual(nonstring_keys, set()) def test_simple(self): expected_attrs = expected_attributes( From fb1311c617585c8ad73f09c3f9b0c2b10aec12ec Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 16 Jan 2020 21:48:18 -0800 Subject: [PATCH 6/8] fixing lint errors --- .../src/opentelemetry/ext/flask/__init__.py | 3 ++- .../tests/test_flask_integration.py | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index ae9a355c99e..4bf0cc8c681 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -3,8 +3,9 @@ import logging -import opentelemetry.ext.wsgi as otel_wsgi from flask import request as flask_request + +import opentelemetry.ext.wsgi as otel_wsgi from opentelemetry import propagators, trace from opentelemetry.ext.flask.version import __version__ from opentelemetry.util import time_ns diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 1e0a020e2ac..1a44e04c805 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -14,13 +14,14 @@ import unittest -import opentelemetry.ext.flask as otel_flask from flask import Flask -from opentelemetry import trace as trace_api -from opentelemetry.ext.testutil.wsgitestutil import WsgiTestBase from werkzeug.test import Client from werkzeug.wrappers import BaseResponse +import opentelemetry.ext.flask as otel_flask +from opentelemetry import trace as trace_api +from opentelemetry.ext.testutil.wsgitestutil import WsgiTestBase + def expected_attributes(override_attributes): default_attributes = { From 3979f40e4bf3739cf7b73fe6c55a3102e02073cd Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Mon, 20 Jan 2020 09:53:50 -0800 Subject: [PATCH 7/8] addressing lint errors --- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 1a44e04c805..dcb5e37ed19 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -59,7 +59,7 @@ def hello_endpoint(helloid): def test_only_strings_in_environ(self): """ - Some WSGI servers (such as Gunicorn) expect keys in the environ object + Some WSGI servers (such as Gunicorn) expect keys in the environ object to be strings OpenTelemetry should adhere to this convention. From 72872b4cf4baf597ddd2b9ecc9daf233b6de2bb5 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Mon, 20 Jan 2020 10:06:27 -0800 Subject: [PATCH 8/8] addressing linting --- .../tests/test_flask_integration.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index dcb5e37ed19..9d2f2560118 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -14,7 +14,7 @@ import unittest -from flask import Flask +from flask import Flask, request from werkzeug.test import Client from werkzeug.wrappers import BaseResponse @@ -67,11 +67,9 @@ def test_only_strings_in_environ(self): nonstring_keys = set() def assert_environ(): - from flask import request - - for k in request.environ: - if not isinstance(k, str): - nonstring_keys.add(k) + for key in request.environ: + if not isinstance(key, str): + nonstring_keys.add(key) return "hi" self.app.route("/assert_environ")(assert_environ)