一.pushGateway搭建
一.搭建pushgateway
docker pull prom/pushgateway
docker run -d --name=pg -p 9091:9091 prom/pushgateway
或者
docker run -d --name=pg10 -p 9091:9091 prom/pushgateway "--persistence.file=pushgateway.data --persistence.interval=3" 持久化保存数据
#访问方式:
http://xxx.195.185.78:9091/#
二.配置prometheus
2.1修改配置
- job_name: "pushgateway"
static_configs:
- target: ['10.206.16.14:9091']
labels:
instance: pushgateway
2.2 使配置生效(使用k8s搭建)
kubectl apply -f prometheus-configmap.yaml
curl -X POST 10.1.192.105:9090/-/reload
2.3 查看是否在prometheus生效
http://xxx.195.185.78:30089/targets
#参考资料
https://www.cnblogs.com/xiao987334176/p/9933963.html
https://songjiayang.gitbooks.io/prometheus/content/pushgateway/how.html
二.shell数据管理
2.1操作
#2.1 shell形式添加数据,主要是添加一个job
echo "some_metric 3.14" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job
#2.2在某一个job下添加instance
cat <<EOF | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job/instance/some_instance # TYPE some_metric counter
some_metric{label="val1"} 999
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
#2.3以文件形式发送到pushgateway
vim pgdata.txt
# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{code="200",interface="/v1/save"} 276
http_request_total{code="404",interface="/v1/delete"} 0
http_request_total{code="500",interface="/v1/save"} 1
http_request_time{code="200",interface="/v1/core"} 0.122
curl -XPOST --data-binary @pgdata.txt http://127.0.0.1:9091/metrics/job/app/instance/app-172.30.0.0
#2.4常用操作
#删除某个组下的所有数据
curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job
#删除某个组下某个实例的所有数据:
curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job/instance/some_instance
2.2结果
三. python数据管理
3.1 安装模块
#1.提前安装py模块
pip install flask
pip install prometheus_client
3.2 prometheus提供4种不通同类型metrics
Counter, Gauge, Summar和Histogram
3.3 使用gauge方式写入数据
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
if __name__ == '__main__':
registry = CollectorRegistry()
labels = ['req_status', 'req_method', 'req_url']
g_one = Gauge('requests_total', 'url请求次数', labels, registry=registry)
g_two = Gauge('avg_response_time_seconds', '1分钟内的URL平均响应时间', labels, registry=registry)
g_one.labels('200','GET', '/test/url').set(1) #set设定值
g_two.labels('200','GET', '/test/api/url/').set(10) #set设定值
push_to_gateway('http://127.0.0.1:9091', job='SampleURLMetrics', registry=registry)
四.go数据管理
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
)
func main() {
ExamplePusher_Push()
}
func ExamplePusher_Push() {
completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "db_backup_last_completion_timestamp_seconds",
Help: "The timestamp of the last successful completion of a DB backup.",
})
completionTime.SetToCurrentTime()
if err := push.New("http://127.0.0.1:9091", "db_backup"). // push.New("pushgateway地址", "job名称")
Collector(completionTime). // Collector(completionTime) 给指标赋值
Grouping("db", "customers").Grouping("instance", "1.1.1.1"). // 给指标添加标签,可以添加多个
Push(); err != nil {
fmt.Println("Could not push completion time to Pushgateway:", err)
}
}