Skip to content

Commit

Permalink
flask: using string keys for wsgi environ values (#366)
Browse files Browse the repository at this point in the history
Some WSGI servers (such as Gunicorn) expect keys in the environ object
to be strings.
  • Loading branch information
toumorokoshi committed Jan 20, 2020
1 parent 983edc6 commit 47d42aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 20 additions & 1 deletion ext/opentelemetry-ext-flask/tests/test_flask_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -57,6 +57,25 @@ 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():
for key in request.environ:
if not isinstance(key, str):
nonstring_keys.add(key)
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(
{
Expand Down

0 comments on commit 47d42aa

Please sign in to comment.