echoprometheus

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2025 License: MIT Imports: 5 Imported by: 2

README

Echo Prometheus

Middleware for echo to instrument all handlers as metrics

Example of usage

With default config
package main

import (
	"github.com/webx-top/echo"
	"github.com/webx-top/echo/engine/standard"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	echoPrometheus "github.com/webx-top/echo-prometheus"
)

func main() {
	e := echo.New()

	e.Use(echoPrometheus.MetricsMiddleware())
	e.Get("/metrics", echo.WrapHandler(promhttp.Handler()))

	e.Get("/", func(c echo.Context) error {
		return c.String("Hello, World!")
	})
	e.Logger().Fatal(e.Run(standard.New(":1323")))
}
With custom config
package main

import (
	"github.com/webx-top/echo"
	"github.com/webx-top/echo/engine/standard"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	echoPrometheus "github.com/webx-top/echo-prometheus"
)

func main() {
	e := echo.New()

	var configMetrics = echoPrometheus.NewConfig()
		configMetrics.Namespace = "namespace"
		configMetrics.Buckets = []float64{
			0.0005, // 0.5ms
			0.001,  // 1ms
			0.005,  // 5ms
			0.01,   // 10ms
			0.05,   // 50ms
			0.1,    // 100ms
			0.5,    // 500ms
			1,      // 1s
			2,      // 2s
	}

	e.Use(echoPrometheus.MetricsMiddlewareWithConfig(configMetrics))
	e.Get("/metrics", echo.WrapHandler(promhttp.Handler()))

	e.Get("/", func(c echo.Context) error {
		return c.String("Hello, World!")
	})
	e.Logger().Fatal(e.Run(standard.New(":1323")))
}
Gzip middleware

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

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

Example output for metric route

# HELP echo_http_request_duration_seconds Spend time by processing a route
# TYPE echo_http_request_duration_seconds histogram
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.0005"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.001"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.002"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.005"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.01"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.02"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.05"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.1"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.2"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="0.5"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="1"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="2"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="5"} 7
echo_http_request_duration_seconds_bucket{handler="/",method="GET",le="+Inf"} 7
echo_http_request_duration_seconds_sum{handler="/",method="GET"} 7.645099999999999e-05
echo_http_request_duration_seconds_count{handler="/",method="GET"} 7
# HELP echo_http_requests_total Number of HTTP operations
# TYPE echo_http_requests_total counter
echo_http_requests_total{handler="/",method="GET",status="2xx"} 7
View metrics via Grafana

We built a grafana dashboard for these metrics, lookup at

1. Echo Framework Process Dashboard:

本地位置:./grafana/tsuru-echo-framework-processes_rev1.json

在线位置:https://grafana.com/grafana/dashboards/10913

2. Go Process Dashboard:

本地位置:./grafana/go-processes_rev2.json

在线位置:https://grafana.com/grafana/dashboards/6671

使用方式

第一步:下载和启动 prometheus
  1. 下载 prometheus

下载地址:https://prometheus.io/download/

下载命令举例:

wget https://github.com/prometheus/prometheus/releases/download/v2.20.0/prometheus-2.20.0.linux-amd64.tar.gz

解压:

tar -zxvf prometheus-2.20.0.linux-amd64.tar.gz
  1. 启动 prometheus

如果可执行文件prometheus没有加入到环境变量PATH中,则执行:

cd prometheus-2.20.0.linux-amd64
./prometheus --config.file=$GOPATH/src/github.com/webx-top/echo-prometheus/prometheus/prometheus.yml

如果可执行文件prometheus已经加入到环境变量PATH中,则执行:

cd $GOPATH/src/github.com/webx-top/echo-prometheus/prometheus
prometheus
第二步:下载和启动 grafana
  1. 下载 grafana

下载地址:https://grafana.com/grafana/download

  1. 启动 grafana
grafana-server --config=/usr/local/etc/grafana/grafana.ini --homepath /usr/local/share/grafana cfg:default.paths.logs=/usr/local/var/log/grafana cfg:default.paths.data=/usr/local/var/lib/grafana cfg:default.paths.plugins=/usr/local/var/lib/grafana/plugins
第三步:配置 grafana
一、配置数据源

点击路径:Configuration -> Data Sources -> Add data source -> 选择“Prometheus”:

  1. 配置:Settings

    HTTP:

    URL -> 填写:http://localhost:9090
    Access -> 选择:Server(default)
    点击“Save & Test“按钮

  2. 安装:Dashboards

    点击“Dashboards”选项卡,安装所有项目

二、导入模版

点击路径:Create -> Import -> 点击“Upload .json file”按钮

分别选择本目录下grafana文件夹中的json文件进行上传,并在数据源中选择刚刚配置的数据源,然后点击“Import”按钮进行导入。

- END -

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	Skipper: func(c echo.Context) bool {
		return c.Request().URL().Path() == c.Echo().Prefix()+`/metrics`
	},
	Namespace: "echo",
	Subsystem: "http",
	Buckets: []float64{
		0.0005,
		0.001,
		0.002,
		0.005,
		0.01,
		0.02,
		0.05,
		0.1,
		0.2,
		0.5,
		1.0,
		2.0,
		5.0,
		10.0,
		15.0,
		20.0,
		30.0,
	},
	NormalizeHTTPStatus: true,
	OnlyRoutePath:       true,
}

DefaultConfig has the default instrumentation config

Functions

func MetricsMiddleware

func MetricsMiddleware() echo.MiddlewareFuncd

MetricsMiddleware returns an echo middleware with default config for instrumentation.

func MetricsMiddlewareWithConfig

func MetricsMiddlewareWithConfig(config Config) echo.MiddlewareFuncd

MetricsMiddlewareWithConfig returns an echo middleware for instrumentation.

Types

type Config

type Config struct {
	Skipper             echo.Skipper
	Namespace           string
	Buckets             []float64
	Subsystem           string
	NormalizeHTTPStatus bool
	OnlyRoutePath       bool
	StatisticalSize     bool
}

Config responsible to configure middleware

func NewConfig

func NewConfig() Config

NewConfig returns a new config with default values

Directories

Path Synopsis
example
customConfig command
defaultConfig command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL