Skip to content

Django explicit support #1088

Closed
Closed

Description

@Chadys

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?

Activity

  1. csmarchbanks commented on Jan 28, 2025

    @csmarchbanks
    Member

    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.

  2. Chadys commented on Jan 28, 2025

    @Chadys
    ContributorAuthor

    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.

  3. Chadys commented on Feb 27, 2025

    @Chadys
    ContributorAuthor

    So, should I make a PR for this? :)

  4. csmarchbanks commented on Feb 28, 2025

    @csmarchbanks
    Member

    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()

  5. Agent-Hellboy commented on May 18, 2025

    @Agent-Hellboy

    Hi @Chadys what do you think of exporting metric at gunicorn or whatever webserver layer ?

  6. Chadys commented on May 18, 2025

    @Chadys
    ContributorAuthor

    @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

  7. Agent-Hellboy commented on May 20, 2025

    @Agent-Hellboy

    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
    
  8. Chadys commented on May 21, 2025

    @Chadys
    ContributorAuthor

    Ok I understand better what you mean. But I think it's out of scope from the current issue.

  9. Chadys commented on Nov 11, 2025

    @Chadys
    ContributorAuthor

    @csmarchbanks I made a PR about this :)

  10. csmarchbanks commented on Nov 20, 2025

    @csmarchbanks
    Member

    Thanks! It's on my list to review soon!

  11. added a commit that references this issue on Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions