Munai Das Udasin
Munai Das Udasin

Reputation: 526

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))
}

Upvotes: 2

Views: 6270

Answers (1)

Marc
Marc

Reputation: 21145

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.

Upvotes: 1

Related Questions