Prometheus的remotewrite for java

 1、增加参数重启prometheus

 修改 prometheus启动参数

Prometheus的remotewrite for java_第1张图片

- "--enable-feature=remote-write-receiver"

重启 prometheus

2、下载案例

GitHub - bprasen/remotewrite

Prometheus的remotewrite for java_第2张图片

3、迁移

将案例中的代码复制到springboot/springcloud中 

Prometheus的remotewrite for java_第3张图片

pom中增加




    com.google.protobuf
    protobuf-java
    3.11.1




    com.google.protobuf
    protobuf-java-util
    3.11.1




    org.xerial.snappy
    snappy-java
    1.1.8.4


    org.json
    json
    20200518


    com.github.wnameless
    json-flattener
    0.2.2

4、测试

import com.itl.iap.kafka.datacollect.provider.GCKafkaCollectProviderApplication;
import com.itl.iap.kafka.datacollect.provider.prometheus.MetricPoint;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import com.itl.iap.kafka.datacollect.provider.prometheus.MetricPoint;
import com.itl.iap.kafka.datacollect.provider.prometheus.PrometheusService;
import com.itl.iap.kafka.datacollect.provider.prometheus.TasksConfig;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@ActiveProfiles("dev")
@SpringBootTest
@ContextConfiguration(classes = GCKafkaCollectProviderApplication.class)
@Slf4j
public class PrometheusTest {

    @Autowired
    private PrometheusService prometheusService;

    @Test
    public void reportTest() throws IOException {
        Map tags = new HashMap<>();
        tags.put("a", "b1");
        tags.put("c", "d1");
        MetricPoint mp = new MetricPoint();
        TasksConfig.Item conf = new TasksConfig.Item();
        conf.setLabels(tags);
        conf.setMetric("metric_testAB");
        mp.setTime(System.currentTimeMillis() - 60*1000);
        mp.setValue(56);
        mp.setConf(conf);

        Map tags2 = new HashMap<>();
        tags2.put("aa", "bb1");
        tags2.put("cc", "dd1");
        MetricPoint mp2 = new MetricPoint();
        TasksConfig.Item conf2 = new TasksConfig.Item();
        conf2.setLabels(tags2);
        conf2.setMetric("metric_testAB");
        mp2.setValue(150);
        mp.setTime(System.currentTimeMillis());
        mp2.setConf(conf2);
        prometheusService.remoteWrite(Lists.newArrayList(mp, mp2));
    }
}

Prometheus的remotewrite for java_第4张图片

参考

prometheus remote write for springboot 远程写入<一>-pudn.com

GitHub - bprasen/remotewrite

你可能感兴趣的:(告警夜莺n9e,java,prometheus)