package main
import (
"context"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
prometheus "github.com/hertz-contrib/monitor-prometheus"
)
func main() {
//...
h := server.Default(
server.WithHostPorts("127.0.0.1:8080"),
server.WithTracer(
prometheus.NewServerTracer(":9091", "/hertz"),
),
)
h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) {
ctx.String(200, "hello get")
})
h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) {
time.Sleep(100 * time.Millisecond)
ctx.String(200, "hello post")
})
h.Spin()
}
-
install docker and start docker
-
change $inetIP to local ip in line 30 of prometheus.yml
-
run Prometheus and Grafana
docker-compose up
ordocker compose up
-
run Hertz server
go run main.go
-
run Hertz client
go run client/main.go
-
visit
http://localhost:3000
, the account password isadmin
by default -
configure Prometheus data sources
Connections
Data sources
Add new data source
- Select
Prometheus
and fill the URL withhttp://prometheus:9090
- click
Save & test
after configuration to test if it works
-
add dashboard
Dashboards
->New
->New dashboard
, add monitoring metrics such as throughput and pct99 according to your needs, for example:- server throughput of succeed requests
sum(rate(hertz_server_throughput{statusCode="200"}[1m])) by (method)
- server latency pct99 of succeed requests
histogram_quantile(0.9,sum(rate(hertz_server_latency_us_bucket{statusCode="200"}[1m]))by(le))
For more information about hertz monitoring, please click monitoring