python仪表库_grafanalib:用于构建Grafana仪表板的Python库

grafanalib是一个用于从简单的Python脚本生成Grafana仪表板的库。它允许你版本化你的仪表板配置,并避免重复工作。通过示例代码展示了如何创建包含QPS和延迟图的前端统计仪表板,同时提供了安装和使用说明。
摘要由CSDN通过智能技术生成

grafanalib

68747470733a2f2f636972636c6563692e636f6d2f67682f7765617665776f726b732f67726166616e616c69622e7376673f7374796c653d736869656c64

Do you like Grafana but wish you could version your dashboard configuration? Do you find yourself repeating common patterns? If so, grafanalib is for you.

grafanalib lets you generate Grafana dashboards from simple Python scripts.

Writing dashboards

The following will configure a dashboard with a single row, with one QPS graph broken down by status code and another latency graph showing median and 99th percentile latency:

from grafanalib.core import *

dashboard = Dashboard(

title="Frontend Stats",

rows=[

Row(panels=[

Graph(

title="Frontend QPS",

dataSource='My Prometheus',

targets=[

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"1.."}[1m]))',

legendFormat="1xx",

refId='A',

),

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"2.."}[1m]))',

legendFormat="2xx",

refId='B',

),

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"3.."}[1m]))',

legendFormat="3xx",

refId='C',

),

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"4.."}[1m]))',

legendFormat="4xx",

refId='D',

),

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))',

legendFormat="5xx",

refId='E',

),

],

yAxes=[

YAxis(format=OPS_FORMAT),

YAxis(format=SHORT_FORMAT),

],

alert=Alert(

name="Too many 500s on Nginx",

message="More than 5 QPS of 500s on Nginx for 5 minutes",

alertConditions=[

AlertCondition(

Target(

expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))',

legendFormat="5xx",

refId='A',

),

timeRange=TimeRange("5m", "now"),

evaluator=GreaterThan(5),

operator=OP_AND,

reducerType=RTYPE_SUM,

),

],

)

),

Graph(

title="Frontend latency",

dataSource='My Prometheus',

targets=[

Target(

expr='histogram_quantile(0.5, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))',

legendFormat="0.5 quantile",

refId='A',

),

Target(

expr='histogram_quantile(0.99, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))',

legendFormat="0.99 quantile",

refId='B',

),

],

yAxes=single_y_axis(format=SECONDS_FORMAT),

),

]),

],

).auto_panel_ids()

There is a fair bit of repetition here, but once you figure out what works for your needs, you can factor that out. See our Weave-specific customizations for inspiration.

Generating dashboards

If you save the above as frontend.dashboard.py (the suffix must be .dashboard.py), you can then generate the JSON dashboard with:

$ generate-dashboard -o frontend.json frontend.dashboard.py

Installation

grafanalib is just a Python package, so:

$ pip install grafanalib

Support

This library is in its very early stages. We'll probably make changes that break backwards compatibility, although we'll try hard not to.

grafanalib works with Python 2.7, 3.4, 3.5, and 3.6.

Developing

If you're working on the project, and need to build from source, it's done as follows:

$ virtualenv .env

$ . ./.env/bin/activate

$ pip install -e .

gfdatasource

This module also provides a script and docker image which can configure grafana with new sources, or enable app plugins.

The script answers the --help with full usage information, but basic invocation looks like this:

$ --grafana-url http://grafana. datasource --data-source-url http://datasource

$ --grafana-url http://grafana. app --id my-plugin

Getting Help

If you have any questions about, feedback for or problems with grafanalib:

Invite yourself to the #weave-community slack channel.

Ask a question on the #weave-community slack channel.

Your feedback is always welcome!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值