title: Adding label to golang prometheus collector
body: <p>I'm trying to figure out how to add a label to a prometheus collector. Any ideas what I'm missing here? I have two files: main.go and collector.go</p>
  
  <p>I used the following link as a guide. <a href="https://rsmitty.github.io/Prometheus-Exporters/" rel="noreferrer">https://rsmitty.github.io/Prometheus-Exporters/</a></p>
  
  <p>I mocked up this example, so I could post it here. I'm ultimately not going to pull "date +%s" for the command. Just can't figure out where to add labels.</p>
  
  <p>For the label I'm trying to add a hostname, so I have a result like:</p>
  
  <pre><code># HELP cmd_result Shows the cmd result
  # TYPE cmd_result gauge
  cmd_result{host="my_device_hostname"} 1.919256141363144e-76
  </code></pre>
  
  <p>I'm also really new to golang, so there is a good chance I'm going about this all wrong! I'm ultimately trying to get prometheus to pull the cmd result on each scrape.</p>
  
  <p><strong>main.go</strong></p>
  
  <pre><code>package main
  
  import (
      "net/http"
  
      log "github.com/Sirupsen/logrus"
      "github.com/prometheus/client_golang/prometheus"
      "github.com/prometheus/client_golang/prometheus/promhttp"
  )
  
  func main() {
  
      //Create a new instance of the collector and
      //register it with the prometheus client.
      cmd := newCollector()
      prometheus.MustRegister(cmd)
  
      //This section will start the HTTP server and expose
      //any metrics on the /metrics endpoint.
      http.Handle("/metrics", promhttp.Handler())
      log.Info("Beginning to serve on port :8080")
      log.Fatal(http.ListenAndServe(":8080", nil))
  }
  </code></pre>
  
  <p><strong>collector.go</strong></p>
  
  <pre><code>package main
  
  import (
      "encoding/binary"
      "fmt"
      "math"
      "os/exec"
      "strings"
  
      "github.com/prometheus/client_golang/prometheus"
  )
  
  type cmdCollector struct {
      cmdMetric *prometheus.Desc
  }
  
  func newCollector() *cmdCollector {
      return &amp;cmdCollector{
          cmdMetric: prometheus.NewDesc("cmd_result",
              "Shows the cmd result",
              nil, nil,
          ),
      }
  }
  
  func (collector *cmdCollector) Describe(ch chan&lt;- *prometheus.Desc) {
      ch &lt;- collector.cmdMetric
  }
  
  func (collector *cmdCollector) Collect(ch chan&lt;- prometheus.Metric) {
  
      var metricValue float64
      command := string("date +%s")
      cmdResult := exeCmd(command)
      metricValue = cmdResult
  
      ch &lt;- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue)
  
  }
  
  func exeCmd(cmd string) float64 {
      parts := strings.Fields(cmd)
      out, err := exec.Command(parts[0], parts[1]).Output()
      if err != nil {
          fmt.Println("error occured")
          fmt.Printf("%s", err)
      }
      cmdProcessResult := Float64frombytes(out)
      return cmdProcessResult
  }
  
  func Float64frombytes(bytes []byte) float64 {
      bits := binary.LittleEndian.Uint64(bytes)
      float := math.Float64frombits(bits)
      return float
  }
  </code></pre>