opentracing-contrib/python-django

python3 errors on iteritems()

Closed this issue · 5 comments

With recent changes landed in jaeger-python-client python3 is now almost supported for django_opentracing.

I only had to modify one line (and remove erroneous commas from the Readme settings.py example) to get it to work.

My environment is
GCE kubernetes engine or docker-compose
pip install django_opentracing jaeger-client
jaeger.version=Python-3.7.1
django_opentracing/_version.py
{
"full-revisionid": "21b48cd0998ffdc3f1284341beb2a9606fd8f30e",
"version": "0.1.18"
}

For docker-compose environment I add this snippet to settings.py
import django_opentracing
from jaeger_client import Config
import logging

log_level = logging.DEBUG
logging.getLogger('').handlers = []
logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)

config = Config(
    config={ # usually read from some yaml config
        'sampler': {
            'type': 'const',
            'param': 1,
        },
        'logging': True,
        # docker compose containers see jaeger all-in-one as <parent_folder>_jaeger_1
        'local_agent': {'reporting_host': 'core_jaeger_1'},
    },  
    service_name='web-app',
)

tracer = config.initialize_tracer()

# OpenTracing settings

# if not included, defaults to False
# has to come before OPENTRACING_TRACER setting because python...
OPENTRACING_TRACE_ALL = True

# defaults to []
# only valid if OPENTRACING_TRACE_ALL == True
OPENTRACING_TRACED_ATTRIBUTES = ['arg1', 'arg2',]

# Callable that returns an `opentracing.Tracer` implementation.
OPENTRACING_TRACER_CALLABLE = 'opentracing.Tracer'

# Parameters for the callable (Depending on the tracer implementation chosen)
OPENTRACING_TRACER_PARAMETERS = {
'example-parameter-host': 'collector',
}

OPENTRACING_TRACER = django_opentracing.DjangoTracer(tracer)`

I get the following error:

web-app_1 | File "/opt/conda/lib/python3.6/site-packages/django_opentracing/middleware.py", line 36, in process_view web-app_1 | self._tracer._apply_tracing(request, view_func, traced_attributes) web-app_1 | File "/opt/conda/lib/python3.6/site-packages/django_opentracing/tracer.py", line 67, in _apply_tracing web-app_1 | for k,v in request.META.iteritems(): web-app_1 | AttributeError: 'dict' object has no attribute 'iteritems'
exec into the django (web_app_1) container. Modify "/opt/conda/lib/python3.6/site-packages/django_opentracing/tracer.py", line 67 iteritems() -> items()`
Ctrl + C, docker-compose up to restart
Then I get

web-app_1 | File "/opt/conda/lib/python3.6/site-packages/django_opentracing/tracer.py", line 89, in _apply_tracing web-app_1 | if hasattr(request, attr): web-app_1 | TypeError: hasattr(): attribute name must be string

This is due to following the Readme.md too closely. The readme snippet for django settings.py has trailing commas for several items which causes the hasattr() errors. Remove the commas and everything worked.

I'm guessing my config settings (maybe OPENTRACING_TRACE_ALL = True) caused the iteritems() error to be exposed.

I will submit a pr with the fix

Closing as #25 fixed this. Feel free to re-open if you think this is still an issue ;)

The fixes in #25 were merged into master but #38 was merged into the v1.0.0 branch. So one branch has Python 3 and Django 1.x support. While the v1.0.0 only supports Python 2 and Django 2.x. But since Django 2.x only works on Python 3, there's no branch that supports Django 2

Hey @crccheck

Recently support for Django 2 was added through #38 (although the name of the PR is wrong, as it should read 'Django 2' instead of 'Python 2').

to clarify, #38 was merged into branch v1.0.0, which doesn't support Python 3, a prerequisite for Django 2. So while Django 2 version support was added, it's impossible to run the code because that branch only works on Python 2.

Hey @crccheck

v1.0.0 does support Python 3 - I've also released a small update (django_opentracing==0.1.20) that removes the Django2 limitation. Feel free to try it out ;)

(Will also be merging and releasing the 1.0.0 version, which will introduce breaking changes and support for OT 2.0.0)