Skip to content

Commit 47d42aa

Browse files
authored
flask: using string keys for wsgi environ values (#366)
Some WSGI servers (such as Gunicorn) expect keys in the environ object to be strings.
1 parent 983edc6 commit 47d42aa

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
logger = logging.getLogger(__name__)
1414

15-
_ENVIRON_STARTTIME_KEY = object()
16-
_ENVIRON_SPAN_KEY = object()
17-
_ENVIRON_ACTIVATION_KEY = object()
15+
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
16+
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
17+
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
1818

1919

2020
def instrument_app(flask):

ext/opentelemetry-ext-flask/tests/test_flask_integration.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import unittest
1616

17-
from flask import Flask
17+
from flask import Flask, request
1818
from werkzeug.test import Client
1919
from werkzeug.wrappers import BaseResponse
2020

@@ -57,6 +57,25 @@ def hello_endpoint(helloid):
5757
otel_flask.instrument_app(self.app)
5858
self.client = Client(self.app, BaseResponse)
5959

60+
def test_only_strings_in_environ(self):
61+
"""
62+
Some WSGI servers (such as Gunicorn) expect keys in the environ object
63+
to be strings
64+
65+
OpenTelemetry should adhere to this convention.
66+
"""
67+
nonstring_keys = set()
68+
69+
def assert_environ():
70+
for key in request.environ:
71+
if not isinstance(key, str):
72+
nonstring_keys.add(key)
73+
return "hi"
74+
75+
self.app.route("/assert_environ")(assert_environ)
76+
self.client.get("/assert_environ")
77+
self.assertEqual(nonstring_keys, set())
78+
6079
def test_simple(self):
6180
expected_attrs = expected_attributes(
6281
{

0 commit comments

Comments
 (0)