Skip to content

Commit

Permalink
Merge pull request #64 from zalando/metrics
Browse files Browse the repository at this point in the history
Add UWSGI metrics collector
  • Loading branch information
hjacobs committed Sep 18, 2015
2 parents a0110c2 + 8f920bf commit c4eaadf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 33 additions & 0 deletions connexion/decorators/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import functools

try:
import uwsgi_metrics
HAS_UWSGI_METRICS = True
except:
HAS_UWSGI_METRICS = False


class UWSGIMetricsCollector:
def __init__(self, path, method):
self.path = path
self.method = method
self.key = '{}.{}'.format(self.path.strip('/').replace('/', '.').replace('<', '{').replace('>', '}'),
self.method.upper())

@staticmethod
def is_available():
return HAS_UWSGI_METRICS

def __call__(self, function):
"""
:type function: types.FunctionType
:rtype: types.FunctionType
"""

@functools.wraps(function)
def wrapper(*args, **kwargs):
with uwsgi_metrics.timing('connexion.response', self.key):
return function(*args, **kwargs)

return wrapper
8 changes: 7 additions & 1 deletion connexion/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

import functools
import logging
import os

from .decorators.produces import BaseSerializer, Produces, Jsonifier
from .decorators.security import security_passthrough, verify_oauth
from .decorators.validation import RequestBodyValidator, ParameterValidator
from .decorators.metrics import UWSGIMetricsCollector
from .exceptions import InvalidSpecification
from .utils import flaskify_endpoint, get_function_from_name, produces_json

Expand Down Expand Up @@ -151,6 +153,10 @@ def function(self):
logger.debug('... Adding security decorator (%r)', security_decorator, extra=vars(self))
function = security_decorator(function)

if UWSGIMetricsCollector.is_available():
decorator = UWSGIMetricsCollector(self.path, self.method)
function = decorator(function)

return function

@property
Expand Down Expand Up @@ -227,7 +233,7 @@ def __security_decorator(self):
scheme_name, scopes = next(iter(security.items())) # type: str, list
security_definition = self.security_definitions[scheme_name]
if security_definition['type'] == 'oauth2':
token_info_url = security_definition.get('x-tokenInfoUrl')
token_info_url = security_definition.get('x-tokenInfoUrl', os.getenv('HTTP_TOKENINFO_URL'))
if token_info_url:
scopes = set(scopes) # convert scopes to set because this is needed for verify_oauth
return functools.partial(verify_oauth, token_info_url, scopes)
Expand Down

0 comments on commit c4eaadf

Please sign in to comment.