doutu1939 2019-01-28 10:32
浏览 287

Prometheus Type Collector-如何使用我自己的数据来提供地图

Disclaimer: I am new to Golang and haven't done much programming in any other language before. I still hope someone can point me in the right direction though.

The goal is: As per the 'Example' section under Prometheus Golang module (https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector) and the part where it's mentioned "// Just example fake data." to use my own real data of course.

My data comes in JSON format from a RabbitMQ endpoint. I parse JSON and I can create my own maps with the right key:value that I need as part of a goroutine under the func main() scope.

Suppose my map looks as follows: [ "device1" : 754, "device2" : 765, ]

For the code, let's follow the original example.

package main

import (
    "log"
    "net/http"
    "fmt"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

type ClusterManager struct {
    Zone string
    // Contains many more fields not listed in this example.
}

func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() (
    oomCountByHost map[string]int, ramUsageByHost map[string]float64,
) {
        // Just example fake data.
        oomCountByHost = map[string]int{
            "foo.example.org": 42,
            "bar.example.org": 2001,
        }
        ramUsageByHost = map[string]float64{
            "foo.example.org": 6.023e23,
            "bar.example.org": 3.14,
        }
        return
}

type ClusterManagerCollector struct {
    ClusterManager *ClusterManager
}

var (
    oomCountDesc = prometheus.NewDesc(
        "clustermanager_oom_crashes_total",
        "Number of OOM crashes.",
        []string{"host"}, nil,
    )
    ramUsageDesc = prometheus.NewDesc(
        "clustermanager_ram_usage_bytes",
        "RAM usage as reported to the cluster manager.",
        []string{"host"}, nil,
    )
)

func (cc ClusterManagerCollector) Describe(ch chan<- *prometheus.Desc) {
    prometheus.DescribeByCollect(cc, ch)
}

func (cc ClusterManagerCollector) Collect(ch chan<- prometheus.Metric) {
    oomCountByHost, ramUsageByHost := cc.ClusterManager.ReallyExpensiveAssessmentOfTheSystemState()
    for host, oomCount := range oomCountByHost {
        ch <- prometheus.MustNewConstMetric(
            oomCountDesc,
            prometheus.CounterValue,
            float64(oomCount),
            host,
        )
    }
    for host, ramUsage := range ramUsageByHost {
        ch <- prometheus.MustNewConstMetric(
            ramUsageDesc,
            prometheus.GaugeValue,
            ramUsage,
            host,
        )
    }
}

func NewClusterManager(zone string, reg prometheus.Registerer) *ClusterManager {
    c := &ClusterManager{
        Zone: zone,
    }
    cc := ClusterManagerCollector{ClusterManager: c}
    prometheus.WrapRegistererWith(prometheus.Labels{"zone": zone}, reg).MustRegister(cc)
    return c
}

func main() {
    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    failOnError(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare("hello", false, false, false, false, nil)
    failOnError(err, "Failed to declare a queue")

    msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil)
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

    go func() {
        for d := range msgs {
            var streams []byte
            streams = d.Body

            var metrics sStreamingMetrics
            err := json.Unmarshal(streams, &metrics)
            if err != nil {
                fmt.Println(err)
            }

            var category string
            category = metrics.Resource.Category

            if category == "server" {
               myMap := make(map[string]float64)
               MyMap [metrics.Resource.ResourceDataList[0].ResourceId] = metrics.Resource.ResourceDataList[0].MetricSampleList[0].ValueArray[0]
             }
    }()

    reg := prometheus.NewPedanticRegistry()

    NewClusterManager("zone", reg)

    reg.MustRegister(
        prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
        prometheus.NewGoCollector(),
    )

    http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
    log.Fatal(http.ListenAndServe(":8080", nil))
    log.Printf(" [*] Waiting for logs. To exit press CTRL+C")
    <-forever
}

//relevant structs go here for parsing JSON

Added the goroutine and the main for a bigger picture. There's concurency as I'll be receiving data from time to time. I think the skill I am missing here is how to call the function so that key:values from myMap go into func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() ( oomCountByHost map[string]int, ramUsageByHost map[string]float64,){}

  • 写回答

1条回答 默认 最新

  • douyan4243 2019-01-28 18:07
    关注

    I think the skill I am missing here is how to call the function so that key:values from myMap go into func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() ( oomCountByHost map[string]int, ramUsageByHost map[string]float64,){}

    ReallyExpensiveAssessmentOfTheSystemState is called in the Collect method. Your map doesn't "go into" that method, it is returned by ReallyExpensiveAssessmentOfTheSystemState.

    Simply move the code from the goroutine into ReallyExpensiveAssessmentOfTheSystemState:

    func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() map[string]float64 {
        myMap := make(map[string]float64)
        myMap["device1"] = 754
        myMap["device2"] = 765
    
        return myMap
    }
    
    func (cc ClusterManagerCollector) Collect(ch chan<- prometheus.Metric) {
        values := cc.ClusterManager.ReallyExpensiveAssessmentOfTheSystemState()
    
        for key, value := range values {
            ch <- prometheus.MustNewConstMetric(
                valueDesc,
                prometheus.CounterValue,
                value,
                key,
            )
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写