在 Prometheus 中看不到来自 Golang REST API 的字符串指标输出

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

我正在尝试使用 Golang 创建一个 REST API 来为 Prometheus 提供指标。我已经有 REST API 可用,例如:http://localhost:46743/prometheusJobs API 在 swagger 测试后工作正常,我得到如下响应内容,现在是只是我从 Prometheus 文档中获得的文本格式示例。

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

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)
}

在我的 Prometheus 中,我已成功连接到此 API,状态现在显示为 UP

并且,该值显示为 1

但是,我在 Prometheus 的下拉列表中没有看到我在上面的 Go 函数中定义为输出的字符串:

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

我认为这些不是有效的普罗米修斯指标:

# 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

你可以试试 :

# 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

使用官方go客户端提供此类指标可确保其有效性