title: Prometheus type Collector - how to feed maps with my own data
body: <p>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.</p>
  
  <p>The goal is:
  As per the 'Example' section under Prometheus Golang module (<a href="https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector" rel="nofollow noreferrer">https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector</a>) and the part where it's mentioned "// Just example fake data." to use my own real data of course.</p>
  
  <p>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.</p>
  
  <p>Suppose my map looks as follows:
  [
  "device1" : 754,
  "device2" : 765,
  ]</p>
  
  <p>For the code, let's follow the original example.</p>
  
  <pre><code>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&lt;- *prometheus.Desc) {
      prometheus.DescribeByCollect(cc, ch)
  }
  
  func (cc ClusterManagerCollector) Collect(ch chan&lt;- prometheus.Metric) {
      oomCountByHost, ramUsageByHost := cc.ClusterManager.ReallyExpensiveAssessmentOfTheSystemState()
      for host, oomCount := range oomCountByHost {
          ch &lt;- prometheus.MustNewConstMetric(
              oomCountDesc,
              prometheus.CounterValue,
              float64(oomCount),
              host,
          )
      }
      for host, ramUsage := range ramUsageByHost {
          ch &lt;- prometheus.MustNewConstMetric(
              ramUsageDesc,
              prometheus.GaugeValue,
              ramUsage,
              host,
          )
      }
  }
  
  func NewClusterManager(zone string, reg prometheus.Registerer) *ClusterManager {
      c := &amp;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, &amp;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")
      &lt;-forever
  }
  
  //relevant structs go here for parsing JSON
  </code></pre>
  
  <p>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,){}</p>