Exemple #1
0
def create_app():
    app = Flask(__name__)
    setup_metrics(app)

    json_logging.ENABLE_JSON_LOGGING = True
    json_logging.init(framework_name='flask')
    json_logging.init_request_instrument(app)
    logger = logging.getLogger("app-logger")
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler(sys.stdout))

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    api = Api(app)

    @app.route('/ping')
    def ping():
        logger.info("pinged", extra={'tags': ['role:web', 'env:prod']})
        return jsonify(ping='pong')

    @app.route("/healthz", methods=["GET"])
    def healthz():
        logger.info("health-checked", extra={'tags': ['role:web', 'env:prod']})
        return jsonify({"status": "SUCCESS"})

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

    api.add_resource(TaskList, "/tasks")
    api.add_resource(Task, "/tasks/<int:id>")

    def initialize_tracer():
        config = Config(config={
            'sampler': {
                'type': 'const',
                'param': 1
            },
            'local_agent': {
                'reporting_host': "cicdnode-0.vdigital.io",
                'reporting_port': 6831
            }
        },
                        service_name='task-service')
        return config.initialize_tracer()  # also sets opentracing.tracer

    flask_tracer = FlaskTracer(initialize_tracer, True, app)

    return app
    def load_classifier(self):
        pickle_path = "./models/model.pickle"
        with open(pickle_path, 'rb') as handle:
            clf = pickle.load(handle)
            features = pickle.load(handle)

        return clf, features

    def __init__(self, *args, **kwargs):
        super(IrisService, self).__init__(*args, **kwargs)
        # load sketchy classifier in memory
        self.classifier, self.features = self.load_classifier()


iris_service = IrisService(__name__)
setup_metrics(iris_service)
#iris_service.config.from_object(BaseConfig)
iris_service.config['SQLALCHEMY_DATABASE_URI'] = \
    'postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}'.format(
        user='******',
        passwd='postgres',
        host='postgres',
        port=5432,
        db='postgres')
iris_service.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
iris_service.secret_key = 'foobarbaz'
db = SQLAlchemy(iris_service)


class Classifications(db.Model):
Exemple #3
0
from flask import Flask, Response
from helpers.middleware import setup_metrics
import prometheus_client

CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')


app = Flask(__name__)
setup_metrics(app)

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

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

@app.errorhandler(500)
def handle_500(error):
    return str(error) + "\n", 500

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)