Skip to content

Allow customizing the suffix function in multiprocess mode (instead of os.getpid()) #509

Closed

Description

@xavfernandez

One frequent issue with using prometheus_client in multiprocess mode is that you can end up with a lot of files which can severely impact the collecting performance ending with scraping timeouts in the worth cases.

This comes from the fact that to prevent concurrent usage, prometheus_client uses os.getpid() to name its metrics file and ensure that each process only touches its own files: it's a perfectly good and simple design.
However for long running processes spawning new processes every few minutes you can end up with several thousands files after a few months.

But in some cases, you have access to other methods to ensure that each process is uniquely identified (and ensure that each process only touches uniquely identified metrics files).

Would it be possible to allow customizing (& document as public API) the function generating metrics file suffix ?
(Of course, this would still be an use-at-your-own-risk escape hatch).

I'm speaking of:

def MultiProcessValue(_pidFunc=os.getpid):

For instance, using uWSGI, we might want to use uwsgi.worker_id():

from prometheus_client import values
import uwsgi

values.ValueClass = values.MultiProcessValue(uwsgi.worker_id)

Or when each worker has its own task and you can ensure that only one worker at a time is dealing with this task (thanks to a postgres advisory lock in my case).

def prometheus_pid_func():
    return os.getenv("PROMETHEUS_WORKER_TASK_ID", str(os.getpid()))


from prometheus_client import values
values.ValueClass = values.MultiProcessValue(prometheus_pid_func)

Activity

  1. brian-brazil commented on Feb 10, 2020

    @brian-brazil
    Contributor

    The problem is that that isn't safe if the process then later forks, it took a while to get the current logic right and I'm not sure how correctly it'll work with anything that's not actually a pid.

  2. xavfernandez commented on Feb 10, 2020

    @xavfernandez
    ContributorAuthor

    The problem is that that isn't safe if the process then later forks, it took a while to get the current logic right and I'm not sure how correctly it'll work with anything that's not actually a pid.

    Yes, hence the "use-at-your-own-risk escape hatch".
    This would be clearly documented as a "I know what I'm doing" hook but I think that in most cases the user has a fairly good understanding whether its code will spawn a subprocess or not (typically web server workers don't).

  3. brian-brazil commented on Feb 11, 2020

    @brian-brazil
    Contributor

    I don't think this is something that should go in the readme, we already have issues with users failing to follow the docs that are there and this would be handing them a chainsaw to juggle. I am open to code changes that'd make this easier to do for those who know what they're up to though.

  4. brian-brazil commented on Jan 26, 2021

    @brian-brazil
    Contributor

    I believe this can be closed now.

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