Prometheus简单使用

prometheus_client

"""
@Time : 2020/6/10
@Author : wangrx
@File : pp.py 
@Software: PyCharm
### prometheus_client库pushgateway简单使用
"""
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
import time
import random

registry = CollectorRegistry()
g = Gauge('some_test1', "some test", ['myname'], registry=registry)
g.labels(myname='wahaha').set(random.randint(30,200))
# g.set_to_current_time()
push_to_gateway('10.0.66.10:9091', job='some_job1', registry=registry)

Prometheus结合consul自动添加pushgateway

# prometheus.yml配置文件
  - job_name: consul
    consul_sd_configs:
      - server: '127.0.0.1:8500'
        services: []
    relabel_configs:
      - source_labels: [__meta_consul_service] #service 源标签
        regex: "consul"  #匹配为"consul" 的service
        action: drop       # 执行的动作

注册服务到consul

curl -X PUT -d '{"id": "127.0.0.1","name": "emmm123","address": "10.0.66.10","port": ''9091,"tags": ["DEV"], "checks": [{"http": "http://10.0.66.10:9091/","interval": "5s"}]}' http://127.0.0.1:8500/v1/agent/service/register

重载Prometheus配置文件

# 需要添加参数
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle &
# 重载接口
curl -X POST http://127.0.0.1:9090/-/reload

Consul图示

Prometheus图示

posted @ 2020-06-11 15:10  ruixing  阅读(652)  评论(0编辑  收藏  举报