Monitoring Flask microservices with Prometheus

栏目: IT技术 · 发布时间: 6年前

内容简介:Getting insights into how your Python web services are doing can be easily done with a few lines of extra code.To demonstrateThat’s really it to get started! By adding an import and a line to initialize

Getting insights into how your Python web services are doing can be easily done with a few lines of extra code.

To demonstrate prometheus_flask_exporter with a minimal example:

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app)

@app.route('/')
def main():
    return 'OK'

That’s really it to get started! By adding an import and a line to initialize PrometheusMetrics you’ll get request duration metrics and request counters exposed on the /metrics endpoint of the Flask application it’s registered on, along with all the default metrics you get from the underlying Prometheus client library .

You can find an easy-to-run example in the GitHub repo that spins up a Prometheus and aGrafanainstance along with a demo app to generate some metrics, which will look something like this:

Monitoring Flask microservices with Prometheus

You’ll also find the list of metrics in the README of the example that are displayed on the dashboard, along with the Prometheus queries which populate the panels.

Configuration

The library has lots of configuration options, have a look at the project README for examples of them with a brief explanation.

The basic configuration is as shown at the top. Simply create a PrometheusMetrics instance, let’s call it metrics , then use it do define additional metrics you want collected by decorating functions with:

@metrics.counter(..)
@metrics.gauge(..)
@metrics.summary(..)
@metrics.histogram(..)

The counters count invocations, while the rest of them collect metrics based on the duration of those invocations. You can define labels for each of these, potentially using properties of the request or the response. For example:

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)

# group by endpoint rather than path
metrics = PrometheusMetrics(app, group_by='endpoint')

@app.route('/collection/:collection_id/item/:item_id')
@metrics.counter(
    'cnt_collection', 'Number of invocations per collection', labels={
        'collection': lambda: request.view_args['collection_id'],
        'status': lambda resp: resp.status_code
    })
def get_item_from_collection(collection_id, item_id):
    pass

In the example above, hitting the endpoint /collection/10002/item/76 would increment a counter like cnt_collection{ collection="10002", status="200" } , plus you would get the default metrics (per-endpoint in this example) from the library, by default:

  • flask_http_request_duration_seconds - HTTP request duration in seconds for all Flask requests by method, path and status
  • flask_http_request_total - Total number of HTTP requests by method and status

There are options to skip tracking certain endpoints, registering more default metrics, or skipping the ones above, or applying the same custom metric to multiple endpoints. Check out the project README to see what’s available.

app = Flask(__name__)
metrics = PrometheusMetrics(app)

@app.route('/')
def main():
    pass  # requests tracked by default

@app.route('/skip')
@metrics.do_not_track()
def skip():
    pass  # default metrics are not collected

# custom metric to be applied to multiple endpoints
common_counter = metrics.counter(
    'by_endpoint_counter', 'Request count by endpoints',
    labels={'endpoint': lambda: request.endpoint}
)

@app.route('/common/one')
@common_counter
def endpoint_one():
    pass  # tracked by the custom and the default metrics

@app.route('/common/two')
@common_counter
def endpoint_two():
    pass  # also tracked by the custom and the default metrics

# register additional default metrics
metrics.register_default(
    metrics.counter(
        'by_path_counter', 'Request count by request paths',
        labels={'path': lambda: request.path}
    )
)

The library has some handy extensions for popular multiprocessing libraries, like uWSGI andGunicorn. You can also find small examples for targeted use-cases, including these multiprocessing ones.

Scraping metrics

As mentioned above, the library exposes a /metrics endpoint on the Flask application by default, which can serve as a target for Prometheus scraping .

From the example with the dashboard above, you can point your Prometheus to a Flask app with the default settings with configuration like this:

scrape_configs:
  - job_name: 'example'

    dns_sd_configs:
      - names: ['app']
        port: 5000
        type: A
        refresh_interval: 5s

See the full example in the GitHub repository . This assumes, that Prometheus can target your Flask application instances on http://app:5000/metrics , where the app domain name can potentially resolve to multiple IP addresses, like when running in Kubernetes orDocker Swarm.

If exposing the metrics endpoint like this is not suitable for you, perhaps because you don’t want to allow external access to it, you can easily disable it by passing path=None when creating the PrometheusMetrics instance.

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app, path=None)

...

metrics.start_http_server(5099)

You can then use start_http_server(port) to expose this endpoint on a different HTTP port, 5099 in the example above. Alternatively, if you’re happy with the endpoint being on the same Flask app, but need to change its path away from /metrics , you can either pass in a different URI as the path parameter, or use register_endpoint(..) to set it up later.

References

If you give it a go, open a GitHub issue or leave a comment here with your feedback and suggestions! Thanks!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

村落效应

村落效应

[加] 苏珊·平克(Susan Pinker) / 青涂 / 浙江人民出版社 / 2017-3-1 / CNY 69.90

 面对面的接触是作为社会性动物的人类最古老、深刻的需求。在互联网时代,社交媒体已经成为人际沟通的主体,人际关系的维系越来越被社交媒体上的点赞、转发、评论代替,在冰冷的互动中,我们失去了真实与温度。面对面的人际关系与接触能让人感受到如村落生活般的归属感,它是一个人免疫力、复原力和影响力的真正来源。虽然互联网拥有毋庸置疑的优势,但是如果我们渴望快乐、健康、长寿……没错,还有智慧,我们就需要想方设法腾......一起来看看 《村落效应》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具