示例#1
0
# https://blog.viktoradam.net/2020/05/11/prometheus-flask-exporter/
# https://github.com/bobbui/json-logging-python
import sys
import logging
import json_logging
import datetime
from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
from time import sleep as delay

app = Flask(__name__)
metrics = PrometheusMetrics(app)

metrics.info('app_info', 'foobar', version='1.0.3', service_name='flask-app')

json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)
logger = logging.getLogger("test-logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/')
def main():
    logger.info("test log statement")
    correlation_id = json_logging.get_correlation_id()
    logger.info("test log statement with extra props",
                extra={'props': {
                    "correlation_id": correlation_id
                }})
    return '<html><p><a href="/wait/4">/wait/4</a><p><a href="/status/500">/status/500</a></html></html>'
示例#2
0
import logging
import sys

import json_logging
import flask


app_name = "app-sample"

app = flask.Flask(app_name)
json_logging.ENABLE_JSON_LOGGING = True
json_logging.ENABLE_JSON_LOGGING_DEBUG = False
json_logging.init_flask()
json_logging.init_request_instrument(app)

# init the logger as usual
logger = logging.getLogger(app_name)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/api/v1/sample', methods=['GET', 'POST'])
def sample():
    content = flask.request.json
    logger.info(f"Received message '{content}'")
    return flask.jsonify(result="success")


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(8000), use_reloader=False)