Skip to content

make Prometheus optional - #688

Merged
Bogdanp merged 5 commits into
Bogdanp:masterfrom
azmeuk:345-optional-prometheus
Jun 6, 2025
Merged

Bogdanp merged 5 commits into
Bogdanp:masterfrom
azmeuk:345-optional-prometheus

Conversation

@azmeuk

@azmeuk azmeuk commented Mar 27, 2025

Copy link
Copy Markdown
Contributor

I applied the patch proposed in #345 to make the Prometheus middleware optional:

  • prometheus-client is now an optional dependency
  • The Prometheus middleware is only loaded if the prometheus_client is installed.

fixes #345

azmeuk added 2 commits March 27, 2025 09:49
- prometheus-client is now an optional dependency
- The Prometheus middleware is only loaded if the prometheus_client is
installed.
Comment thread dramatiq/middleware/__init__.py Resolved
Comment thread setup.py


dependencies = [
"prometheus-client>=0.2",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would technically be a breaking change to the build process for people who use Dramatiq, so I'm not sure what to do about it. Is there a way to specify negative dependencies in setup.py? Eg., pip install dramatiq[no-prometheus]. If so, that's the way we should go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that there is a thing such as negative dependencies. I think this is covered by PEP771 but this is still a draft at the moment.

Removing prometheus-client is indeed a breaking change, and I am not sure how this could be otherwise. Usually such changes come with deprecation warnings, but in the case of dependencies, I don't know how to raise a deprecation warning for users that use the middleware but have not explicitly added its extra.

The only remaining option I see is good communication on the changelog.

Comment thread dramatiq/middleware/__init__.py Outdated

try:
if importlib.util.find_spec("prometheus_client"):
default_middleware.insert(0, Prometheus)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion: if we are going to make a breaking change release removing Prometheus from the default middleware, we should also remove this line. In case there is someone who wants to install this library, but doesn't want the middleware.

i.e. the default_middleware list shouldn't change depending on what libraries you have installed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bogdanp, do you have an opinion about this?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @LincolnPuzey 's suggestion makes a lot of sense if we are making a breaking change.

My preference, as always, is not to make breaking changes. But, maybe I am being too dogmatic on this point and it doesn't matter that much in this particular case. I would love it if folks who operate Dramatiq in production would weigh in and let us know what they think.

@azmeuk

azmeuk commented May 5, 2025 •

Copy link
Copy Markdown
Contributor Author

Hi @frankie567 @oYASo @sfahad1414 @SamR1 @mhdzumair

I apologize for the ping. We would like some advice on your usage of dramatiq, and especially if you have an opinion about removing prometheus as a required dependency.

I would love it if folks who operate Dramatiq in production would weigh in and let us know what they think.

I summoned you because your projects are the most popular from the dramatiq dependency graph.

Any thoughts on the matter?

@frankie567

Copy link
Copy Markdown
Contributor

Thank you @azmeuk for asking, really appreciate it!

In my opinion, making Prometheus optional is definitely the right move. Not everyone needs or uses Prometheus metrics, so having it by default is a bit surprising, especially since it opens an HTTP server (quite silently besides that). Of course, it's nice to have it as a built-in middleware we can enable at will 🙂

In our case, we don't really consume the Prometheus metrics: we use it as a quick and dirty health check endpoint. It could be nice to have a middleware exposing a simple health endpoint for this purpose.

@mhdzumair

Copy link
Copy Markdown

You can make it optional.

@sfahad1414

Copy link
Copy Markdown

Yes @azmeuk can be made optional.

@azmeuk

azmeuk commented May 11, 2025

Copy link
Copy Markdown
Contributor Author

Thank you all for your answers!

@Bogdanp do you need more feedback? I can post something on the ML if you want.

@Bogdanp

Bogdanp commented May 12, 2025

Copy link
Copy Markdown
Owner

Yes, let's do a post on the mailing list and if no one objects within a week or so we can go ahead with this. Thanks for your work on this!

@azmeuk

azmeuk commented May 13, 2025

Copy link
Copy Markdown
Contributor Author

I just sent a message on the ML.

@guedesfelipe

Copy link
Copy Markdown
Contributor

Need changes at doc? https://dramatiq.io/advanced.html#prometheus-metrics

@karolinepauls

karolinepauls commented May 27, 2025 •

Copy link
Copy Markdown
Collaborator

I get around some of this problem by specifying all middleware explicitly, as a list - maybe the docs should also move this direction. It won't solve the issue of the prometheus dep being added but at least users following the docs will be able to easily remove its middleware.

@azmeuk

azmeuk commented May 28, 2025 •

Copy link
Copy Markdown
Contributor Author

@guedesfelipe you mean adding a note to indicate that Prometheus must be installed as an extra?

@karolinepauls could it is the subject of another PR?

@guedesfelipe

Copy link
Copy Markdown
Contributor

@guedesfelipe you mean adding a note to indicate that Prometheus must be installed as an extra?

@karolinepauls could it is the subject of another PR?

Something like that.

And add an error when importing the prometheus client if it is not installed correctly?

@KrilleGH

KrilleGH commented Jun 2, 2025

Copy link
Copy Markdown

@azmeuk, how do they feel on the ML?

I'd like to see this merged soon, was really hoping it could land in 1.18.0 already.

@azmeuk

azmeuk commented Jun 3, 2025

Copy link
Copy Markdown
Contributor Author

And add an error when importing the prometheus client if it is not installed correctly?

@guedesfelipe Isn't the natural ImportError enough?

@azmeuk, how do they feel on the ML?

@KrilleGH the ML has been quiet, so no one has raised objections.

@Bogdanp, what do you think?

@KrilleGH

KrilleGH commented Jun 4, 2025

Copy link
Copy Markdown

And add an error when importing the prometheus client if it is not installed correctly?

@guedesfelipe Isn't the natural ImportError enough?

I think what's suggested is how FastAPI does it. When only installing fastapi without the optional standard dependencies coming with fastapi[standard] the fastapi script is still available due to the nature of the packaging tools but doesn't work. It then shows:

eim@83537f9cb06d:/opt/project$ fastapi
To use the fastapi command, please install "fastapi[standard]":

        pip install "fastapi[standard]"

Traceback (most recent call last):
  File "/usr/local/bin/fastapi", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/usr/local/lib/python3.13/site-packages/fastapi/cli.py", line 12, in main
    raise RuntimeError(message)  # noqa: B904
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: To use the fastapi command, please install "fastapi[standard]":

        pip install "fastapi[standard]"

It could be nicer to have a verbose message in this case as well.

@guedesfelipe

Copy link
Copy Markdown
Contributor

And add an error when importing the prometheus client if it is not installed correctly?

@guedesfelipe Isn't the natural ImportError enough?

I think what's suggested is how FastAPI does it. When only installing fastapi without the optional standard dependencies coming with fastapi[standard] the fastapi script is still available due to the nature of the packaging tools but doesn't work. It then shows:

eim@83537f9cb06d:/opt/project$ fastapi
To use the fastapi command, please install "fastapi[standard]":

        pip install "fastapi[standard]"

Traceback (most recent call last):
  File "/usr/local/bin/fastapi", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/usr/local/lib/python3.13/site-packages/fastapi/cli.py", line 12, in main
    raise RuntimeError(message)  # noqa: B904
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: To use the fastapi command, please install "fastapi[standard]":

        pip install "fastapi[standard]"

It could be nicer to have a verbose message in this case as well.

Yeaah! That's it! Explicit is better than implicit

Tnx @KrilleGH

@Bogdanp Bogdanp left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had one last minor comment on this and I think it can be merged.

Isn't the natural ImportError enough?

I agree that it is enough.

Comment thread docs/source/changelog.rst Resolved
@Bogdanp
Bogdanp merged commit 7b7441f into Bogdanp:master Jun 6, 2025
@Bogdanp

Bogdanp commented Jun 6, 2025

Copy link
Copy Markdown
Owner

Thanks @azmeuk for carrying this through!

@azmeuk
azmeuk deleted the 345-optional-prometheus branch June 6, 2025 11:37
@azmeuk

azmeuk commented Jun 6, 2025

Copy link
Copy Markdown
Contributor Author

One detail @Bogdanp, the changelog entry is displayed 1.17.1 instead of 1.18.

@Bogdanp

Bogdanp commented Jun 6, 2025

Copy link
Copy Markdown
Owner

One detail @Bogdanp, the changelog entry is displayed 1.17.1 instead of 1.18.

Thanks for the heads-up. I've pushed a follow-up change to move it under Unreleased.

@LincolnPuzey LincolnPuzey added this to the v2.0.0 milestone Jun 29, 2025
@KrilleGH

Copy link
Copy Markdown

@Bogdanp, can we have a release including this PR?

@karolinepauls

Copy link
Copy Markdown
Collaborator

@KrilleGH
Hi, the maintainers team are going to make a release off the v1 branch. It isn't trivial because we merged both breaking changes and black formatting to master, and so now we need to pick commits from it carefully.

@guedesfelipe

Copy link
Copy Markdown
Contributor

Any version release estimates? @karolinepauls

aequitas pushed a commit to aequitas/dramatiq that referenced this pull request Nov 17, 2025
* chore: make Prometheus optional

- prometheus-client is now an optional dependency
- The Prometheus middleware is only loaded if the prometheus_client is
installed.

* chore: changelog

* fix: insert prometheus middleware at the beginning of the default_middleware list

* feat: remove Prometheus from the default middlewares

* doc: changelog update
@FyZzyss

FyZzyss commented Nov 19, 2025

Copy link
Copy Markdown

I am getting error if I import Prometheus middleware directly with installed prometheus-client. It's a little confusing

@jenstroeger

Copy link
Copy Markdown
Contributor

Question: is the Prometheus middleware missing here

from .age_limit import AgeLimit
from .asyncio import AsyncIO
from .callbacks import Callbacks
from .current_message import CurrentMessage
from .group_callbacks import GroupCallbacks
from .middleware import Middleware, MiddlewareError, SkipMessage
from .pipelines import Pipelines
from .retries import Retries
from .shutdown import Shutdown, ShutdownNotifications
from .threading import Interrupt, raise_thread_exception
from .time_limit import TimeLimit, TimeLimitExceeded
__all__ = [
# Basics
"Middleware",
"MiddlewareError",
"SkipMessage",
# Threading
"Interrupt",
"raise_thread_exception",
# Middlewares
"AgeLimit",
"AsyncIO",
"Callbacks",
"CurrentMessage",
"GroupCallbacks",
"Pipelines",
"Retries",
"Shutdown",
"ShutdownNotifications",
"TimeLimit",
"TimeLimitExceeded",
]
because it’s got some custom module setup that you want to avoid running (unless Prometheus is imported explicitly)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove Prometheus from the list of default middleware