如何用Java/Python/Postman调用带Snappy压缩的Prometheus remote_write API
Prometheus Remote Write API的Snappy压缩调用示例
当调用Prometheus的/api/v1/write接口写入数据时出现「snappy: corrupt input」错误,本质是远程接收器要求请求体必须经过Snappy压缩,直接发送明文文本不符合要求。以下是Python、Java和Postman的实现方案:
Python 示例
依赖python-snappy处理压缩,requests发送HTTP请求:
- 先安装依赖包:
pip install requests python-snappy
- 代码实现:
import requests import snappy # 待写入的Prometheus指标内容 metric_content = 'metrics_app_20221201091829_0000_driver_BlockManager_memory_diskSpaceUsed_MB_Number{type="gauges"} 0\n' # 对文本进行Snappy压缩 compressed_data = snappy.compress(metric_content.encode('utf-8')) # 发送POST请求 request_url = 'http://<prometheus-host>/api/v1/write' headers = { 'Content-Type': 'application/x-protobuf', 'Content-Encoding': 'snappy' } response = requests.post(request_url, headers=headers, data=compressed_data) print(f"响应状态码: {response.status_code}") print(f"响应内容: {response.text}")
Java 示例
使用org.xerial.snappy做Snappy压缩,OkHttp处理HTTP请求:
- Maven依赖配置:
<dependencies> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency> <dependency> <groupId>org.xerial.snappy</groupId> <artifactId>snappy-java</artifactId> <version>1.1.10.1</version> </dependency> </dependencies>
- 代码实现:
import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import org.xerial.snappy.Snappy; import java.io.IOException; public class PrometheusRemoteWriter { public static void main(String[] args) throws IOException { // 指标文本内容 String metricText = "metrics_app_20221201091829_0000_driver_BlockManager_memory_diskSpaceUsed_MB_Number{type=\"gauges\"} 0\n"; byte[] rawBytes = metricText.getBytes("UTF-8"); // 执行Snappy压缩 byte[] compressedBytes = Snappy.compress(rawBytes); // 构建并发送请求 OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-protobuf"); RequestBody body = RequestBody.create(compressedBytes, mediaType); Request request = new Request.Builder() .url("http://<prometheus-host>/api/v1/write") .post(body) .addHeader("Content-Encoding", "snappy") .build(); try (Response response = client.newCall(request).execute()) { System.out.println("响应状态码: " + response.code()); System.out.println("响应内容: " + response.body().string()); } } }
Postman 操作步骤
- 新建POST请求,填入目标URL:
http://<prometheus-host>/api/v1/write - 在Headers标签页添加两个请求头:
Content-Type:application/x-protobufContent-Encoding:snappy
- 切换到Body标签页,选择
binary类型,点击Select File - 提前将明文指标文本通过Snappy压缩为二进制文件(可借助Python脚本生成:读取文本文件后用
snappy.compress()处理,再写入新文件) - 选择压缩后的二进制文件,点击「发送」即可
注意事项
- 必须设置
Content-Encoding: snappy请求头,明确告知服务器请求体的压缩方式 - 部分远程接收器要求
Content-Type为application/x-protobuf,而非原始的text/plain,需根据实际配置调整
内容的提问来源于stack exchange,提问作者Shane
相关产品推荐
相关产品推荐

