简体   繁体   中英

Prometheus metrics not showing up using prometheus go client

I am using prometheus golang client. The code snippet is below. The build for the same is working okay.

The issue is that only go metrics are shown. xyz_* metrics are missing. I call the initMetrics() as first thing in the main() func.

// Declaring prometheus metric counters
var (

  metric_prefix = "xyz_"

  xyzAPICallsCounter = prometheus.NewCounterVec(
    prometheus.CounterOpts{
      Name: metric_prefix + "api_calls_total" ,
      Help: "Number of calls to xyz endpoint",
    },
    []string{
      // Type of api call. Present values 
      "type",
      // Method can be "add", "delete", "getall", "get", "create", "ensure"
      "method",
      // Status is success or failed
      "status",
    },
  )

)

    func initMetrics(){
    prometheus.MustRegister(xyzAPICallsCounter)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":8080", nil)
    }

Edit: I changed the initMetrics() function to below and now there are no metrics at all and the below error message.

func initMetrics(){
  var registry = prometheus.NewRegistry()
  registry.MustRegister(
    xyzAPICallsCounter,
  )
  http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
  log.Fatal(http.ListenAndServe(":8080", nil))
}

CounterVec is a collection of counters and is not exported until it has counters in it.

See the code docs and example for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM