如何解析 Prometheus 数据 [英] How to parse Prometheus data

查看:38
本文介绍了如何解析 Prometheus 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够通过发送 HTTP GET 获取指标,如下所示:

I have been able to obtain the metrices by sending an HTTP GET as follows:

# TYPE net_conntrack_dialer_conn_attempted_total untyped net_conntrack_dialer_conn_attempted_total{dialer_name="federate",instance="localhost:9090",job="prometheus"} 1 1608520832877

现在我需要解析这些数据并获得对每条数据的控制,以便我可以像 json 一样转换 tand 格式.

Now I need to parse this data and obtain control over every piece of data so that I can convert tand format like json.

我一直在研究 Go 中的 ebnf 包:ebnf 包

I have been looking into the ebnf package in Go: ebnf package

有人能指出我解析上述数据的正确方向吗?

Can somebody point me the right direction to parse the above data?

推荐答案

已经有一个很好的软件包可以做到这一点,它由 Prometheus 的作者 自己提供.

There's a nice package already available to do that and it's by the Prometheus's Authors itself.

他们编写了一堆在 Prometheus 组件和库之间共享的 Go 库.它们被认为是 Prometheus 内部的,但您可以使用它们.

They have written a bunch of Go libraries that are shared across Prometheus components and libraries. They are considered internal to Prometheus but you can use them.

参考:github.com/prometheus/common 文档.有一个名为 expfmt 的包可以解码和编码 Prometheus's Exposition Format (链接).是的,它遵循 EBNF 语法,因此也可以使用 ebnf 包,但您可以直接使用 expfmt.

Refer: github.com/prometheus/common doc. There's a package called expfmt that can decode and encode the Prometheus's Exposition Format (Link). Yes, it follows the EBNF syntax so ebnf package could also be used but you're getting expfmt right out of the box.

使用的包: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 是您的用例的不错选择.

So, expfmt is a good choice for your use-case.

更新:OP 发布的输入中的格式问题:

Update: Formatting problem in OP's posted input:

参考:

  1. 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 '
'). Ending a line in other ways, e.g. with 
'CR' aka '
', 'CRLF' aka '
', or just the end of the packet, will
result in a protocol error.

但是从错误消息中,我可以看到 字符存在于 put 中,这是设计不可接受的.所以使用 作为行尾.

But from the error message, I could see char is present in in the put which is not acceptable by design. So use for line endings.

这篇关于如何解析 Prometheus 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆