Either I am missing something, or the current implementation of prometheus handler will not capture any code returned from handlers as it will trigger only at the beginning of the request.
package main
import (
"fmt"
"net/http"
"time"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const listenAddress = "localhost:1323"
func main() {
e := echo.New()
// Enable metrics middleware
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/404", func(c echo.Context) error {
return c.String(http.StatusNotFound, "404")
})
e.GET("/500", func(c echo.Context) error {
return c.String(http.StatusInternalServerError, "oops")
})
go test()
e.Logger.Fatal(e.Start(listenAddress))
}
func test() {
time.Sleep(time.Second * 1)
urls := []string{
"/",
"/404",
"/500",
}
for _, url := range urls {
resp, err := http.Get(fmt.Sprintf("http://%s%s", listenAddress, url))
if err != nil {
fmt.Printf("Error %s\n", err)
continue
}
_ = resp.Body.Close()
}
}
{"time":"2019-12-19T16:42:28.748008Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"Go-http-client/1.1","status":200,"error":"","latency":14464,"latency_human":"14.464µs","bytes_in":0,"bytes_out":13}
{"time":"2019-12-19T16:42:28.749981Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/404","user_agent":"Go-http-client/1.1","status":404,"error":"","latency":6046,"latency_human":"6.046µs","bytes_in":0,"bytes_out":3}
{"time":"2019-12-19T16:42:28.75144Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/500","user_agent":"Go-http-client/1.1","status":500,"error":"","latency":3108,"latency_human":"3.108µs","bytes_in":0,"bytes_out":4}
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/"} 1
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/404"} 1
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/500"} 1