Skip to content

Instantly share code, notes, and snippets.

@pglombardo
Created January 17, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pglombardo/6d1d7c920c1cf140968aa6a91fc90652 to your computer and use it in GitHub Desktop.
Save pglombardo/6d1d7c920c1cf140968aa6a91fc90652 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import instana
import tornado.httpserver
import tornado.ioloop
import tornado.web
import opentracing
import opentracing.ext.tags as tags
import logging
instana.service_name = "Tornado 🌪"
class MainHandler(tornado.web.RequestHandler):
def initialize(self):
pass
def get(self):
entry_span = opentracing.tracer.start_span(operation_name="tornade-request")
entry_span.set_tag('RequestHandler', 'MainHandler')
entry_span.set_tag('ClassName', self.__class__.__name__)
entry_span.set_tag(tags.COMPONENT, "Tornado")
entry_span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_SERVER)
entry_span.set_tag(tags.PEER_HOSTNAME, "localhost")
entry_span.set_tag(tags.HTTP_URL, "/tornado")
entry_span.set_tag(tags.HTTP_METHOD, "GET")
self.write("<h1>Welcome to the Python Tracing</h1>")
self.write("<hr></hr>")
welcomeMessage = """Welcome to the Instana Python Sensor and Tracing Example.
The instana package provides Python metrics and traces (request, queue &
cross-host) for Instana. Build Status OpenTracing Badge
Note This package supports Python 2.7 or greater.
Any and all feedback is welcome. Happy Python visibility. See the
<a href="https://github.com/instana/python-sensor">Python Sensor</a>
Installation : pip install instana into the virtual-env or container
(hosted on pypi)
"""
self.write(welcomeMessage)
entry_span.set_tag(tags.HTTP_STATUS_CODE, self.__class__.get_status(self))
entry_span.finish()
def make_app():
return tornado.web.Application([(r"/", MainHandler)])
if __name__ == "__main__":
app = make_app()
portNumber = 8889
app.listen(portNumber)
print ("Starting Tornado 🌪 Server on port " + str(portNumber))
tornado.ioloop.IOLoop.current().start()
@hughbrien
Copy link

Adding the following wrapt.decorator to
class _HandlerDelegate(httputil.HTTPMessageDelegate):def execute(self) method in the Tornado/web.py module
should auto instrument all the RequestHandlers

@wrapt.decorator
def wrapRequestHandler(wrapped, instance, args, kwargs):
try:
print(instance.request)
print(args)
span = opentracing.tracer.start_span(operation_name="tornade-request")
span.set_tag(ext.COMPONENT, "RequestHandler")
span.set_tag(ext.SPAN_KIND, "tornado-request-handler")
span.set_tag(ext.SPAN_KIND, ext.SPAN_KIND_RPC_SERVER)
span.set_tag(ext.PEER_HOSTNAME, instance.request.host)
span.set_tag(ext.HTTP_URL, instance.request.uri )
span.set_tag(ext.HTTP_METHOD, instance.request.method)

        rv = wrapped(*args, **kwargs)
    except Exception as e:
        span.log_kv({'message': e})
        span.set_tag("error", True)
        ec = span.tags.get('ec', 0)
        span.set_tag("ec", ec + 1)
        span.finish()
        raise
    else:
        span.set_tag(ext.HTTP_STATUS_CODE, 200)
        span.finish()
        return rv


@wrapRequestHandler
def execute(self):.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment