Python flask.g.start() Examples

The following are 4 code examples of flask.g.start(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.g , or try the search function .
Example #1
Source File: __init__.py    From notifications-api with MIT License 5 votes vote down vote up
def init_app(app):

    @app.before_request
    def record_request_details():
        CONCURRENT_REQUESTS.inc()

        g.start = monotonic()
        g.endpoint = request.endpoint

    @app.after_request
    def after_request(response):
        CONCURRENT_REQUESTS.dec()

        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
        return response

    @app.errorhandler(Exception)
    def exception(error):
        app.logger.exception(error)
        # error.code is set for our exception types.
        msg = getattr(error, 'message', str(error))
        code = getattr(error, 'code', 500)
        return jsonify(result='error', message=msg), code

    @app.errorhandler(WerkzeugHTTPException)
    def werkzeug_exception(e):
        return make_response(
            jsonify(result='error', message=e.description),
            e.code,
            e.get_headers()
        )

    @app.errorhandler(404)
    def page_not_found(e):
        msg = e.description or "Not found"
        return jsonify(result='error', message=msg), 404 
Example #2
Source File: api.py    From grin-pool with Apache License 2.0 5 votes vote down vote up
def pre_request():
    global database
    database.db.initializeSession()
    g.start = time.time() 
Example #3
Source File: api.py    From grin-pool with Apache License 2.0 5 votes vote down vote up
def after_request(response):
    global LOGGER
    timediff = time.time() - g.start
    debug and LOGGER.warn("Exec time: {}".format(str(timediff)))
    return response 
Example #4
Source File: metrics.py    From fastlane with MIT License 4 votes vote down vote up
def init_app(app):
    def start_timer():
        request_id = request.headers.get("X-Request-ID", str(uuid4()))
        g.logger = current_app.logger.bind(request_id=request_id)
        g.request_id = request_id
        g.start = datetime.now()

    app.before_request(start_timer)

    def log_request(response):
        if request.path == "/favicon.ico":
            return response

        now = datetime.now()

        if hasattr(g, "start"):
            duration = int(round((now - g.start).microseconds / 1000, 2))
        else:
            duration = -1

        user_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
        host = request.host.split(":", 1)[0]

        log_params = {
            "method": request.method,
            "path": request.path,
            "status": response.status_code,
            "duration": duration,
            "ip": user_ip,
            "host": host,
        }

        request_id = getattr(g, "request_id", None)

        if request_id:
            log_params["request_id"] = request_id

        if response.status_code < 400:
            current_app.logger.info("Request succeeded", **log_params)
        elif response.status_code < 500:
            current_app.logger.info("Bad Request", **log_params)
        else:
            current_app.logger.error("Internal Server Error", **log_params)

        return response

    app.after_request(log_request)