Echo: using prometheus.Handler()

Created on 22 Nov 2015  路  6Comments  路  Source: labstack/echo

Hi,

i'd like to use prometheus (https://github.com/prometheus/prometheus) to see some stats.
usin the golang lib it should be pretty easy (github.com/prometheus/client_golang)

    // from the working sample
    http.Handle("/metrics", prometheus.Handler())
    http.ListenAndServe(*addr, nil)

Unfortunately the prometheus.Handler() returns a http.Handler which doesn't work with echo.
So this doesn' work (flate: read error at offset 5: unexpected EOF, but should be a text)

    e.Any("/metrics", prometheus.Handler())
    e.Run("0.0.0.0:3000")

diving in these are the internals

// prometheus.Handler() --> 
func Handler() http.Handler {
    return InstrumentHandler("prometheus", defRegistry)
}
// InstrumentHandler(...) ->
func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
    return InstrumentHandlerFunc(handlerName, handler.ServeHTTP)
}
// InstrumentHandlerFunc(...) ->
func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
    return InstrumentHandlerFuncWithOpts(
        SummaryOpts{
            Subsystem:   "http",
            ConstLabels: Labels{"handler": handlerName},
        },
        handlerFunc,
    )
}
// InstrumentHandlerFuncWithOpts () ->
func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
...
}

any idea what i could do?
I'm using echo for the first time

invalid

Most helpful comment

This works:
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

All 6 comments

Below code works for me, please re-open the issue, if needed.

package main

import (
    "github.com/labstack/echo"
    "github.com/prometheus/client_golang/prometheus"
)

func main() {
    e := echo.New()
    e.Get("/metrics", prometheus.Handler())
    e.Run(":8080")
}

Thank you!
You helped me find the problem.

It is

e.Use(mw.Gzip())

prometheus doesn't seem to like it.
hm, I still would like to use gzip for everything but prometheus.
Do you have an idea?

You can use groups and separate middleware. Look into documentation and test files.

For echo v2:

e.GET("/metrics", fasthttp.WrapHandler(
    fasthttpadaptor.NewFastHTTPHandler(prometheus.Handler()),
))

Just bumped into this issue. Updating provided here information with up-to-date state:

  • It seems that prometheus.Handler() is not recommended anymore (see here). Use promhttp.Handler() instead. Note that promhttp is NOT instrumented (does not have default, useful metrics regarding HTTP requests like size, duration or count). If you want them, see InstrumentHandlerDuration example.

This works:
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ellisonleao picture ellisonleao  路  3Comments

linux-support picture linux-support  路  3Comments

wangxianzhuo picture wangxianzhuo  路  4Comments

absinsekt picture absinsekt  路  4Comments

alexzorin picture alexzorin  路  3Comments