Skip to content

Commit

Permalink
Merge pull request #98 from MJedr/prometheus-exporter
Browse files Browse the repository at this point in the history
app: add prometheus exporter
  • Loading branch information
MJedr authored May 19, 2022
2 parents 22f4d70 + a4763a4 commit be03683
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ RUN apt update && apt install poppler-utils -y
COPY setup.py setup.cfg README.rst ./
COPY refextract refextract/
RUN python setup.py install
ENV PROMETHEUS_MULTIPROC_DIR='/tmp'
ENTRYPOINT exec gunicorn -b :5000 --access-logfile - --error-logfile - refextract.app:app --timeout 650
34 changes: 26 additions & 8 deletions refextract/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging

from flask import Flask, jsonify, make_response
from prometheus_flask_exporter.multiprocess import \
GunicornInternalPrometheusMetrics
from webargs import fields
from webargs.flaskparser import FlaskParser

from refextract.references.api import (extract_journal_reference,
extract_references_from_url,
extract_references_from_string)
extract_references_from_string,
extract_references_from_url)

parser = FlaskParser()

Expand Down Expand Up @@ -36,7 +40,14 @@ def extract_journal_info(args):
extracted_publication_info = {}
extracted_publication_infos.append(extracted_publication_info)
except Exception as e:
return make_response(jsonify({"message": f"Can not extract publication info data. Reason: {str(e)}"}), 500)
return make_response(
jsonify(
{
"message": f"Can not extract publication info data. Reason: {str(e)}"
}
),
500,
)
return jsonify({"extracted_publication_infos": extracted_publication_infos})

@app.route("/extract_references_from_text", methods=["POST"])
Expand All @@ -55,11 +66,12 @@ def extract_references_from_text(args):
extracted_references = extract_references_from_string(
text,
override_kbs_files=journal_dict,
reference_format=u"{title},{volume},{page}",
reference_format="{title},{volume},{page}",
)
except Exception as e:
return make_response(
jsonify({"message": f"Can not extract references. Reason: {str(e)}"}), 500
jsonify({"message": f"Can not extract references. Reason: {str(e)}"}),
500,
)
return jsonify({"extracted_references": extracted_references})

Expand All @@ -80,12 +92,13 @@ def extract_references_from_file_url(args):
url,
**{
"override_kbs_files": journal_dict,
"reference_format": "{title},{volume},{page}"
}
"reference_format": "{title},{volume},{page}",
},
)
except Exception as e:
return make_response(
jsonify({"message": f"Can not extract references. Reason: {str(e)}"}), 500
jsonify({"message": f"Can not extract references. Reason: {str(e)}"}),
500,
)
return jsonify({"extracted_references": extracted_references})

Expand All @@ -94,5 +107,10 @@ def extract_references_from_file_url(args):

app = create_app()

if app.config.get('PROMETHEUS_ENABLE_EXPORTER_FLASK'):
logging.info("Starting prometheus metrics exporter")
metrics = GunicornInternalPrometheusMetrics.for_app_factory(prefix=app.name)
metrics.init_app(app)

if __name__ == "__main__":
app.run(host="0.0.0.0")
1 change: 1 addition & 0 deletions refextract/config.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FILES_DOWNLOAD_MAX_RETRIES = 3
FILES_DOWNLOAD_TIMEOUT = 60
PROMETHEUS_ENABLE_EXPORTER_FLASK = False
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
'inspire-utils~=3.0,>=3.0.25',
'Flask>=2.0.3',
"webargs<=5.4.0",
"gunicorn>=20.1.0"
"gunicorn>=20.1.0",
"prometheus-flask-exporter~=0.20,>=0.20.1"
]

docs_require = [
Expand Down

0 comments on commit be03683

Please sign in to comment.