当前位置 : 主页 > 编程语言 > python >

python自定义metric

来源:互联网 收集:自由互联 发布时间:2022-06-15
1.Django 1.1环境 Django 3.1 .1 python 3.6 .6 django - prometheus 2.1 .0 1.2安装django-prometheus pip install django - prometheus 1.3metric settings.py INSTALLED_APPS = [ ... 'django_prometheus' , ] urls.py #这是一个/metrics的url请求

1.Django

1.1环境

Django 3.1.1
python 3.6.6
django-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, Summary

counter = 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.http import HttpResponse
from django.views import View
from djangoPrometheusExporter import counter, gauge, histogram, summary


class 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(​​http://127.0.0.1:8080/metrics​​)

# HELP python_counter_one_total url请求总数
# TYPE python_counter_one_total counter
python_counter_one_total{application="app1",endpoint="/",method="get"} 4.0
# HELP python_counter_one_created url请求总数
# TYPE python_counter_one_created gauge
python_counter_one_created{application="app1",endpoint="/",method="get"} 1.599635106319964e+09

# HELP python_gauge_one url请求时间
# TYPE python_gauge_one gauge
python_gauge_one{application="app1",endpoint="/",method="get"} 40.0

# HELP python_histogram_one url请求时间分布
# TYPE python_histogram_one histogram
python_histogram_one_bucket{application="app1",endpoint="/",le="0.0",method="get"} 0.0
python_histogram_one_bucket{application="app1",endpoint="/",le="50.0",method="get"} 2.0
python_histogram_one_bucket{application="app1",endpoint="/",le="60.0",method="get"} 3.0
python_histogram_one_bucket{application="app1",endpoint="/",le="70.0",method="get"} 3.0
python_histogram_one_bucket{application="app1",endpoint="/",le="+Inf",method="get"} 3.0
python_histogram_one_count{application="app1",endpoint="/",method="get"} 3.0
python_histogram_one_sum{application="app1",endpoint="/",method="get"} 150.0
# HELP python_histogram_one_created url请求时间分布
# TYPE python_histogram_one_created gauge
python_histogram_one_created{application="app1",endpoint="/",method="get"} 1.599635106319995e+09

# HELP python_summary_one url请求时间分布
# TYPE python_summary_one summary
python_summary_one_count{application="app1",endpoint="/",method="get"} 1.0
python_summary_one_sum{application="app1",endpoint="/",method="get"} 50.0
# HELP python_summary_one_created url请求时间分布
# TYPE python_summary_one_created gauge
python_summary_one_created{application="app1",endpoint="/",method="get"} 1.599635106320102e+09

...还有一些其他默认指标

2.Flask

2.1环境

python 3.6.6
django-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, Response
from prometheus_client import Counter, Gauge, Histogram, Summary, \
generate_latest, CollectorRegistry

app = 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(​​http://127.0.0.1:8080/metrics​​)

# HELP flask_counter_one_total url请求总数
# TYPE flask_counter_one_total counter
flask_counter_one_total{application="app1",endpoint="get",method="/"} 4.0
# HELP flask_counter_one_created url请求总数
# TYPE flask_counter_one_created gauge
flask_counter_one_created{application="app1",endpoint="get",method="/"} 1.599636192649268e+09

# HELP flask_gauge_one url请求总数
# TYPE flask_gauge_one gauge
flask_gauge_one{application="app1",endpoint="get",method="/"} 40.0

# HELP my_histogram url请求时间分布
# TYPE my_histogram histogram
my_histogram_bucket{application="app1",endpoint="get",le="50.0",method="/"} 2.0
my_histogram_bucket{application="app1",endpoint="get",le="60.0",method="/"} 3.0
my_histogram_bucket{application="app1",endpoint="get",le="70.0",method="/"} 3.0
my_histogram_bucket{application="app1",endpoint="get",le="+Inf",method="/"} 3.0
my_histogram_count{application="app1",endpoint="get",method="/"} 3.0
my_histogram_sum{application="app1",endpoint="get",method="/"} 150.0
# HELP my_histogram_created url请求时间分布
# TYPE my_histogram_created gauge
my_histogram_created{application="app1",endpoint="get",method="/"} 1.5996361926493351e+09

# HELP my_summary url请求时间分布
# TYPE my_summary summary
my_summary_count{application="app1",endpoint="get",method="/"} 1.0
my_summary_sum{application="app1",endpoint="get",method="/"} 50.0
# HELP my_summary_created url请求时间分布
# TYPE my_summary_created gauge
my_summary_created{application="app1",endpoint="get",method="/"} 1.5996361926494222e+09

3.官网

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

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

网友评论