Skip to content

Commit

Permalink
flask app prometheus update
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsaha committed Apr 29, 2017
1 parent 6e7fc87 commit 19a4960
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -87,3 +87,6 @@ ENV/

# Rope project settings
.ropeproject

# Misc
.DS_Store
4 changes: 2 additions & 2 deletions flask_app_promethus/Dockerfile.py2
Expand Up @@ -7,8 +7,8 @@ RUN set -e; \
libc-dev \
linux-headers \
; \
pip install -r requirements.txt; \
pip install -r src/requirements.txt; \
apk del .build-deps;
EXPOSE 5000
RUN python --version
VOLUME /application
CMD uwsgi --http :5000 --manage-script-name --mount /myapplication=flask_app:app --enable-threads --processes 5
4 changes: 2 additions & 2 deletions flask_app_promethus/Dockerfile.py3
Expand Up @@ -7,8 +7,8 @@ RUN set -e; \
libc-dev \
linux-headers \
; \
pip install -r requirements.txt; \
pip install -r src/requirements.txt; \
apk del .build-deps;
EXPOSE 5000
RUN python --version
VOLUME /application
CMD uwsgi --http :5000 --manage-script-name --mount /myapplication=flask_app:app --enable-threads --processes 5
14 changes: 5 additions & 9 deletions flask_app_promethus/README.md
@@ -1,3 +1,7 @@
# Example Flask application

See ``src`` for the application code.

## Building Docker image

Python 3:
Expand All @@ -6,18 +10,10 @@ Python 3:
$ docker build -t amitsaha/flask_app -f Dockerfile.py3 .
```

Python 2:

```
$ docker build -t amitsaha/flask_app -f Dockerfile.py2 .
```

## Running the application

```
$ docker run -P amitsaha/flask_app
$ docker ps # will show the mapped port on the host
$ docker run -ti -p 5000:5000 -v `pwd`/src:/application amitsaha/flask_app
```

## Bringing up the web application, along with prometheus
Expand Down
30 changes: 0 additions & 30 deletions flask_app_promethus/flask_app.py

This file was deleted.

21 changes: 21 additions & 0 deletions flask_app_promethus/src/flask_app.py
@@ -0,0 +1,21 @@
from flask import Flask
from middleware import setup_metrics

app = Flask(__name__)
setup_metrics(app)

@app.route('/test/')
def test():
return 'rest'

@app.route('/test1/')
def test1():
1/0
return 'rest'

@app.errorhandler(500)
def handle_500(error):
return str(error), 500

if __name__ == '__main__':
app.run()
23 changes: 23 additions & 0 deletions flask_app_promethus/src/middleware.py
@@ -0,0 +1,23 @@
from flask import request
import time
import sys

def start_timer():
request.start_time = time.time()

def stop_timer(response):
resp_time = time.time() - request.start_time
sys.stderr.write("Response time: %ss\n" % resp_time)
return response

def record_request_data(response):
sys.stderr.write("Request path: %s Request method: %s Response status: %s\n" %
(request.path, request.method, response.status_code))
return response

def setup_metrics(app):
app.before_request(start_timer)
# The order here matters since we want stop_timer
# to be executed first
app.after_request(record_request_data)
app.after_request(stop_timer)
File renamed without changes.

0 comments on commit 19a4960

Please sign in to comment.