rycus86 / prometheus_flask_exporter

Prometheus exporter for Flask applications
https://pypi.python.org/pypi/prometheus-flask-exporter
MIT License
642 stars 162 forks source link

ImportError: cannot import name 'MethodViewType' from 'flask.views' #139

Closed amroghoneim closed 2 years ago

amroghoneim commented 2 years ago

I have a flask endpoint with Gunicorn as the server. I containerized the app and deployed on an azure container instance.

It has been working fine but then this error started popping up when starting the container instance.

complete error traceback

from prometheus flask exporter.multiprocess import Gunicorn PrometheusMetrics
 File "/usr/local/lib/python3.7/site-packages/prometheus_flask_exporter/__init__.py", line 12, in <module> from flask.views import MethodViewType 
ImportError: cannot import name 'MethodViewType' from 'flask.views' (/usr/local/lib/python3.7/site-packages/flask/views.py) 2022-08-02T10:19:32.1978187Z
 stderr F

The code looks like this

config.py

import os

from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics

def when_ready(server):
    GunicornPrometheusMetrics.start_http_server_when_ready(80)

def child_exit(server, worker):
    GunicornPrometheusMetrics.mark_process_dead_on_child_exit(worker.pid)

app.py

from flask import Flask, Response, jsonify, request
from .errors import errors
from .middleware import *
from PIL import Image
import time
import io
import base64
import logging
import json
from logging.handlers import RotatingFileHandler
# from concurrent_log_handler  import ConcurrentRotatingFileHandler
from azure_storage_logging.handlers import BlobStorageRotatingFileHandler
from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics
import os
import warnings
warnings.filterwarnings("ignore")

logging.basicConfig(
        handlers=[
                  # RotatingFileHandler('./ocr_log.log', maxBytes=100000),
                  BlobStorageRotatingFileHandler(filename=os.environ['filename'], maxBytes=100000,
                                                 container=os.environ['container_name'],
                                                 account_name=os.environ['storage_account_name'],
                                                 account_key=os.environ['storage_account_key']),
                  # ConcurrentRotatingFileHandler(filename='ocr_log_concurrent.log')
        ],
        level=logging.INFO,
        format=u"[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
        datefmt='%Y-%m-%dT%H:%M:%S')
logging.captureWarnings(True)
logging.propagate = True

app = Flask(__name__)
app.register_blueprint(errors)

metrics = GunicornPrometheusMetrics(app)
metrics.info("app_info", "App Info, this can be anything you want", version="1.0.0")

gunicorn command

gunicorn wsgi:app --bind 0.0.0.0:443 --conf config.py --log-level=INFO --access-logfile 'gunicorn_accesslogs.log' --access-logformat '%(h)s %(l)s "%(q)s" %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(L)s' --reuse-port --worker-tmp-dir /dev/shm --workers=6-k gevent_pywsgi --reload --timeout 0 --graceful-timeout 10
rycus86 commented 2 years ago

Thanks for reporting! This has just been fixed in the latest version, see https://github.com/rycus86/prometheus_flask_exporter/issues/138