项目作者: globocom

项目描述 :
Middleware for echo v4 to instrument all handlers as metrics
高级语言: Go
项目地址: git://github.com/globocom/echo-prometheus.git
创建时间: 2019-09-24T12:37:40Z
项目社区:https://github.com/globocom/echo-prometheus

开源协议:MIT License

下载


Echo Prometheus

Middleware for echo v4 to instrument all handlers as metrics

Example of usage

With default config

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo/v4"
  5. "github.com/prometheus/client_golang/prometheus/promhttp"
  6. echoPrometheus "github.com/globocom/echo-prometheus"
  7. )
  8. func main() {
  9. e := echo.New()
  10. e.Use(echoPrometheus.MetricsMiddleware())
  11. e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
  12. e.GET("/", func(c echo.Context) error {
  13. return c.String(http.StatusOK, "Hello, World!")
  14. })
  15. e.Logger.Fatal(e.Start(":1323"))
  16. }

With custom config

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo/v4"
  5. "github.com/prometheus/client_golang/prometheus/promhttp"
  6. echoPrometheus "github.com/globocom/echo-prometheus"
  7. )
  8. func main() {
  9. e := echo.New()
  10. var configMetrics = echoPrometheus.NewConfig()
  11. configMetrics.Namespace = "namespace"
  12. configMetrics.Buckets = []float64{
  13. 0.0005, // 0.5ms
  14. 0.001, // 1ms
  15. 0.005, // 5ms
  16. 0.01, // 10ms
  17. 0.05, // 50ms
  18. 0.1, // 100ms
  19. 0.5, // 500ms
  20. 1, // 1s
  21. 2, // 2s
  22. }
  23. e.Use(echoPrometheus.MetricsMiddlewareWithConfig(configMetrics))
  24. e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
  25. e.GET("/", func(c echo.Context) error {
  26. return c.String(http.StatusOK, "Hello, World!")
  27. })
  28. e.Logger.Fatal(e.Start(":1323"))
  29. }

Gzip middleware

A middleware skipper can be passed to avoid gzip /metrics URL:

  1. e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
  2. Skipper: func(c echo.Context) bool {
  3. return c.Path() == "/metrics"
  4. },
  5. }))

Example output for metric route

  1. # HELP echo_http_request_duration_seconds Spend time by processing a route
  2. # TYPE echo_http_request_duration_seconds histogram
  3. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.0005"} 7
  4. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.001"} 7
  5. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.002"} 7
  6. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.005"} 7
  7. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.01"} 7
  8. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.02"} 7
  9. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.05"} 7
  10. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.1"} 7
  11. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.2"} 7
  12. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.5"} 7
  13. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="1"} 7
  14. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="2"} 7
  15. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="5"} 7
  16. echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="+Inf"} 7
  17. echo_http_request_duration_seconds_sum{handler="/",method="GET"} 7.645099999999999e-05
  18. echo_http_request_duration_seconds_count{handler="/",method="GET"} 7
  19. # HELP echo_http_requests_total Number of HTTP operations
  20. # TYPE echo_http_requests_total counter
  21. echo_http_requests_total{handler="/",method="GET",status="2xx"} 7

Tests

Run the tests

  1. make test

View metrics via Grafana

We built a grafana dashboard for these metrics, lookup at https://grafana.com/grafana/dashboards/10913.