Python:使用prometheus-client提交数据到实现prometheus+ grafana数据监控

news/2024/5/19 0:21:43 标签: python, prometheus, grafana

相关资料

目录

1、使用Python提供数据源

安装依赖

pip install prometheus-client

基于flask服务代码示例

随机返回一些数值

python"># -*- coding: utf-8 -*-
"""
@File    : demo.py
@Date    : 2023-06-30
"""

import random
import prometheus_client
from flask import Flask, Response
from prometheus_client import Gauge
from prometheus_client.core import CollectorRegistry

app = Flask(__name__)


@app.get('/metrics')
def metrics():
    """
    prometheus metrics接口
    :return:
    """
    registry = CollectorRegistry(auto_describe=False)

    gauge = Gauge(
        name="flask_app",
        documentation="this is a flask app data",
        labelnames=["label"],
        registry=registry)

    rows = [
        {
            'label': '张飞',
            'value': random.randint(10, 20)
        },
        {
            'label': '赵云',
            'value': random.randint(10, 20)
        },
        {
            'label': '刘备',
            'value': random.randint(10, 20)
        }
    ]

    for row in rows:
        gauge.labels(row['label']).set(row['value'])

    return Response(prometheus_client.generate_latest(registry), mimetype='text/plain')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

prometheus_76">2、启动 prometheus

From metrics to insight Power your metrics and alerting with the leading open-source monitoring solution.

译文:从度量到洞察力,通过领先的开源监控解决方案。

配置文件 prometheus.yml

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
      - targets: ["192.168.0.101:8000"]

注意:如果是docker方式启动,ip地址需要配置局域网ip,不能使用127.0.0.1

使用docker方式启动 prometheus

docker run \
-p 9090:9090 \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
--name prometheus \
prom/prometheus

访问地址:http://localhost:9090/

在这里插入图片描述

grafana_136">3、启动 grafana

The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.

译文:开放、可组合的可观测性和数据可视化平台。可视化来自Prometheus、Loki、Elasticsearch、InfluxDB、Postgres等多个来源的指标、日志和跟踪。

使用docker方式启动 grafana

docker run -d \
-p 3000:3000 \
--name grafana \
grafana/grafana

访问地址:http://localhost:3000/

在这里插入图片描述

参考文章

  1. grafana添加prometheus数据源并导入仪表盘展示
  2. 数据采集export使用prometheus_client 和 Flask实现

http://www.niftyadmin.cn/n/993766.html

相关文章

linux服务器安装及常用软件安装---本人全都一步一步照着做的

安装linux系统CentOS-7版本 使用U盘安装&#xff0c;教程https://article.itxueyuan.com/Dpd8r 及系统镜像下载&#xff1a;https://www.linuxidc.com/Linux/2014-10/108014.htm 安装系统时&#xff0c;选择自己配置分区/boot 配置2-3G 挂在swap也配置上 Linxu服务器开启SS…

raid0、1、5、10创建

•常见raid原理 •raid卡 •配置raid •删除raid 实验需要添加5块硬盘 sda , b ,c,d,e,f RAID&#xff08;Redundant Array of Independent Disks,独立磁盘冗余阵列&#xff09;可以提供较普通磁盘更高的速度、安全性&#xff0c;所以服务器在安装时都会选择创建RAID。RAID的创…

java访问asmx接口

网上找了很多例子&#xff0c;很多没法用&#xff0c;只有这个成功了记录一下,原链接忘了&#xff0c;大哥不好意思 <dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.1</ver…

Puppet cert命令参数介绍(四)

puppet cert是管理Puppet证书签名的命令,在agent访问master时使用的是SSL安全套接字&#xff0c;优点是加密双方的通信数据&#xff0c;从而保证信息安全.puppet cert命令可以实现对证书的管理、授权、回收、显示和产生签名文件. 1、查看puppet cert帮助信息:(帮助文档注释过多…

RSA密钥C#端转换成java密钥

BASE64工具类 /** Copyright (C) 2010 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** …

RHEL 5基础篇—打包、压缩文件或目录

打包、压缩文件或目录 打包&#xff1a;在linux中打包即将许多个文件和目录合并保存为一个整体的包文件&#xff0c;以方便传递或携带。 压缩&#xff1a;而压缩操作可以进一步降低打包好的包文件所占用的磁盘空间。 在linux系统中比较常见的压缩工具包括gzip、bzip2&#xff…

配置Tomcat使用https协议(单向认证)

超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息。HTTP协议以明文方式发送内容&#xff0c;不提供任何方式的数据加密&#xff0c;如果攻击者截取了Web浏览器和网站服务器之间的传输报文&#xff0c;就可以直接读懂其中的信息&#xff0c;因此HTTP协议不适合…

radiogroup的bug

{xtype:"radiogroup",name:"sex1",fieldLabel:性别,width:300,items:[{xtype:"radio", name : "sex", //传到后台的参数名inputValue :1, //传到后台的参数值boxLabel : "男", //显示标签checked:true }…