在groovy中向prometheus pushgateway发布度量失败,并返回400

gr8qqesn  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(161)

我正在尝试将一个可用的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命令之间,我错过了什么会收到这个错误?

EDIT:我正在添加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
nhjlsmyf

nhjlsmyf1#

在进一步修改代码后,似乎根本不需要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
nqwrtyyt

nqwrtyyt2#

我在http状态码400上也遇到了问题,直到我在度量的末尾添加了一个换行符。
之后,我的电话工作。我不知道你的“三重引用”在一天结束时是什么样子的,但我怀疑你和我有同样的问题。

相关问题