<reponame>geoffbernard/python-stack-docker-client-example
import time

import redis
from flask import Flask

from elasticapm.contrib.flask import ElasticAPM

app = Flask(__name__)

app.config['ELASTIC_APM'] ={
    'APP_NAME': 'hithere',
    'SERVER_URL': 'http://apm:8200',
}


apm = ElasticAPM(app, logging=True)

cache = redis.Redis(host='redis', port=6379)


def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)


@app.route('/')
def hello():
    
    apm.capture_message('hello, world, again!')

    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
