其他分享
首页 > 其他分享> > pushgateway安装使用

pushgateway安装使用

作者:互联网

PushGateway使用说明

安装

# 默认不持久化数据
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();
    }
}

查看

参考链接

标签:安装,guage,9091,job,PushGateway,使用,my,pushgateway
来源: https://blog.51cto.com/happywzy/2870584