-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using start_http_server
results into undefined behaviour
#4
Comments
Hi! Thanks for trying this library! :) The reason for exposing the metrics endpoint with Flask is that, I thought, we already have an HTTP server to use, why start a different one. Thanks! |
Hi @sbrandtb , I've done some changes to try detect if the app is running in debug mode. There is one caveat though, the actual metrics endpoint won't see the changes that are loaded via the reloader. Let me know if this makes things better for you or not! Thanks! |
Hi, thanks for responding so quickly!
That's true if you choose the same port. But if you choose a different port, like we and AFAIK lots of others do because it makes it easier to not expose metrics to the outside world, then it makes not that much sense anymore. How I solved it: def init_prometheus(port):
log.info('Starting prometheus export on port %s', port)
start_http_server(port)
app = your_global_instance
def init_app(...):
# run all kind of initialization to the app depending on parameters to main
def main(...):
...
if not flag_that_tells_if_will_run_in_reload or is_running_from_reloader():
init_prometheus(prometheus_port)
init_app(...)
app.run(...) The important point here is that if we are running with reload, the first invocation of So, if we would create the server/socket for prometheus in the first invocation, the next invocation would cause an IOError because the port is used already. However, if we would create the prometheus only for the parent process and not subprocesses, it would never show the metrics of the subprocesses. I haven't tested your changes, but I think it would suffer from the same issue, wouldn't it? While one could argue that having metrics in development is not required, it's still confusing and makes it harder to test, doesn't it? |
Thanks for looking into this some more! I get that it might be confusing to have the metrics endpoint not reflecting updates in debug mode, but I think for the time being, I'm going to just add this as a note to the readme. Thanks again for this very valuable input, and for mentioning this library in your project. :) |
+1 for having metrics in development via configurable parameter just in case you reconsider in future. |
I'd accept PRs if you'd have time to investigate a solution for it? :) |
@skbly7 I've added some very basic support for metrics in |
When starting a Flask app in debug mode (
werkzeug
server) on port, let's say, 5000 and usingPrometheusMetrics.start_http_server
on another port, let's say 9000, Prometheus endpoint is served properly on port 9000, but also on port 5000, resulting in responses that come randomly from the original or the prometheus Flask app.I'm not sure if that's a limitation of
werkzeug
- however, when running the original Flask app with another WSGI container, this behaviour disappears.So, why not just use
start_http_server
ofprometheus_client
under the hood? Is there a particular reason to serve metrics via Flask?The text was updated successfully, but these errors were encountered: