python自定义metric(python自定义函数实例)

网友投稿 400 2022-09-01


python自定义metric(python自定义函数实例)

1.Django

1.1环境

Django 3.1.1python 3.6.6django-prometheus 2.1.0

1.2安装django-prometheus

pip install django-prometheus

1.3metric

settings.py

INSTALLED_APPS = [ ... 'django_prometheus',]

urls.py

#这是一个/metrics的url请求path('', include('django_prometheus.urls')),#测试请求path('test', Test.as_view()),

__init__.py(也可以写在settings.py)

关键点:同一个指标的定义只能定义一次,定义多次会报错

from prometheus_client import Counter, Histogram, Gauge, Summarycounter = Counter('python_counter_one', 'url请求总数', ['application', 'method', 'endpoint'])gauge = Gauge('python_gauge_one', 'url请求时间', ['application', 'method', 'endpoint'])histogram = Histogram('python_histogram_one', 'url请求时间分布', ['application', 'method', 'endpoint'], buckets=[0, 50, 60, 70])summary = Summary('python_summary_one', 'url请求时间分布', ['application', 'method', 'endpoint'])

views.py

from django.import HttpResponsefrom django.views import Viewfrom djangoPrometheusExporter import counter, gauge, histogram, summaryclass Test(View): def get(self, request): # counter指标 counter.labels('app1', 'get', '/').inc(4) # gauge指标 gauge.labels('app1', 'get', '/').set(40) # histogram指标 histogram.labels('app1', 'get', '/').observe(40) histogram.labels('app1', 'get', '/').observe(50) histogram.labels('app1', 'get', '/').observe(60) # summary指标 summary.labels('app1', 'get', '/').observe(50) return HttpResponse(content='test')

启动django,先请求一次/test,在访问/metric

访问/metrics(​​HELP python_counter_one_total url请求总数# TYPE python_counter_one_total counterpython_counter_one_total{application="app1",endpoint="/",method="get"} 4.0# HELP python_counter_one_created url请求总数# TYPE python_counter_one_created gaugepython_counter_one_created{application="app1",endpoint="/",method="get"} 1.599635106319964e+09# HELP python_gauge_one url请求时间# TYPE python_gauge_one gaugepython_gauge_one{application="app1",endpoint="/",method="get"} 40.0# HELP python_histogram_one url请求时间分布# TYPE python_histogram_one histogrampython_histogram_one_bucket{application="app1",endpoint="/",le="0.0",method="get"} 0.0python_histogram_one_bucket{application="app1",endpoint="/",le="50.0",method="get"} 2.0python_histogram_one_bucket{application="app1",endpoint="/",le="60.0",method="get"} 3.0python_histogram_one_bucket{application="app1",endpoint="/",le="70.0",method="get"} 3.0python_histogram_one_bucket{application="app1",endpoint="/",le="+Inf",method="get"} 3.0python_histogram_one_count{application="app1",endpoint="/",method="get"} 3.0python_histogram_one_sum{application="app1",endpoint="/",method="get"} 150.0# HELP python_histogram_one_created url请求时间分布# TYPE python_histogram_one_created gaugepython_histogram_one_created{application="app1",endpoint="/",method="get"} 1.599635106319995e+09# HELP python_summary_one url请求时间分布# TYPE python_summary_one summarypython_summary_one_count{application="app1",endpoint="/",method="get"} 1.0python_summary_one_sum{application="app1",endpoint="/",method="get"} 50.0# HELP python_summary_one_created url请求时间分布# TYPE python_summary_one_created gaugepython_summary_one_created{application="app1",endpoint="/",method="get"} 1.599635106320102e+09...还有一些其他默认指标

2.Flask

2.1环境

python 3.6.6django-prometheus 2.1.0 Flask 1.1.2

2.2安装django-prometheus

pip install django-prometheus

2.3Metric

关键点:同一个指标的定义只能定义一次,定义多次会报错

# !/usr/bin/env python# -*- coding:utf-8 -*-from flask import Flask, Responsefrom prometheus_client import Counter, Gauge, Histogram, Summary, \ generate_latest, CollectorRegistryapp = Flask(__name__)registry = CollectorRegistry()counter = Counter('flask_counter_one', 'url请求总数', ['application', 'endpoint', 'method'], registry=registry)gauge = Gauge('flask_gauge_one', 'url请求总数', ['application', 'endpoint', 'method'], registry=registry)buckets = (50, 60, 70, float('inf'))histogram = Histogram('my_histogram', 'url请求时间分布', ['application', 'endpoint', 'method'], registry=registry, buckets=buckets)summary = Summary('my_summary', 'url请求时间分布', ['application', 'endpoint', 'method'], registry=registry)@app.route('/metrics')def metric(): return Response(generate_latest(registry), mimetype='text/plain')@app.route('/test')def test(): counter.labels('app1', 'get', '/').inc(4) gauge.labels('app1', 'get', '/').set(40) histogram.labels('app1', 'get', '/').observe(40) histogram.labels('app1', 'get', '/').observe(50) histogram.labels('app1', 'get', '/').observe(60) summary.labels('app1', 'get', '/').observe(50) return Response("test", mimetype='text/plain')if __name__ == '__main__': app.run(host='127.0.0.1', port=8080)

启动flask,先请求一次/test,在访问/metric

访问/metrics(​​HELP flask_counter_one_total url请求总数# TYPE flask_counter_one_total counterflask_counter_one_total{application="app1",endpoint="get",method="/"} 4.0# HELP flask_counter_one_created url请求总数# TYPE flask_counter_one_created gaugeflask_counter_one_created{application="app1",endpoint="get",method="/"} 1.599636192649268e+09# HELP flask_gauge_one url请求总数# TYPE flask_gauge_one gaugeflask_gauge_one{application="app1",endpoint="get",method="/"} 40.0# HELP my_histogram url请求时间分布# TYPE my_histogram histogrammy_histogram_bucket{application="app1",endpoint="get",le="50.0",method="/"} 2.0my_histogram_bucket{application="app1",endpoint="get",le="60.0",method="/"} 3.0my_histogram_bucket{application="app1",endpoint="get",le="70.0",method="/"} 3.0my_histogram_bucket{application="app1",endpoint="get",le="+Inf",method="/"} 3.0my_histogram_count{application="app1",endpoint="get",method="/"} 3.0my_histogram_sum{application="app1",endpoint="get",method="/"} 150.0# HELP my_histogram_created url请求时间分布# TYPE my_histogram_created gaugemy_histogram_created{application="app1",endpoint="get",method="/"} 1.5996361926493351e+09# HELP my_summary url请求时间分布# TYPE my_summary summarymy_summary_count{application="app1",endpoint="get",method="/"} 1.0my_summary_sum{application="app1",endpoint="get",method="/"} 50.0# HELP my_summary_created url请求时间分布# TYPE my_summary_created gaugemy_summary_created{application="app1",endpoint="get",method="/"} 1.5996361926494222e+09

3.官网

​​https://github.com/prometheus/client_python​​

​​https://github.com/korfuri/django-prometheus​​


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java命令行运行错误之找不到或无法加载主类问题的解决方法
下一篇:python模块之binascii(常用的python模块)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~