Creating registry and using middleware on all HTTP endpoints --> no metrics shown on prometheus instance

Hi all,

I have an existing Go cloud service that I’m trying to add instrumentation to. My plan was to use middleware to add instrumentation to every endpoint in one fell swoop instead of adding instrumentation to every endpoint one at a time. I’ve created the following middleware function that I thought was supposed to wrap all the endpoints with the created metrics:

var (
	inFlightGauge = prometheus.NewGauge(prometheus.GaugeOpts{
		Name: "in_flight_requests",
		Help: "A gauge of requests currently being served by the wrapped handler.",
	})

	counter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "api_requests_total",
			Help: "A counter for requests to the wrapped handler.",
		},
		[]string{"method", "code"},
	)

	duration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "request_duration_seconds",
			Help:    "A histogram of latencies for requests.",
			Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
		},
		[]string{"handler"},
	)
)

func prometheusMiddleware(next http.Handler) http.Handler {
	return promhttp.InstrumentHandlerInFlight(inFlightGauge,
		promhttp.InstrumentHandlerDuration(duration.MustCurryWith(prometheus.Labels{"handler": "api"}),
			promhttp.InstrumentHandlerCounter(counter, next),
		),
	)
}

I’ve added the following registry and middleware to my NewRouter() function:

	// Create non-global registry.
	registry := prometheus.NewRegistry()
	// Add go runtime metrics and process collectors.
	registry.MustRegister(
		// collectors.NewGoCollector(),
		// collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
		inFlightGauge,
		counter,
		duration,
	)

	router.Use(
		middleware.RealIP, // True-Client-IP, X-Real-IP or the X-Forwarded-For support
		chizerolog.LoggerMiddleware(&logger),
		nethttp.OpenAPIMiddleware(apiSchema),          // Provides OpenAPI schema
		request.DecoderMiddleware(decoderFactory),     // request decoder (from JSON)
		request.ValidatorMiddleware(validatorFactory), // validation of requests/schema
		response.EncoderMiddleware,                    // encoding of responses
		gzip.Middleware,                               // support for gzip
		// Add Prometheus metrics middleware
		prometheusMiddleware,
	)

	router.Method(http.MethodGet, "/api/v1/docs/openapi", apiSchema)
	// router.Method(http.MethodGet, "/metrics", promhttp.Handler())
	router.Handle("/metrics", promhttp.Handler())

        return router

When I run my service, I am able to confirm that the middleware is being used for every request, but I am unable to find those metrics when I go to my prometheus instance (localhost:XXXX/graphs)…

Any help would be appreciated. Thank you!