Prometheus's Authors 已经有一个很好的包可以做到这一点。
他们编写了一堆在 Prometheus 组件和库之间共享的 Go 库。它们被认为是 Prometheus 内部的,但您可以使用它们。
参考:github.com/prometheus/common 文档。有一个名为 expfmt 的包可以对 Prometheus 的 Exposition Format (Link) 进行解码和编码。是的,它遵循 EBNF 语法,因此也可以使用 ebnf 包,但您会立即获得 expfmt。
使用的包:expfmt
示例输入:
# HELP net_conntrack_dialer_conn_attempted_total
# TYPE net_conntrack_dialer_conn_attempted_total untyped
net_conntrack_dialer_conn_attempted_total{dialer_name="federate",instance="localhost:9090",job="prometheus"} 1 1608520832877
示例程序:
package main
import (
"flag"
"fmt"
"log"
"os"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
func fatal(err error) {
if err != nil {
log.Fatalln(err)
}
}
func parseMF(path string) (map[string]*dto.MetricFamily, error) {
reader, err := os.Open(path)
if err != nil {
return nil, err
}
var parser expfmt.TextParser
mf, err := parser.TextToMetricFamilies(reader)
if err != nil {
return nil, err
}
return mf, nil
}
func main() {
f := flag.String("f", "", "set filepath")
flag.Parse()
mf, err := parseMF(*f)
fatal(err)
for k, v := range mf {
fmt.Println("KEY: ", k)
fmt.Println("VAL: ", v)
}
}
样本输出:
KEY: net_conntrack_dialer_conn_attempted_total
VAL: name:"net_conntrack_dialer_conn_attempted_total" type:UNTYPED metric:<label:<name:"dialer_name" value:"federate" > label:<name:"instance" value:"localhost:9090" > label:<name:"job" value:"prometheus" > untyped:<value:1 > timestamp_ms:1608520832877 >
因此,expfmt 是您的用例的不错选择。
更新:OP 发布的输入中的格式问题:
参考:
-
https://github.com/prometheus/pushgateway/issues/147#issuecomment-368215305
-
https://github.com/prometheus/pushgateway#command-line
Note that in the text protocol, each line has to end with a line-feed
character (aka 'LF' or '\n'). Ending a line in other ways, e.g. with
'CR' aka '\r', 'CRLF' aka '\r\n', or just the end of the packet, will
result in a protocol error.
但从错误消息中,我可以看到 \r char 存在于 put 中,这是设计不可接受的。所以使用\n作为行尾。