zoukankan      html  css  js  c++  java
  • Prometheus-自定义Node_Exporter

    标量(Scalar):一个浮点型的数字值

    标量只有一个数字,没有时序。

    需要注意的是,当使用表达式count(http_requests_total),返回的数据类型,依然是瞬时向量。用户可以通过内置函数scalar()将单个瞬时向量转换为标量。

    Prometheus-自定义Exporter-使用flask

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    from flask import Flask,request,render_template,jsonify,Markup
    import json
    
    import logging
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)
    #定义一个全局字典
    Show_Prometheus_Handler = {
    
    }
    
    app = Flask(__name__)
    
    @app.route('/send_data',methods={'GET','POST'})
    def send_data_to_dict():
        if request.method == "GET":
            return "method error,please use post method!"
        else:
            # 向dict里注册
            global Show_Prometheus_Handler
    
            message = request.data
            #print(request.data)
            message_to_json = json.loads(message)
            metric = message_to_json.get("metric",None)
            type_staus = message_to_json.get("counterType",None)
            value = message_to_json.get("value",None)
            tags = message_to_json.get("tags",None)
            endpoint = message_to_json.get("endpoint",None)
            # 生成字段
            tag_list = []
            for tag in tags.split(","):
                t1,t2 = tag.split("=")
                tag_list.append('{0}="{1}"'.format(t1,t2))
            else:
                tag_list.append('%s="%s"' %("endpoint",endpoint))
    
            Show_Prometheus_Handler[metric] = '# HELP %s prometheus collected metric.
    # TYPE %s %s
    %s{%s} %s' %(metric,metric,type_staus.lower(),metric,",".join(tag_list),value)
            #print(Show_Prometheus_Handler[metric])
    
            return "ok"
    
    
    @app.route('/metrics',methods=['GET','POST'])
    def get_metrics():
        if request.method == "GET":
            metric_list = []
            for _,value in Show_Prometheus_Handler.items():
                metric_list.append(value)
            content_msg = Markup('{0}'.format("
    ".join(metric_list)))
            return render_template('status.html',content=content_msg), {'Content-Type': 'text/plain; version=0.0.4'}
    
    @app.route("/clean_metrics",methods=["GET","POST"])
    def clean_metrics():
        if request.method == "GET":
            global Show_Prometheus_Handler
            Show_Prometheus_Handler = {}
            return "clean_ok"
    
    if __name__ == '__main__':
        app.run("0.0.0.0",port=7777,threaded=True)
    

     需要注意的点  当Prometheus 出现Navtive Token的时候 需要在返回html的时候 加上 {'Content-Type': 'text/plain; version=0.0.4'}  Prometheus才能正常拉取数据.

  • 相关阅读:
    C++ 多线程
    C++ 信号处理
    首页流量监控代码
    macro-name replacement-text 宏 调试开关可以使用一个宏来实现 do { } while(0)
    color depth 色彩深度 像素深度
    数据更新 数据同步 起始点 幂等同步历史数据
    获取当前调用函数名 方法名
    版本号风格为 Major.Minor.Patch
    query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
    Cutoff frequency
  • 原文地址:https://www.cnblogs.com/liujiliang/p/9694685.html
Copyright © 2011-2022 走看看