mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
Remove built-in support for uWSGI (#1544)
* Remove check for uwsgi metrics * Removing uwsgi metrics and corresponding test files
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
"""
|
||||
This module defines view function decorator to collect UWSGI metrics and expose them via an
|
||||
endpoint.
|
||||
"""
|
||||
|
||||
import functools
|
||||
import os
|
||||
import time
|
||||
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
from connexion.exceptions import ProblemException
|
||||
|
||||
try:
|
||||
import uwsgi_metrics
|
||||
HAS_UWSGI_METRICS = True # pragma: no cover
|
||||
except ImportError:
|
||||
uwsgi_metrics = None
|
||||
HAS_UWSGI_METRICS = False
|
||||
|
||||
|
||||
class UWSGIMetricsCollector:
|
||||
def __init__(self, path, method):
|
||||
self.path = path
|
||||
self.method = method
|
||||
swagger_path = path.strip('/').replace('/', '.').replace('<', '{').replace('>', '}')
|
||||
self.key_suffix = f'{method.upper()}.{swagger_path}'
|
||||
self.prefix = os.getenv('HTTP_METRICS_PREFIX', 'connexion.response')
|
||||
|
||||
@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):
|
||||
status = 500
|
||||
start_time_s = time.time()
|
||||
try:
|
||||
response = function(*args, **kwargs)
|
||||
status = response.status_code
|
||||
except HTTPException as http_e:
|
||||
status = http_e.code
|
||||
raise http_e
|
||||
except ProblemException as prob_e:
|
||||
status = prob_e.status
|
||||
raise prob_e
|
||||
finally:
|
||||
end_time_s = time.time()
|
||||
delta_s = end_time_s - start_time_s
|
||||
delta_ms = delta_s * 1000
|
||||
key = f'{status}.{self.key_suffix}'
|
||||
uwsgi_metrics.timer(self.prefix, key, delta_ms)
|
||||
return response
|
||||
|
||||
return wrapper
|
||||
@@ -7,7 +7,6 @@ import abc
|
||||
import logging
|
||||
|
||||
from ..decorators.decorator import RequestResponseDecorator
|
||||
from ..decorators.metrics import UWSGIMetricsCollector
|
||||
from ..decorators.parameter import parameter_to_arg
|
||||
from ..decorators.produces import BaseSerializer, Produces
|
||||
from ..decorators.response import ResponseValidator
|
||||
@@ -379,10 +378,6 @@ class AbstractOperation(metaclass=abc.ABCMeta):
|
||||
|
||||
function = self._request_response_decorator(function)
|
||||
|
||||
if UWSGIMetricsCollector.is_available(): # pragma: no cover
|
||||
decorator = UWSGIMetricsCollector(self.path, self.method)
|
||||
function = decorator(function)
|
||||
|
||||
return function
|
||||
|
||||
@property
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import flask
|
||||
import pytest
|
||||
from connexion.decorators.metrics import UWSGIMetricsCollector
|
||||
from connexion.exceptions import ProblemException
|
||||
|
||||
|
||||
def test_timer(monkeypatch):
|
||||
wrapper = UWSGIMetricsCollector('/foo/bar/<param>', 'get')
|
||||
|
||||
def operation(req):
|
||||
raise ProblemException(418, '', '')
|
||||
|
||||
op = wrapper(operation)
|
||||
metrics = MagicMock()
|
||||
monkeypatch.setattr('flask.request', MagicMock())
|
||||
monkeypatch.setattr('flask.current_app', MagicMock(response_class=flask.Response))
|
||||
monkeypatch.setattr('connexion.decorators.metrics.uwsgi_metrics', metrics)
|
||||
with pytest.raises(ProblemException) as exc:
|
||||
op(MagicMock())
|
||||
assert metrics.timer.call_args[0][:2] == ('connexion.response',
|
||||
'418.GET.foo.bar.{param}')
|
||||
Reference in New Issue
Block a user