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

Any examples with django as middleware? #48

Open
SEJeff opened this issue Apr 18, 2018 · 5 comments
Open

Any examples with django as middleware? #48

SEJeff opened this issue Apr 18, 2018 · 5 comments

Comments

@SEJeff
Copy link
Contributor

SEJeff commented Apr 18, 2018

For a few of my users, they'd like to use this library (for it's depth with db libs, redis, and requests / urllib) with the opentracing python-django middleware.

Would the right approach be to fork the python_django middleware, or write one for this library from scratch? Also, would it even make sense to move this library to the opentracing-contrib github org?

@yurishkuro
Copy link
Contributor

I am sure it's possible to make this library work with Django, but currently it does not include specific modules for server frameworks. We internally use it with Flask. I think it would be great to including reusable middleware for both Flask and Django though, as long as they are conditional, i.e. do not require runtime dependencies on either.

One gotcha is with the upcoming opentracing-python 2.0 - see #49.

@SEJeff
Copy link
Contributor Author

SEJeff commented Apr 19, 2018

Is the example in the README how you use it with flask/clay as a middleware?

What I meant previously was more about what it would take to use this instrumentation library with this django opentracing library. It was very easy to get an example application working with this library + jaeger_client + opentracing-python, but this lib is just more indepth.

@yurishkuro
Copy link
Contributor

Yes, that example is similar to how we use it internally.

@opobla
Copy link

opobla commented Mar 30, 2019

Starting from the given flask/clay middleware example, I have set up this instrumentation library with Django + Django Rest Framework, by means of this wsgi.py file:

import logging
import os
import opentracing
from django.core.wsgi import get_wsgi_application
from jaeger_client import Config
from opentracing_instrumentation.client_hooks import install_all_patches
from opentracing_instrumentation.http_server import WSGIRequestWrapper
from opentracing_instrumentation.http_server import before_request
from opentracing_instrumentation import request_context

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sampletrace.settings')


def init_jaeger_tracer(service_name='your-app-name'):
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(message)s', level=logging.DEBUG)
    config = Config(config={
        'enabled': True,
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'logging': True
    }, service_name=service_name, validate=True)
    return config.initialize_tracer()


def create_wsgi_tracing_middleware(other_wsgi):

    def wsgi_tracing_middleware(environ, start_response):

        request = WSGIRequestWrapper.from_wsgi_environ(environ)
        span = before_request(request=request, tracer=opentracing.tracer)
        span.set_operation_name("WSGI {}".format(request.operation))

        # Wrapper around the real start_response object to log
        # additional information to opentracing Span
        def start_response_wrapper(status, response_headers,
                                   exc_info=None):
            if exc_info is not None:
                span.log(event='exception', payload=exc_info)
            span.finish()

            return start_response(status, response_headers)

        with request_context.span_in_context(span):
            install_all_patches()
            return other_wsgi(environ, start_response_wrapper)

    return wsgi_tracing_middleware


application = create_wsgi_tracing_middleware(get_wsgi_application())

I wonder if there is a better way to implement the middleware and to install_all_patches, but it seem to work.

Now I would like to instrument views, so that a new span is created automatically when the view's class method is called. Any tips on how to achieve this?

@danihodovic
Copy link

https://github.com/opentracing-contrib/python-django

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants