Custom Prometheus Exporter

I am trying to create a custom prometheus exporter. I have been able to successfully send the metrics over to my port and all but how do I “overwrite” the previous metric with a new one if I am looking to update it. I am making an API GET request to retrieve a number, that is the number im passing on into the metric. This number may change a minute later, how do i update that metric. Ive been looking everywhere and I get the answer of using MustNewConstMetric in my Update() method… I have no idea how to go about this. Can someone please shed some light on this. Below is what i have so far

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidwall/gjson"
)

var productionTotal = prometheus.NewGauge(prometheus.GaugeOpts{
	Name: "production_total", Help: ""})

func init() {
	prometheus.MustRegister(productionTotal)
	productionTotal.Set(getTotal("production"))
}

func getTotal(x string) float64 {
	URL := fmt.Sprintf("apigetrequest")
	res, err := http.Get(URL)
	if err != nil {
		panic(err.Error())
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err.Error())
	}
	bodyString := string(body)
	value := gjson.Get(bodyString, "result.total_count")
	return float64(value.Num)
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.