java - 在 groovy 中将指标发布到 prometheus pushgateway 失败,出现 400
问题描述
我正在尝试将工作 curl 命令转换为 groovy,因此我可以在我的 Jenkins 管道中本地执行它。我的问题是 curl 命令有效,但是我的 groovy 请求失败,来自 prometheus 的 pushgateway 的 400 错误。我在 groovy 中的代码如下所示:
def post = new URL("https://prometheus_url").openConnection();
def message = '''
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
''';
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
post.with {
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
writer << message.bytes.encodeBase64().toString()
}
println content.text
}
assert post.responseCode == 200
我收到的错误是
Caught: java.io.IOException: Server returned HTTP response code: 400 for URL: https://prometheus_url
java.io.IOException: Server returned HTTP response code: 400 for URL: https://prometheus_url
我试图复制的工作 curl 命令如下
cat <<EOF | curl --data-binary @- 'https://prometheus_url' -vvv
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
所以我的问题是,我的 groovy 代码和 curl 命令之间缺少什么来接收这个错误?
编辑:我正在从工作 curl 操作中添加详细文本
cat <<EOF | curl --data-binary @- 'https://prometheus_url' -vvv
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
* Trying X.X.X.X...
* TCP_NODELAY set
* Connected to prometheus_url (X.X.X.X) port XYZ (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / --------------------
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: CN=--------------------
* start date: Jun 17 00:00:00 2021 GMT
* expire date: Jul 16 23:59:59 2022 GMT
* subjectAltName: host "prometheus_url" matched cert's "----------------"
* issuer: C=--; O=--------; OU=-------- CA 1B; CN=--------
* SSL certificate verify ok.
> POST ------------------------ HTTP/1.1
> Host: prometheus_url
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
> Referer:
> Content-Length: 147
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 147 out of 147 bytes
< HTTP/1.1 200 OK
< Date: Fri, 06 Aug 2021 13:18:39 GMT
< Content-Length: 0
< Connection: keep-alive
<
* Connection #0 to host prometheus_url left intact
* Closing connection 0
解决方案
在进一步摆弄代码之后,似乎根本不需要base64编码。因此,更简化和工作的代码如下:
def post = new URL("https://prometheus_url").openConnection();
def message = """
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
""";
post.setRequestMethod("POST");
post.setDoOutput(true);
post.getOutputStream().write(message.bytes);
assert post.responseCode == 200
推荐阅读
- javascript - 无法在 CSS 中定位正文
- reactjs - 尝试将 react-hook-form 与 react-input mask 结合使用
- angular - ionic 5 - ngFor 不显示数据
- c - 信号量未在初始化行初始化
- python - 带有文本输入的 Kivi 黑屏
- r - 无法在 R 3.6.3 中安装包“Liszt”(不在 CRAN 中)
- c - 使用节点地址在链表中进行循环检测
- python-3.x - 如果列数据相同,如何合并行并在 pandas 中有效地更改合并列上另一个特定列的值?
- python - 为什么对谷歌语音到文本 api 的异步 long_running_recognize 调用的 Progress_percent 仅返回 0 和 100(完成后)?
- c# - 使用 Entity Framework 计算列通过存储过程获取多个结果集