监控

Metrics 中间件用于实现服务的性能指标监控,统计了请求耗时和请求计数。

配置

Metrics 中间件中提供了两个配置方法 WithSeconds()WithRequests()

WithSeconds()

  1. func WithSeconds(c metrics.Observer) Option {
  2. return func(o *options) {
  3. o.seconds = c
  4. }
  5. }

用于设置 metrics 中间件统计请求耗时的 Observer 直方图。

WithRequests()

  1. func WithRequests(c metrics.Counter) Option {
  2. return func(o *options) {
  3. o.requests = c
  4. }
  5. }

用于设置 metrics 中间件统计请求计数的 Counter 计数器。

使用方式 (kratos < 2.8.0)

使用 prometheus

  1. // 详见 https://github.com/go-kratos/examples/tree/main/metrics
  2. _metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  3. Namespace: "server",
  4. Subsystem: "requests",
  5. Name: "duration_sec",
  6. Help: "server requests duratio(sec).",
  7. Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.250, 0.5, 1},
  8. }, []string{"kind", "operation"})
  9. _metricRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
  10. Namespace: "client",
  11. Subsystem: "requests",
  12. Name: "code_total",
  13. Help: "The total number of processed requests",
  14. }, []string{"kind", "operation", "code", "reason"})
  15. prometheus.MustRegister(_metricSeconds, _metricRequests)

Server 中使用 metrics

  1. import (
  2. prom "github.com/go-kratos/kratos/contrib/metrics/prometheus/v2"
  3. )
  4. // grpc service
  5. grpcSrv := grpc.NewServer(
  6. grpc.Address(":9000"),
  7. grpc.Middleware(
  8. metrics.Server(
  9. metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
  10. metrics.WithRequests(prom.NewCounter(_metricRequests)),
  11. ),
  12. ),
  13. )
  14. // http service
  15. httpSrv := http.NewServer(
  16. http.Address(":8000"),
  17. http.Middleware(
  18. metrics.Server(
  19. metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
  20. metrics.WithRequests(prom.NewCounter(_metricRequests)),
  21. ),
  22. ),
  23. )
  24. httpSrv.Handle("/metrics", promhttp.Handler())

Client 中使用 metrics

  1. // grpc client
  2. conn, err := grpc.DialInsecure(
  3. context.Background(),
  4. grpc.WithEndpoint("127.0.0.1:9000"),
  5. grpc.WithMiddleware(
  6. metrics.Client(
  7. metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
  8. metrics.WithRequests(prom.NewCounter(_metricRequests)),
  9. ),
  10. ),
  11. )
  12. // http client
  13. conn, err := http.NewClient(
  14. context.Background(),
  15. http.WithEndpoint("127.0.0.1:8000"),
  16. http.WithMiddleware(
  17. metrics.Client(
  18. metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
  19. metrics.WithRequests(prom.NewCounter(_metricRequests)),
  20. ),
  21. ),
  22. )

使用方式 (kratos >= 2.8.0)

kratos 从 v2.8.0 开始使用 otel.Metrics,需要用以下方法 export 数据到 prometheus。

使用 prometheus

  1. import (
  2. "github.com/go-kratos/kratos/v2/middleware/metrics"
  3. "go.opentelemetry.io/otel/exporters/prometheus"
  4. "go.opentelemetry.io/otel/metric"
  5. sdkmetric "go.opentelemetry.io/otel/sdk/metric"
  6. )
  7. // Detailed reference https://github.com/go-kratos/examples/tree/main/metrics
  8. func init() {
  9. exporter, err := prometheus.New()
  10. if err != nil {
  11. panic(err)
  12. }
  13. provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter))
  14. meter := provider.Meter(Name)
  15. _metricRequests, err = metrics.DefaultRequestsCounter(meter, metrics.DefaultServerRequestsCounterName)
  16. if err != nil {
  17. panic(err)
  18. }
  19. _metricSeconds, err = metrics.DefaultSecondsHistogram(meter, metrics.DefaultServerSecondsHistogramName)
  20. if err != nil {
  21. panic(err)
  22. }
  23. }

Server 中使用 metrics

  1. import (
  2. "github.com/prometheus/client_golang/prometheus/promhttp"
  3. )
  4. // grpc service
  5. grpcSrv := grpc.NewServer(
  6. grpc.Address(":9000"),
  7. grpc.Middleware(
  8. metrics.Server(
  9. metrics.WithSeconds(_metricSeconds),
  10. metrics.WithRequests(_metricRequests),
  11. ),
  12. ),
  13. )
  14. // http service
  15. httpSrv := http.NewServer(
  16. http.Address(":8000"),
  17. http.Middleware(
  18. metrics.Server(
  19. metrics.WithSeconds(_metricSeconds),
  20. metrics.WithRequests(_metricRequests),
  21. ),
  22. ),
  23. )
  24. httpSrv.Handle("/metrics", promhttp.Handler())

Client 中使用 metrics

  1. // grpc client
  2. conn, err := grpc.DialInsecure(
  3. context.Background(),
  4. grpc.WithEndpoint("127.0.0.1:9000"),
  5. grpc.WithMiddleware(
  6. metrics.Client(
  7. metrics.WithSeconds(_metricSeconds),
  8. metrics.WithRequests(_metricRequests),
  9. ),
  10. ),
  11. )
  12. // http client
  13. conn, err := http.NewClient(
  14. context.Background(),
  15. http.WithEndpoint("127.0.0.1:8000"),
  16. http.WithMiddleware(
  17. metrics.Client(
  18. metrics.WithSeconds(_metricSeconds),
  19. metrics.WithRequests(_metricRequests),
  20. ),
  21. ),
  22. )

References