无法在Prometheus中看到来自Golang REST API的字符串度量输出。

huangapple go评论77阅读模式
英文:

Cannot see the string metrics output from Golang REST API in Prometheus

问题

我正在尝试使用Golang创建一个REST API,以向Prometheus提供指标。我已经有一个可用的REST API,例如:http://localhost:46743/prometheusJobs 在Swagger上测试后,API运行正常,我得到了以下的响应内容,目前只是从Prometheus文档中获取的文本格式示例。

这是我的Go函数,用于向Prometheus输出(提供)指标,目前只是一个字符串,我想先进行测试:

func GetPrometheusJobsHandler(params jobs_management.GetPrometheusJobsParams) middleware.Responder {
    ret := ""

    // TODO : 构建指标
    ret += `
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000
    `

    return jobs_management.NewGetPrometheusJobsOK().WithPayload(ret)
}

在我的Prometheus中,我成功连接到了这个API,状态显示为UP

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

而且,值显示为1

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

然而,在Prometheus的下拉菜单中,我看不到我在上面的Go函数中定义的字符串:

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

请问我做错了什么?或者这不是向Prometheus提供指标的正确方法,我必须使用Golang库(https://github.com/prometheus/client_golang/)吗?非常感谢您的帮助。

英文:

I am trying to create a REST API using Golang to provide metrics to Prometheus. I already have REST API available that is available on for example: http://localhost:46743/prometheusJobs The API works fine after testing on swagger, I get the response content as followed, for now it's just the sample of text format I got from Prometheus documentation.

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

This is my Go function to output (provide) the metrics to Prometheus, for now it's just a string as I would like to test first:

func GetPrometheusJobsHandler(params jobs_management.GetPrometheusJobsParams) middleware.Responder {
    ret := ""

    // TODO : construct metrics
    ret += `
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000
    `

    return jobs_management.NewGetPrometheusJobsOK().WithPayload(ret)
}

In my Prometheus, I am successfully connected to this API as the state shows as UP now:

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

And, the value shows as 1

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

However, I don't see the string I define as output in my Go function above in the drop down here in Prometheus:

无法在Prometheus中看到来自Golang REST API的字符串度量输出。

Could you please let me know what I did wrong? or is this NOT the right way to provide the metrics to Prometheus and I must use the Golang libraries (https://github.com/prometheus/client_golang/)? Thank you very much in advance.

答案1

得分: 1

我不认为这些是有效的Prometheus指标:

# HELP http_requests_total HTTP请求的总数。
# TYPE http_requests_total 计数器
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000

你可以尝试使用以下方式:

# HELP http_requests_total HTTP请求的总数。
# TYPE http_requests_total 计数器
http_requests_total{method="post",code="200"} 1027 
http_requests_total{method="post",code="400"} 3

使用官方的Go客户端提供这样的指标可以确保它们的有效性。

英文:

I don't think these are valid prometheus metrics:

# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000

Can you try with :

# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 
http_requests_total{method="post",code="400"} 3

Using the official go client to provide such metrics would ensure their validity

huangapple
  • 本文由 发表于 2021年5月19日 17:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/67600242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定