Skip to content

Commit

Permalink
move dir and fixed 404 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiss committed Oct 28, 2019
1 parent 232110a commit 5fe4a83
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Empty file removed connexion-plus/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions connexion_plus/Factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging

logger = logging.getLogger('')


class Factory():
def __init__(self, app, api, tracer=None, metrics=False):
self.app = app
self.api = api

if tracer is not None:
logger.debug("add tracer")
self.addTracer(tracer)

if metrics:
logger.debug("add prometheus")
self.addPrometheus()

def addTracer(self, tracer):
try:
from .TracerDecorator import TracerDecorator
self.api.get_response = TracerDecorator(self.api.get_response, tracer)
logger.debug("Add Tracing for flask")
except ImportError as e:
print(e)
logger.debug("TracerDecorator not found")


def addPrometheus(self):
try:
from prometheus_flask_exporter import PrometheusMetrics
logger.debug("Add PrometheusMetrics for flask")
return PrometheusMetrics(self.app.app)
except ImportError as e:
logger.debug("PrometheusMetrics not found")


52 changes: 52 additions & 0 deletions connexion_plus/TracerDecorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging

logger = logging.getLogger('')


def TracerDecorator(func, tracer):
def wrapper(*args):
#don't need to check, because this Decorator will only be executed, if the tracer was configured at startup

# this check is needed, because api is called outside from class, so sometimes there is a self-argument, and sometimes it doesnt.
if len(args) == 4:
cls = args[0]
response = args[1]
mimetype = args[2]
request = args[3]
else:
response = args[0]
try:
mimetype = args[1]
except IndexError as e:
mimetype = None
try:
request = args[2]
except IndexError as e:
request = None

from opentracing.ext import tags
from opentracing.propagation import Format

# extract the context from request header to continue a session
# taken from https://github.com/yurishkuro/opentracing-tutorial/tree/master/python/lesson03#extract-the-span-context-from-the-incoming-request-using-tracerextract
if request is not None:
span_ctx = tracer.extract(Format.HTTP_HEADERS, request.headers)
span_tags = {tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER}

# remove domain from url, so only the path is in the span
from urllib.parse import urlparse
path = urlparse(request.url).path

scope = tracer.start_span(request.method + "_" + path, child_of=span_ctx, tags=span_tags)
scope.log_kv({"request": request})
else:
scope = tracer.start_span("NO_REQUEST_CONTEXT")

resp = func(response, mimetype, request)

scope.log_kv({"response": response})
scope.finish()

return resp

return wrapper
1 change: 1 addition & 0 deletions connexion_plus/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .Factory import Factory

0 comments on commit 5fe4a83

Please sign in to comment.