Reputation: 1
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,){}
Upvotes: 0
Views: 1633
Reputation: 31691
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,
)
}
}
Upvotes: 0