作者:wujianhong61391 | 来源:互联网 | 2023-06-03 19:46
大致思路先写下,prometheus.yml加个配置项,远程写是调用http请求来写的,数据格式是protobuf(一种自定义的编码格式),编码格式是snappy(一种压缩格式);远程写通过snappy先压缩,然后protobuf编码的字节数组请求的;接收到远程写数据时是乱码,先用snappy进行解压缩,prometheus官网文档远程写提供remote.proto(包含编码和解码),remote.proto文件中依赖了types.proto和gogo.proto两个文件,我是在prometheus源码的包里边找到的,所以需要把这三个protobuf文件生成java文件,调用Remote.WriteRequest.parseFrom来解码
一、protobuf转java
1. 下载Protocol Buffers
地址:https://github.com/protocolbuffers/protobuf/releases
Mac解压,如图:
2. 把remote.proto、types.proto、gogo.proto三个文件copy的bin下面 如图:
3. protoc生成java文件
protoc -I=src_dir --java_out=dst_dir src_dir/xxx.proto
如图三个protobuf文件生成三个java文件:
二、prometheus.yml配置
远程写地址
emote_write:- url: "http://localhost:8080/receive"# 基本认证 这是每个远程写请求的授权头属性basic_auth:username: prometheus password: prometheus
三、java代码(三个java文件copy到项目中)
package com.jyh.prome;
import com.jyh.prome.protobuf.Remote;
import com.jyh.prome.protobuf.Types;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xerial.snappy.Snappy;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;@RestController
public class RemoteWrite {&#64;RequestMapping(value &#61; "/receive")public void write(&#64;RequestBody byte[] body, HttpServletRequest request) throws IOException, InterruptedException {byte[] a &#61; Snappy.uncompress(body);List<Types.TimeSeries> timeSeriesList &#61; Remote.WriteRequest.parseFrom(a).getTimeseriesList();for (Types.TimeSeries timeSeries : timeSeriesList) {List<Types.Sample> samples &#61; timeSeries.getSamplesList();List<Types.Label> labels &#61; timeSeries.getLabelsList();Map<String, Object> map &#61; new LinkedHashMap<>();for (Types.Label label : labels) {map.put(label.getName(), label.getValue());}for (Types.Sample sample : samples) {map.put("timestamp", sample.getTimestamp());map.put("value", sample.getValue());}System.out.println(map);}}}
labels {name: "__name__"value: "mysql_global_variables_query_cache_min_res_unit"
}
labels {name: "instance"value: "192.168.31.40:9101"
}
labels {name: "job"value: "ser4"
}
samples {value: 4096.0timestamp: 1601196751516
}