Skip to content

Commit

Permalink
Measure DB call time
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemmkin committed Oct 30, 2017
1 parent e443f6a commit d8a0316
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion post-py/post_app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from flask import Flask, request
from flask import Flask, request, Response
from pymongo import MongoClient
from bson.objectid import ObjectId
from bson.json_util import dumps
from helpers import health
import os
import prometheus_client
import time

CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
REQUEST_DB_LATENCY = prometheus_client.Histogram('post_read_db_seconds', 'Request DB time')

mongo_host = os.getenv('POST_DATABASE_HOST', '127.0.0.1')
mongo_port = os.getenv('POST_DATABASE_PORT', '27017')
Expand All @@ -15,6 +20,10 @@
app = Flask(__name__)


@app.route('/metrics')
def metrics():
return Response(prometheus_client.generate_latest(), mimetype=CONTENT_TYPE_LATEST)

@app.route("/posts")
def posts():
posts = mongo_db.find().sort('created_at', -1)
Expand Down Expand Up @@ -42,7 +51,11 @@ def add_post():

@app.route("/post/<id>")
def get_post(id):
start_time = time.time()
post = mongo_db.find_one({'_id': ObjectId(id)})
stop_time = time.time() # + 0.3
resp_time = stop_time - start_time
REQUEST_DB_LATENCY.observe(resp_time)
return dumps(post)


Expand Down
3 changes: 3 additions & 0 deletions post-py/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prometheus_client==0.0.21
flask==0.12.2
pymongo==3.5.1

0 comments on commit d8a0316

Please sign in to comment.