Skip to content

Conversation

Peopl3s
Copy link

@Peopl3s Peopl3s commented Jun 9, 2025

Description

This PR updates the documentation to include detailed instructions for configuring Prometheus metrics collection when running FastStream in multi-process mode. The new section describes the required environment variable (PROMETHEUS_MULTIPROC_DIR), how to initialize the Prometheus collector registry with multiprocess support, and provides a code example for integrating the configuration with FastStream and Kafka.

Close #2220

Type of change

  • Documentation (typos, code examples, or any documentation updates)

Checklist

  • My code adheres to the style guidelines of this project (scripts/lint.sh shows no errors)
  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • Both new and existing unit tests pass successfully on my local environment by running scripts/test-cov.sh
  • I have ensured that static analysis tests are passing by running scripts/static-analysis.sh
  • I have included code examples to illustrate the modifications

Lancetnik
Lancetnik previously approved these changes Jun 9, 2025
@Lancetnik Lancetnik enabled auto-merge June 9, 2025 16:00
@Lancetnik Lancetnik disabled auto-merge June 9, 2025 16:05
@draincoder
Copy link
Collaborator

draincoder commented Jun 11, 2025

Hi, your example in the documentation is not valid. All metrics will automatically pick up the multiprocessing mode if the PROMETHEUS_MULTIPROC_DIR environment variable is specified, manipulations with the MultiProcessCollector are needed only when exporting. Check out the second section on this page https://prometheus.github.io/client_python/multiprocess/.

Here is an example of a function that works correctly in multiprocessing mode. Try to simplify it for documentation.

import os
import time

from prometheus_client import CollectorRegistry, multiprocess, generate_latest, CONTENT_TYPE_LATEST
from prometheus_client.asgi import make_asgi_app

from faststream.asgi import AsgiFastStream, get, AsgiResponse
from faststream.rabbit.annotations import RabbitBroker
from faststream.rabbit.prometheus import RabbitPrometheusMiddleware


registry = CollectorRegistry()


@get
async def metrics(scope):
    if path := os.environ.get("PROMETHEUS_MULTIPROC_DIR"):
        registry_ = CollectorRegistry()
        multiprocess.MultiProcessCollector(registry_, path=path)
    else:
        registry_ = registry

    headers = {"Content-Type": CONTENT_TYPE_LATEST}
    return AsgiResponse(generate_latest(registry_), status_code=200, headers=headers)


mid = RabbitPrometheusMiddleware(registry=registry)
broker = RabbitBroker(middlewares=[mid], max_consumers=10)
app = AsgiFastStream(broker, asgi_routes=[("/metrics", metrics)])


@broker.subscriber("test")
async def handler(msg: str) -> None:
    time.sleep(0.1)
    print(msg, os.getpid())

To test the behavior, you need to launch the application via uvicorn, specify PROMETHEUS_MULTIPROC_DIR, and send messages to the test queue. When requesting /metrics, any process should respond the same way.

What else needs to be specified in the documentation:

  • The metrics folder should be emptied between runs
  • The metrics folder can be mounted on tmpfs for faster operation

You also need to write a test that will check the correctness of the work. Let me know if you can't get it tested.
More information about the operation of the multiprocessing mode can be found in this article https://habr.com/ru/companies/domclick/articles/773136/. Also, all the necessary information is in the documentation https://prometheus.github.io/client_python/multiprocess/.

@Peopl3s
Copy link
Author

Peopl3s commented Jun 11, 2025

@draincoder Thx, I'll try to write the tests, and I'll let you know if I do/don't.

@Lancetnik
Copy link
Member

@Peopl3s @draincoder hi! Have we any updates here?

@Peopl3s
Copy link
Author

Peopl3s commented Jun 22, 2025

I've corrected the documentation, but I haven't gotten around to writing tests yet

@draincoder
Copy link
Collaborator

@Peopl3s Hi, are there any updates regarding the tests?

@Peopl3s
Copy link
Author

Peopl3s commented Jul 10, 2025

@draincoder Hi. I'm sorry that I'm updating the status late. I've corrected the documentation, but I won't be able to take the tests in July. If someone else joins in, it would be great.

# Conflicts:
#	docs/docs/en/getting-started/observability/prometheus.md
@github-actions github-actions bot added documentation Improvements or additions to documentation Observability labels Aug 11, 2025
@Peopl3s Peopl3s requested a review from kumaranvpl as a code owner October 3, 2025 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Observability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature:Support MultiProcess Collector for PrometheusMiddleWare

4 participants