Django explicit support
Hello, I was thinking it would be great to add explicit support for Django. Besides updating the documentation, it would be useful to provide a view so it can be added easily in the urlconf of a django app.
django-prometheus does it like this using generate_latest, but from looking at the already provided exposition endpoints, I was thinking of using the _bake_output utility instead.
I'm volunteering to make a PR for it. What do you think?
There could be opportunity for this similar to what is done for twisted. Specifically, only optional dependency on django code and in a separate directory.
That said, is there a problem you are encountering with django-prometheus? Perhaps we just link out to other projects to make it easier to discover how to use Prometheus with various frameworks? Starlette also comes to mind.
Yes I was thinking of following the same code architecture as the twisted support, to avoid any conflict.
To give you my context:
- I am not using django-prometheus currently (so I have no problem with it ^^): django-prometheus gives you lots of already configured metrics out of the box and that's its main use, whereas I was only trying to add one specific custom metrics to my existing django-app so prometheus-client seemed more than enough
- I found it hard to understand how to add a prometheus endpoint to my existing django app with no specific guidance in the prometheus-client documentation
- Hence I ended up looking at how django-prometheus was doing it, and also how it was done for other web-frameworks here (like twisted)
django-prometheus is kinda huge and does a lot of things, so linking it as an example on how to integrate with prometheus-client is a bit involved I think. A simple view calling _bake_output and some dedicated documentation would be very appreciated of those coming from django I think :)
Maybe in time the provided view might even be used by django-prometheus directly instead of having to provide their own.
So, should I make a PR for this? :)
I am still a bit hesitant to add full code for django as that could be confusing with when to use django-prometheus. That said, I do agree that having at least some documentation would be helpful. What if you added an example like https://prometheus.github.io/client_python/exporting/http/flask/, but for django? Especially if it is just going to do something like call generate_latest()
Hi @Chadys what do you think of exporting metric at gunicorn or whatever webserver layer ?
@Agent-Hellboy I'm not sure what you mean exactly, could you elaborate? PS: this issue is still in my todo list, I don't have much time at the moment but I'm planning to get back to it eventually
Hi @Chadys ,
I mean writing an ASGI middleware, as you would be serving Django over Uvicorn (maybe something else) if you're running an asynchronous app. I’ve written something that could be helpful for you. Please take a look. https://github.com/Agent-Hellboy/asgi-prometheus-exporter
If it’s really helpful I will try to test and enhancer it with other frameworks and web servers as this is an independent bridge which assume framework and webserver following a spec.
+---------------------+ +--------------------------+ +-----------------------+
| | | | | |
| Web Server | <-- HTTP -->| Prometheus Middleware | <-- HTTP -->| Application Server |
| (Uvicorn) | | (Metrics Collection) | | (Django) |
| | | | | |
+---------------------+ +--------------------------+ +-----------------------+
| | |
| | |
|------------------- Request ------------------------------->|
Intercepts request
Collects Metrics
Passes request to the application
Ok I understand better what you mean. But I think it's out of scope from the current issue.
@csmarchbanks I made a PR about this :)