英文:
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
:
而且,值显示为1
然而,在Prometheus的下拉菜单中,我看不到我在上面的Go函数中定义的字符串:
请问我做错了什么?或者这不是向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.
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:
And, the value shows as 1
However, I don't see the string I define as output in my Go function above in the drop down here in Prometheus:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论