rycus86 / prometheus_flask_exporter

Prometheus exporter for Flask applications
https://pypi.python.org/pypi/prometheus-flask-exporter
MIT License
646 stars 161 forks source link

two questions about filtering information (only need path info, omit static file like *.css) #155

Open CooFlow opened 1 year ago

CooFlow commented 1 year ago

How do I save only the information I need?

I only need info about path count: flask_http_request_duration_seconds_count{method="GET",path="/sanhe/products",status="200"} 2.0

how can I hidden following information in route /metrics: can I

flask_http_request_duration_seconds_bucket{le="0.005",method="GET",path="/sanhe/products",status="200"} 1.0
flask_http_request_duration_seconds_bucket{le="0.01",method="GET",path="/sanhe/products",status="200"} 1.0
......
flask_http_request_duration_seconds_bucket{le="7.5",method="GET",path="/sanhe/products",status="200"} 2.0
flask_http_request_duration_seconds_bucket{le="10.0",method="GET",path="/sanhe/products",status="200"} 2.0
flask_http_request_duration_seconds_bucket{le="+Inf",method="GET",path="/sanhe/products",status="200"} 2.0

Can I add filters to the information?

I don't need count static files like .js, .css

image

Thank you very much for checking my issue.

rycus86 commented 1 year ago

Hi! Please check the README for excluding things you don't need or want: https://github.com/rycus86/prometheus_flask_exporter#default-metrics

You can avoid recording metrics on individual endpoints by decorating them with @metrics.do_not_track(), or use the excluded_paths argument when creating the PrometheusMetrics instance that takes a regular expression (either a single string, or a list) and matching paths will be excluded. These apply to both built-in and user-defined default metrics, unless you disable it by setting the exclude_user_defaults argument to False. If you have functions that are inherited or otherwise get metrics collected that you don't want, you can use @metrics.exclude_all_metrics() to exclude both default and non-default metrics being collected from it.

See also the https://github.com/rycus86/prometheus_flask_exporter#configuration section that has more customisation options.

CooFlow commented 1 year ago

thank you very much, I solved my second problem with your help.

metrics = PrometheusMetrics(app, excluded_paths=".*.js|.*.gif|.*.css|.*.mp4|.*.jpg|.*.png|.*.svg|.*.ico|.*.woff2")

however, I still don't know how to filter specific default

when I set

metrics = PrometheusMetrics(app,
                            excluded_paths=".*.js|.*.gif|.*.css|.*.mp4|.*.jpg|.*.png|.*.svg|.*.ico|.*.woff2",
                            exclude_user_defaults=False,
                            export_defaults=False,
                            )

I lose all default information, how can I make the last two metrics appear?

I don't need some of default metrics

image
rycus86 commented 1 year ago

To get those, you can follow the README instructions in https://github.com/rycus86/prometheus_flask_exporter#default-metrics

To register your own default metrics that will track all registered Flask view functions, use the register_default function. Note: register your default metrics after all routes have been set up. Also note, that Gauge metrics registered as default will track the /metrics endpoint, and this can't be disabled at the moment.