Fortunately, I mange to solve it.

```
from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import make_wsgi_app
from prometheus_client.core import REGISTRY

app = Flask(__name__)

def health(environ, start_response):
    headers = [("content-type", "application/json")]
    status = "200 OK"

    output = json.dumps({"data": {"status": "running"})
    output_encoded = output.encode("utf-8")

    start_response(status, headers)
    return [output_encoded]


app.wsgi_app = DispatcherMiddleware(
    app.wsgi_app, {"/metrics": make_wsgi_app(), "/health": util.health}
)
```


On Thursday, February 25, 2021 at 1:39:32 PM UTC+7 Azzam S.A wrote:

> Mimicking `prometheus_client` code also doesn't work for me.
>
> ```python
> def make_wsgi_custom():
>     def health(environ, start_response):
>         status = "200 OK"
>         output = "Hello World!\n"
>         response_headers = [
>             ("Content-type", "text/plain"),
>             ("Content-Length", str(len(output))),
>         ]
>         start_response(status, response_headers)
>         return [output]
>
>     return health
>
>
> app.wsgi_app = DispatcherMiddleware(
>     app.wsgi_app, {"/metrics": make_wsgi_app(), "/health": 
> make_wsgi_custom()}
> )
> ```
>
> I think I need to use [Multiprocess Mode (Gunicorn)](
> https://github.com/prometheus/client_python#multiprocess-mode-gunicorn). 
> But it comes with many limitations.
> Maybe I will put the health info (buld number) inside the `/matrics` too
> On Thursday, February 25, 2021 at 12:51:56 PM UTC+7 Azzam S.A wrote:
>
>>
>> I want to add a custom `/health` path in Python prometheus_client.
>> I am using Flask and Gunicorn for this.
>> Unfortunately, it doesn't work. I have tried:
>>
>> ```python
>> from flask import Flask
>> from werkzeug.middleware.dispatcher import DispatcherMiddleware
>> from prometheus_client import make_wsgi_app
>> from prometheus_client.core import REGISTRY
>> from exporter.collector import Collector
>>
>> REGISTRY.register(Collector())
>> app = Flask(__name__)
>>
>> def health():
>>     return {"status": "running", "build": "111"}
>>
>> app.wsgi_app = DispatcherMiddleware(
>>     app.wsgi_app, {"/metrics": make_wsgi_app(), "/health": health()}
>> )
>> ```
>>
>> I also try to use flask `@app.route` decorator. But dosn't work too:
>>
>> ```python
>> @app.route("/health")
>> def health():
>>     return {"status": "running", "build": "111"}
>>
>>
>> app.wsgi_app = DispatcherMiddleware(
>>     app.wsgi_app, {"/metrics": make_wsgi_app(), "/health": health()}
>> )
>>
>> ```
>>
>> Any endpoint (even undefined such `/foo`) will produce the same content 
>> as `/metrics`
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/1e687d63-372c-4502-892a-95a24e9bbbfan%40googlegroups.com.

Reply via email to