Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask: using string keys for wsgi environ values #366

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

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

logger = logging.getLogger(__name__)

_ENVIRON_STARTTIME_KEY = object()
_ENVIRON_SPAN_KEY = object()
_ENVIRON_ACTIVATION_KEY = object()
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime"

I think we don't need to add _key to the key.

_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"


def instrument_app(flask):
Expand Down
28 changes: 24 additions & 4 deletions ext/opentelemetry-ext-flask/tests/test_flask_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprised to see these imports order needing to change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorting through it, something to do with how isort resolves first party vs third-party imports.

Copy link
Member

@Oberon00 Oberon00 Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try deleting your virtualenv. I had troubles with isort too and it seems that if there is a site-packages/openetelemetry directory (even an empty one), it will think of opentelemetry as a 3rd party. On the other hand, if you install it editable, there is only a opentelemetry.egg-link file, which isort doesn't find, so it falls back to thinking of opentelemetry as a first-party.
EDIT: Or actually, this should not matter since we have opentelemetry as a "known 1st party" in

known_first_party=opentelemetry
. Strange that it still doesn't work for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've tracked this down. Effectively modules that are not in the virtualenv are considered first party packages by isort. I'm working on crafting a ticket to fire at isort to clarify this behavior.

For now I'm unblocked by install flask into my virtualenv. On that note, should we be calling out the module that the integration is meant for in the setup.py? I mistakenly thought that flask would be installed when I installed the extension.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: PyCQA/isort#1104



def expected_attributes(override_attributes):
Expand Down Expand Up @@ -57,6 +56,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(
{
Expand Down