Skip to content

Commit

Permalink
Metrics fixes (#98)
Browse files Browse the repository at this point in the history
* Add http status code to latency metrics

Also, change some metrics and labels name to make them more standard.

* Add tests for adding http method to latency metrics

* Add pylint exception

It's disabled because the whole point about converting the the two
methods in a class is to use self.
  • Loading branch information
alexppg committed Mar 14, 2020
1 parent 8cb721e commit 248142c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyms/flask/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def init_metrics(self):
self.application.logger,
self.application.config["APP_NAME"]
)
self.metrics.monitor(self.application)
self.metrics.monitor(self.application.config["APP_NAME"], self.application)

def reload_conf(self):
self.delete_services()
Expand Down
30 changes: 17 additions & 13 deletions pyms/flask/services/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@
# and https://github.com/korfuri/python-logging-prometheus/

FLASK_REQUEST_LATENCY = Histogram(
"flask_request_latency_seconds", "Flask Request Latency", ["method", "endpoint"]
"http_server_requests_seconds", "Flask Request Latency", ["service", "method", "uri", "status"]
)
FLASK_REQUEST_COUNT = Counter(
"flask_request_count", "Flask Request Count", ["method", "endpoint", "http_status"]
"http_server_requests_count", "Flask Request Count", ["service", "method", "uri", "status"]
)

LOGGER_TOTAL_MESSAGES = Counter(
"python_logging_messages_total",
"logger_messages_total",
"Count of log entries by service and level.",
["service", "level"],
)


def before_request():
request.start_time = time.time()
class FlaskMetricsWrapper():
def __init__(self, app_name):
self.app_name = app_name

def before_request(self): # pylint: disable=R0201
request.start_time = time.time()

def after_request(response):
request_latency = time.time() - request.start_time
FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency)
FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc()
def after_request(self, response):
request_latency = time.time() - request.start_time
FLASK_REQUEST_LATENCY.labels(self.app_name, request.method, request.path, response.status_code).observe(request_latency)
FLASK_REQUEST_COUNT.labels(self.app_name, request.method, request.path, response.status_code).inc()

return response
return response


class Service(DriverService):
Expand All @@ -46,9 +49,10 @@ def __init__(self, *args, **kwargs):
self.serve_metrics()

@staticmethod
def monitor(app):
app.before_request(before_request)
app.after_request(after_request)
def monitor(app_name, app):
metric = FlaskMetricsWrapper(app_name)
app.before_request(metric.before_request)
app.after_request(metric.after_request)

def serve_metrics(self):
@self.metrics_blueprint.route("/metrics", methods=["GET"])
Expand Down
10 changes: 5 additions & 5 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ def setUp(self):
def test_metrics_latency(self):
self.client.get("/")
self.client.get("/metrics")
generated_latency_root = b'flask_request_latency_seconds_bucket{endpoint="/",le="0.005",method="GET"}'
generated_latency_metrics = b'flask_request_latency_seconds_bucket{endpoint="/metrics",le="0.005",method="GET"}'
generated_latency_root = b'http_server_requests_seconds_bucket{le="0.005",method="GET",service="Python Microservice",status="200",uri="/"}'
generated_latency_metrics = b'http_server_requests_seconds_bucket{le="0.005",method="GET",service="Python Microservice with Jaeger",status="200",uri="/metrics"}'
assert generated_latency_root in generate_latest()
assert generated_latency_metrics in generate_latest()

def test_metrics_count(self):
self.client.get("/")
self.client.get("/metrics")
generated_count_root = b'flask_request_count_total{endpoint="/",http_status="404",method="GET"}'
generated_count_metrics = b'flask_request_count_total{endpoint="/metrics",http_status="200",method="GET"}'
generated_count_root = b'http_server_requests_count_total{method="GET",service="Python Microservice",status="200",uri="/"}'
generated_count_metrics = b'http_server_requests_count_total{method="GET",service="Python Microservice with Jaeger",status="200",uri="/metrics"}'
assert generated_count_root in generate_latest()
assert generated_count_metrics in generate_latest()

def test_metrics_logger(self):
self.client.get("/")
self.client.get("/metrics")
generated_logger = b'python_logging_messages_total{level="DEBUG",service="Python Microservice with Jaeger"}'
generated_logger = b'logger_messages_total{level="DEBUG",service="Python Microservice with Jaeger"}'
assert generated_logger in generate_latest()

def test_metrics_jaeger(self):
Expand Down

0 comments on commit 248142c

Please sign in to comment.