0%

prometheus【pushgateway部署使用】

PushGateway使用说明

  • Prometheus采用定时Pull模式,可能由于子网络或者防火墙的原因,不能直接拉取各个Target的指标数据,此时可以采用各个TargetPushGatewayPush数据,然后PrometheusPushGateway上定时pull
  • 其次在监控各个业务数据时,需要将各个不同的业务数据进行统一汇总,此时也可以采用PushGateway来统一收集,然后Prometheus来统一拉取
  • Prometheus每次从PushGateway拉取的数据,并不是拉取周期内用户推送上来的所有数据,而是最后一次PushPushGateway上的数据
  • 指标值只能是数字类型,非数字类型报错
  • 指标值支持最大长度为16位,超过16位后默认置为0

安装

# 默认不持久化数据
docker run -d -p 9091:9091 prom/PushGateway 

# 指定持久化文件和保留时间
docker run -d -p 9091:9091 prom/pushgateway "-persistence.file=pg_file –persistence.interval=5m"

测试

# 单条推送
echo "test_metric 123456" | curl --data-binary @- http://192.168.41.128:9091/metrics/job/test_job

# 多条推送
cat <<EOF | curl --data-binary @- http://192.168.41.128:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metrics counter
test_metrics{label="app1",name="demo"} 100.00
# TYPE another_test_metrics gauge
# HELP another_test_metrics Just an example.
another_test_metrics 123.45
EOF

# 通过文件
vim pg.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
# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{code="200",interface="/v1/core"} 0.122

curl -XPOST --data-binary @pg.txt http://192.168.41.128:9091/metrics/job/app/instance/app-192.168.41.128

删除指标,参考官方

# {job="some_job",instance="some_instance"}
curl -X DELETE http://192.168.41.128:9091/metrics/job/some_job/instance/some_instance

# {job="some_job"}
curl -X DELETE http://192.168.41.128:9091/metrics/job/some_job

# 删除所有, 需要开启--web.enable-admin-api
curl -X PUT http://192.168.41.128:9091/api/v1/admin/wipe

通过SDK上传:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.7.0</version>
</dependency>
public static void main(String[] args) {
    try{
        String url = "192.168.41.128:9091";
        CollectorRegistry registry = new CollectorRegistry();
        Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.").create();
        guage.set(23.12);
        guage.register(registry);
        PushGateway pg = new PushGateway(url);
        Map<String, String> groupingKey = new HashMap<String, String>();
        groupingKey.put("instance", "my_instance");
        pg.pushAdd(registry, "my_job", groupingKey);
    } catch (Exception e){
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    try{
        String url = "172.30.12.167:9091";
        CollectorRegistry registry = new CollectorRegistry();
        Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.").labelNames("app", "date").create();
        String date = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date());
        guage.labels("my-pushgateway-test-0", date).set(25);
        guage.labels("my-pushgateway-test-1", date).dec();
        guage.labels("my-pushgateway-test-2", date).dec(2);
        guage.labels("my-pushgateway-test-3", date).inc();
        guage.labels("my-pushgateway-test-4", date).inc(5);
        guage.register(registry);
        PushGateway pg = new PushGateway(url);
        Map<String, String> groupingKey = new HashMap<String, String>();
        groupingKey.put("instance", "my_instance");
        pg.pushAdd(registry, "my_job", groupingKey);
    } catch (Exception e){
        e.printStackTrace();
    }
}

查看

参考链接