Created
January 17, 2018 16:08
-
-
Save pglombardo/6d1d7c920c1cf140968aa6a91fc90652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)