首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析文件中的prometheus指标并更新计数器

解析文件中的prometheus指标并更新计数器
EN

Stack Overflow用户
提问于 2021-01-12 18:58:02
回答 1查看 506关注 0票数 0

我有一个由批处理定期运行的go应用程序。每次运行时,它应该从文件中读取一些prometheus指标,运行其逻辑,更新成功/失败计数器,并将指标写回文件。

通过查看How to parse Prometheus datagodocs for prometheus,我能够读入文件,但我不知道如何使用expfmt.ExtractSamples()返回的值更新app_processed_total

这就是我到目前为止所做的。有人能告诉我我该怎么做吗?如何将获取的向量类型转换为CounterVec

代码语言:javascript
复制
package main

import (
    "fmt"
    "net/http"
    "strings"
    "time"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    dto "github.com/prometheus/client_model/go"
    "github.com/prometheus/common/expfmt"
    "github.com/prometheus/common/model"
)

var (
    fileOnDisk     = prometheus.NewRegistry()
    processedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
        Name: "app_processed_total",
        Help: "Number of times ran",
    }, []string{"status"})
)

func doInit() {
    prometheus.MustRegister(processedTotal)
}

func recordMetrics() {
    go func() {
        for {
            processedTotal.With(prometheus.Labels{"status": "ok"}).Inc()
            time.Sleep(5 * time.Second)
        }
    }()
}

func readExistingMetrics() {
    var parser expfmt.TextParser
    text := `
# HELP app_processed_total Number of times ran
# TYPE app_processed_total counter
app_processed_total{status="ok"} 300
`
    parseText := func() ([]*dto.MetricFamily, error) {
        parsed, err := parser.TextToMetricFamilies(strings.NewReader(text))
        if err != nil {
            return nil, err
        }
        var result []*dto.MetricFamily
        for _, mf := range parsed {
            result = append(result, mf)

        }
        return result, nil
    }

    gatherers := prometheus.Gatherers{
        fileOnDisk,
        prometheus.GathererFunc(parseText),
    }

    gathering, err := gatherers.Gather()
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("gathering: ", gathering)
    for _, g := range gathering {
        vector, err := expfmt.ExtractSamples(&expfmt.DecodeOptions{
            Timestamp: model.Now(),
        }, g)

        fmt.Println("vector: ", vector)
        if err != nil {
            fmt.Println(err)
        }

        // How can I update processedTotal with this new value?
    }

}

func main() {
    doInit()
    readExistingMetrics()
    recordMetrics()

    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:2112", nil)
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-12 22:22:27

我相信你需要使用processedTotal.WithLabelValues("ok").Inc()或者类似的东西。

更完整的例子在这里

代码语言:javascript
复制
func ExampleCounterVec() {
    httpReqs := prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
        },
        []string{"code", "method"},
    )
    prometheus.MustRegister(httpReqs)

    httpReqs.WithLabelValues("404", "POST").Add(42)

    // If you have to access the same set of labels very frequently, it
    // might be good to retrieve the metric only once and keep a handle to
    // it. But beware of deletion of that metric, see below!
    m := httpReqs.WithLabelValues("200", "GET")
    for i := 0; i < 1000000; i++ {
        m.Inc()
    }
    // Delete a metric from the vector. If you have previously kept a handle
    // to that metric (as above), future updates via that handle will go
    // unseen (even if you re-create a metric with the same label set
    // later).
    httpReqs.DeleteLabelValues("200", "GET")
    // Same thing with the more verbose Labels syntax.
    httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})
}

这张图片来自Github上的Promethus examples

要使用vector的值,可以执行以下操作:

代码语言:javascript
复制
    vectorFloat, err := strconv.ParseFloat(vector[0].Value.String(), 64)
    if err != nil {
        panic(err)
    }

    processedTotal.WithLabelValues("ok").Add(vectorFloat)

这是假设你在响应中只会得到一个向量值。向量的值存储为字符串,但您可以使用strconv.ParseFloat方法将其转换为浮点数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65682589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档