python3+flask的prometheus监控示例

1.源码

#!/usr/bin/env python3

#测试prometheus的python接口
#参考文献  https://github.com/prometheus/client_python/blob/master/README.md

from prometheus_client import start_http_server, Summary
import random
import time

import os
import time
import datetime
import pymongo
import math

import logging
import logging.handlers

from flask import Flask, request, jsonify
from gevent.wsgi import WSGIServer

app = Flask(__name__)

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@app.route("/t1")
@REQUEST_TIME.time()
def process_request():
    """A dummy function that takes some time."""
    time.sleep(1)
    return jsonify({"hi":"world"})


if __name__ == '__main__':
    #这里是prometheus的监控端口,展示matrix信息
    start_http_server(8000)
    #启动flask
    http_server = WSGIServer(('0.0.0.0', 6666), app)
    http_server.serve_forever()

2.执行流程

    2.1 启动py程序。

    2.2 在浏览器开一个页面,打开http://localhost:8000 能看到度量参数

    2.3 在浏览器开另一个页面,打开http://localhost:6666/t1,每刷新一下,就能看到2.2的参数变动一下。

    2.4 可以增加更多的参数。

3. prometheus和k8s介绍
  3.1 k8s是容器管理 
    https://blog.csdn.net/zhangxxxww/article/details/73547251
  3.2 prometheus是开源监控,k8s也使用prometheus
    http://www.cnblogs.com/vovlie/p/Prometheus_CONCEPTS.html

猜你喜欢

转载自blog.csdn.net/u011539200/article/details/80714834