Stay Hungry,Stay Foolish!

开源监控系统Prometheus的前世今生

开源监控系统Prometheus的前世今生

http://www.uml.org.cn/itil/202012101.asp

https://prometheus.io/docs/introduction/overview/

 

info数据结构

可以存储任意信息

https://prometheus.github.io/client_python/instrumenting/info/

from prometheus_client import Info

i = Info('my_build_version', 'Description of info')

i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})

 

其它四种常用数据类型

https://prometheus.io/docs/concepts/metric_types/

 

查询接口

https://prometheus.io/docs/prometheus/latest/querying/api/

Querying exemplars

$ curl -g 'http://localhost:9090/api/v1/query_exemplars?query=test_exemplar_metric_total&start=2020-09-14T15:22:25.479Z&end=2020-09-14T15:23:25.479Z'
{
    "status": "success",
    "data": [
        {
            "seriesLabels": {
                "__name__": "test_exemplar_metric_total",
                "instance": "localhost:8090",
                "job": "prometheus",
                "service": "bar"
            },
            "exemplars": [
                {
                    "labels": {
                        "trace_id": "EpTxMJ40fUus7aGY"
                    },
                    "value": "6",
                    "timestamp": 1600096945.479
                }
            ]
        },
        {
            "seriesLabels": {
                "__name__": "test_exemplar_metric_total",
                "instance": "localhost:8090",
                "job": "prometheus",
                "service": "foo"
            },
            "exemplars": [
                {
                    "labels": {
                        "trace_id": "Olp9XHlq763ccsfa"
                    },
                    "value": "19",
                    "timestamp": 1600096955.479
                },
                {
                    "labels": {
                        "trace_id": "hCtjygkIHwAN9vs4"
                    },
                    "value": "20",
                    "timestamp": 1600096965.489
                }
            ]
        }
    ]
}

 

airflow-prometheus-pushgateway

https://github.com/DanielMorales9/airflow-prometheus-pushgateway

https://github.com/DanielMorales9/airflow-prometheus-pushgateway/blob/master/airflow/dags/hello_world.py

import time
from datetime import datetime
from datetime import timedelta
from random import randint, random
from timeit import default_timer as timer

from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from prometheus_client import CollectorRegistry, push_to_gateway, Summary, Counter

GATEWAY = 'pushgateway'


def print_hello():
    registry = CollectorRegistry()
    c = Counter('count_exceptions', 'counts number of successes and failures',
                labelnames=['type'], registry=registry)
    s = Summary('time_delta', 'execution time of print_hello function', registry=registry)

    for i in range(randint(1, 10)):
        start = timer()
        time.sleep(random()*10)

        try:

            if randint(0, 1) == 1:
                raise Exception

            c.labels(type='success').inc()

        except:
            c.labels(type='failure').inc()

        end = timer()
        s.observe(timedelta(seconds=end - start).seconds)

    push_to_gateway('%s:9091' % GATEWAY, job='print_hello', registry=registry)

    return 'Hello world!'


dag = DAG('hello_world',
          description='Simple tutorial DAG',
          schedule_interval='*/1 * * * *',
          start_date=datetime(2017, 3, 20),
          catchup=False)

dummy_operator = DummyOperator(task_id='dummy_task', retries=3, dag=dag)

hello_operator = PythonOperator(task_id='hello_task', python_callable=print_hello, dag=dag)

dummy_operator >> hello_operator

 

自定义node exporter

https://www.cnblogs.com/liukx/p/15549688.html

#!/usr/bin/python3
# coding:utf-8
from prometheus_client import Gauge, start_http_server, Counter
from prometheus_client.core import CollectorRegistry
import prometheus_client
import uvicorn
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

# 定义api对象
app = FastAPI()

# 设置接口访问路径/metrics
# @app语法糖需要放在最终入口函数
@app.get('/metrics', response_class=PlainTextResponse)
def get_data():
    '''
    该函数为最终函数入口(采集数据函数),该例子模拟采集到数据标签label1、label2和label3,数据data1
    '''
    # 定义client_python里提供的prometheus Gauge数据类型
    REGISTRY = CollectorRegistry(auto_describe=False)
    example_G = Gauge("this_is_a_metric_name", "this is a metric describe", ["label1", "label2", "label3"], registry=REGISTRY)
    
    label1 = '111'
    label2 = '222'
    label3 = '333'
    data1 = '444'
    # 调用Gauge数据类型,把采集的数据放入到设定的example_G
    example_G.labels(label1,label2,label3).set(data1)
    # return需放在函数下最外层,否则返回的数据有缺失
    return prometheus_client.generate_latest(REGISTRY)
 
# 用uvicorn调用接口,启用端口为9330
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=9330, log_level="info")

 

posted @ 2024-10-24 09:36  lightsong  阅读(4)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel