client_golang icon indicating copy to clipboard operation
client_golang copied to clipboard

Counters without `_total` suffixes loose metric type in Open Metrics exposition format

Open Antanukas opened this issue 4 years ago • 0 comments

When exposing counters without _total suffixes in Open Metrics exposition format metric type is lost.

Using github.com/prometheus/client_golang v1.11.0.

Code to reproduce:

package main

import (
	"fmt"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
	registry := prometheus.NewRegistry()
	c := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "counter_without_suffix"}, []string{})
	registry.MustRegister(c)

	promHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{EnableOpenMetrics: true})
	http.Handle("/metrics", promHandler)

	c.WithLabelValues().Inc()

	if err := http.ListenAndServe(":3030", nil); err != nil {
		fmt.Println(err)
	}
}

$ curl -H "Accept: application/openmetrics-text" localhost:3030/metrics
# HELP counter_without_suffix
# TYPE counter_without_suffix unknown
counter_without_suffix 1.0
# EOF

$ curl localhost:3030/metrics
# HELP counter_without_suffix
# TYPE counter_without_suffix counter
counter_without_suffix 1

Open Metrics requires these suffixes, however it does not look practical to loose metric type in different exposure format when it is known to be a counter.

Prometheus scraping logic sets Accept header to prefer Open Metrics format if client supports it.

Antanukas avatar Nov 26 '21 08:11 Antanukas