8000 supporting custom multiprocess metric path by iostream96 · Pull Request #1094 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

supporting custom multiprocess metric path #1094

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

iostream96
Copy link

This pr is about supporting a custom multiprocess metric path, not changing current usage about env PROMETHEUS_MULTIPROC_DIR.

I found that MultiProcessCollector supports a path param https://github.com/prometheus/client_python/blob/master/prometheus_client/multiprocess.py#L22, though metrics don't support it. According to the discussion here #395 (comment) and some more, path is fixed to PROMETHEUS_MULTIPROC_DIR and MultiProcessCollector path param don't actually function afaics. So this pr keeps PROMETHEUS_MULTIPROC_DIR usage, adds support for custom path if multiprocess_dir is specified by metric.

Now MultiProcessCollector path works fine and allows us to serve different metrics exporters for scrapers to scrape in multiprocess mode. These changes have been applied in our production environment for months and look good.

@csmarchbanks ptal :) happy to know if any suggestions.

@iostream96 iostream96 marked this pull request as ready for review February 26, 2025 03:56
Copy link
Member
@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

I'm not opposed to this feature, as you say, it seems like it could be helpful for cases where you want to have multiple /metrics endpoints in one server?

That said, how are you dealing with value class? Just setting the env var to a default then using these as overrides? Something else?

def getMultiprocDir() -> str:
if 'prometheus_multiproc_dir' in os.environ and 'PROMETHEUS_MULTIPROC_DIR' not in os.environ:
os.environ['PROMETHEUS_MULTIPROC_DIR'] = os.environ['prometheus_multiproc_dir']
warnings.warn("prometheus_multiproc_dir variable has been deprecated in favor of the upper case naming PROMETHEUS_MULTIPROC_DIR", DeprecationWarning)
Copy link
Member

Choose a reason for hiding this comment

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

While you're in here could you just remove this deprecation warning? I think we should just allow either casing permanently, it's not a big deal especially with a helper function.

Copy link
Author

Choose a reason for hiding this comment

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

Sure, i'll work on it.




def getMultiprocDir() -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def getMultiprocDir() -> str:
def _getMultiprocDir() -> str:

Just want to make sure this is explicitly private to avoid anyone depending on it...

@iostream96
Copy link
Author

I'm not opposed to this feature, as you say, it seems like it could be helpful for cases where you want to have multiple /metrics endpoints in one server?

That said, how are you dealing with value class? Just setting the env var to a default then using these as overrides? Something else?

Yes it's used like an override. My case is our PaaS platform runs some platform metrics, and we want to provide prom metrics usage for app devs. So it will be better to expose 2 different endpoints in one server to separate platform and app metrics. In practice, PROMETHEUS_MULTIPROC_DIR is set to enable multiprocess mode and is used for platform metrics, and there is an api for app devs like

def new_app_counter(name, documentation, labelnames=None):
    namespace = os.environ.get('APPNAME', '')
    multiprocess_dir = os.environ.get('PROMETHEUS_MULTIPROC_DIR_APP')
    return Counter(
        name=name,
        documentation=documentation,
        namespace=namespace,
        labelnames=labelnames if labelnames else (),
        multiprocess_dir=multiprocess_dir
    )

It sets multiprocess_dir to PROMETHEUS_MULTIPROC_DIR_APP for app metrics.

Signed-off-by: iostream96 <jianing0412@hotmail.com>
Signed-off-by: iostream96 <jianing0412@hotmail.com>
Signed-off-by: iostream96 <jianing0412@hotmail.com>
Signed-off-by: iostream96 <jianing0412@hotmail.com>
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.

2 participants
0