【发布时间】:2021-10-11 07:46:00
【问题描述】:
我正在尝试将一个有效的 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
【问题讨论】:
-
内容类型不应该是
application/octet-stream吗? -
嘿@tim_yates,是吗?我也试过这个,它似乎没有做任何不同的事情。谢谢你的想法。
-
我不相信
application/x-www-form-urlencoded是对的... curl 示例没有这样做,是吗?为什么要编码为base64?不确定 curl 示例是否也这样做? -
可能也是个问题。我的理解是,这就是普罗米修斯所期待的?
-
去阅读 curl 的文档 ;-) curl.se/docs/manpage.html#--data-binary 所以 AFAIK,您的内容类型很好,但您不需要对字节进行编码,只需使用 @987654328 将它们发送到 OutputStream @
标签: java curl groovy prometheus prometheus-pushgateway