docs: add Prometheus metrics configuration for multi-process mode#2275
docs: add Prometheus metrics configuration for multi-process mode#2275Peopl3s wants to merge 17 commits intoag2ai:mainfrom
Conversation
|
Hi, your example in the documentation is not valid. All metrics will automatically pick up the multiprocessing mode if the 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 What else needs to be specified in the documentation:
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. |
|
@draincoder Thx, I'll try to write the tests, and I'll let you know if I do/don't. |
|
@Peopl3s @draincoder hi! Have we any updates here? |
|
I've corrected the documentation, but I haven't gotten around to writing tests yet |
|
@Peopl3s Hi, are there any updates regarding the tests? |
|
@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
Description
This PR updates the documentation to include detailed instructions for configuring Prometheus metrics collection when running
FastStream inmulti-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
Checklist
scripts/lint.shshows no errors)scripts/test-cov.shscripts/static-analysis.sh