Skip to content

Commit

Permalink
Merge pull request #11 from instana/better_faster_stronger_flask
Browse files Browse the repository at this point in the history
Better, faster, stronger Flask initialization
  • Loading branch information
pglombardo authored Aug 11, 2017
2 parents 805265b + 3f4007c commit 28f8fa0
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions instana/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext
import os


DJ_INSTANA_MIDDLEWARE = 'instana.django.InstanaMiddleware'
Expand Down Expand Up @@ -38,6 +39,11 @@ def __call__(self, request):

def hook(module):
""" Hook method to install the Instana middleware into Django """
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running django hook")
print("==============================================================")

if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE:
return

Expand Down
20 changes: 12 additions & 8 deletions instana/flaskana.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import print_function
from instana import wsgi
from flask.cli import ScriptInfo
import wrapt
import os


def wrap_load_app(func):
def wrapper(self, *args):
app = func(self, *args)
app.wsgi_app = wsgi.iWSGIMiddleware(app.wsgi_app)
return app
return wrapper
def wrapper(wrapped, instance, args, kwargs):
rv = wrapped(*args, **kwargs)
instance.wsgi_app = wsgi.iWSGIMiddleware(instance.wsgi_app)
return rv


def hook(module):
""" Hook method to install the Instana middleware into Flask """
ScriptInfo.load_app = wrap_load_app(ScriptInfo.load_app)
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running flask hook")
print("==============================================================")
wrapt.wrap_function_wrapper('flask', 'Flask.__init__', wrapper)
2 changes: 1 addition & 1 deletion instana/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def to_dict(self):

class Meter(object):
SNAPSHOT_PERIOD = 600
snapshot_countdown = 25
snapshot_countdown = 35
sensor = None
last_usage = None
last_collect = None
Expand Down
4 changes: 4 additions & 0 deletions instana/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def hook(module):
""" Hook method to install the Instana middleware into Flask """
if os.environ["AUTOWRAPT_BOOTSTRAP"] == "runtime":
if "INSTANA_DEV" in os.environ:
print("==========================================================")
print("Instana: Running runtime hook")
print("==========================================================")
level = logging.DEBUG
else:
level = logging.WARN

opts = options.Options(log_level=level)
ot.global_tracer = tracer.InstanaTracer(opts)
3 changes: 0 additions & 3 deletions instana/span.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
HTTP_CLIENT = "g.hc"
HTTP_SERVER = "g.http"

class InstanaSpan(object):
t = 0
p = None
Expand Down
2 changes: 1 addition & 1 deletion instana/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def id_to_header(id):
return ""

byteString = struct.pack('>q', id)
return binascii.hexlify(byteString).decode('UTF-8').lstrip('0')
return str(binascii.hexlify(byteString).decode('UTF-8').lstrip('0'))


def header_to_id(header):
Expand Down
1 change: 0 additions & 1 deletion instana/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'psutil>=5.1.3'],
entry_points={'django': ['django.core.handlers.base = instana.django:hook'],
'django19': ['django.core.handlers.base = instana.django19:hook'],
'flask': ['flask.cli = instana.flaskana:hook'],
'flask': ['flask = instana.flaskana:hook'],
'runtime': ['string = instana.runtime:hook']},
test_suite='nose.collector',
keywords=['performance', 'opentracing', 'metrics', 'monitoring'],
Expand Down

0 comments on commit 28f8fa0

Please sign in to comment.