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

查看:564
本文介绍了如何解析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.

他们编写了许多Go库,这些库在 Prometheus 组件和库之间共享.它们被认为是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的博览会格式(链接).是的,它遵循 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

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存在于看跌期权中,这在设计上是不可接受的.因此,将 \ n 用于行尾.

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

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

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