-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
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") | ||
|
||
|
This file contains 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
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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .Factory import Factory |