From 5d611d5fae61401940e63860f2ad6bf35b3c6d57 Mon Sep 17 00:00:00 2001 From: Julius Unverfehrt Date: Tue, 21 Jun 2022 15:49:01 +0200 Subject: [PATCH] Pull request #17: RED-4329 add prometheus Merge in RR/image-prediction from RED-4329-add-prometheus to master Squashed commit of the following: commit 7fcf256c5277a3cfafcaf76c3116e3643ad01fa4 Merge: 8381621 c14d00c Author: Julius Unverfehrt Date: Tue Jun 21 15:41:14 2022 +0200 Merge branch 'master' into RED-4329-add-prometheus commit 8381621ae08b1a91563c9c655020ec55bb58ecc5 Author: Julius Unverfehrt Date: Tue Jun 21 15:24:50 2022 +0200 add prometheus endpoint commit 26f07088b0a711b6f9db0974f5dfc8aa8ad4e1dc Author: Julius Unverfehrt Date: Tue Jun 21 15:14:34 2022 +0200 refactor commit c563aa505018f8a14931a16a9061d361b5d4c383 Author: Julius Unverfehrt Date: Tue Jun 21 15:10:19 2022 +0200 test bamboo build commit 2b8446e703617c6897b6149846f2548ec292a9a1 Author: Julius Unverfehrt Date: Tue Jun 21 14:40:44 2022 +0200 RED-4329 add prometheus endpoint with summary metric --- image_prediction/flask.py | 10 ++++++++++ requirements.txt | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/image_prediction/flask.py b/image_prediction/flask.py index b1473da..4297a6f 100644 --- a/image_prediction/flask.py +++ b/image_prediction/flask.py @@ -3,6 +3,7 @@ import traceback from typing import Callable from flask import Flask, request, jsonify +from prometheus_client import generate_latest, CollectorRegistry, Summary from image_prediction.utils import get_logger @@ -34,6 +35,10 @@ def wrap_in_process(func_to_wrap): def make_prediction_server(predict_fn: Callable): app = Flask(__name__) + registry = CollectorRegistry(auto_describe=True) + metric = Summary( + f"redactmanager_imageClassification_seconds", f"Time spent on image-service classification.", registry=registry + ) @app.route("/ready", methods=["GET"]) def ready(): @@ -54,6 +59,7 @@ def make_prediction_server(predict_fn: Callable): @app.route("/predict", methods=["POST"]) @app.route("/", methods=["POST"]) + @metric.time() def predict(): # Tensorflow does not free RAM. Workaround: Run prediction function (which instantiates a model) in sub-process. @@ -71,4 +77,8 @@ def make_prediction_server(predict_fn: Callable): logger.error("Analysis failed.") return __failure() + @app.route("/prometheus", methods=["GET"]) + def prometheus(): + return generate_latest(registry=registry) + return app diff --git a/requirements.txt b/requirements.txt index aa6a0f0..da99202 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,4 +21,5 @@ Pillow==9.1.0 PDFNetPython3==9.1.0 pdf2image==1.16.0 frozendict==2.3.0 -protobuf<=3.20.* \ No newline at end of file +protobuf<=3.20.* +prometheus-client==0.13.1