:::info 支持。 :::

设置支持指标记录

  1. // see internal/router/router.go
  2. ...
  3. mux, err := core.New(logger,
  4. ...
  5. core.WithRecordMetrics(metrics.RecordHandler(logger)),
  6. ...
  7. )
  8. // core.WithRecordMetrics() 表示已设置指标记录
  9. // metrics.RecordHandler 表示具体指标记录的实现,目前是 prometheus 指标记录

指标记录逻辑

  1. // see internal/metrics/metrics.go
  2. // RecordHandler 指标处理
  3. func RecordHandler(logger *zap.Logger) func(msg *proposal.MetricsMessage) {
  4. if logger == nil {
  5. panic("logger required")
  6. }
  7. return func(msg *proposal.MetricsMessage) {
  8. RecordMetrics(
  9. msg.Method,
  10. msg.Path,
  11. msg.IsSuccess,
  12. msg.HTTPCode,
  13. msg.BusinessCode,
  14. msg.CostSeconds,
  15. msg.TraceID,
  16. )
  17. }
  18. }

记录 prometheus 指标

  1. // see internal/metrics/prometheus.go
  2. // metricsRequestsTotal metrics for request total 计数器(Counter)
  3. var metricsRequestsTotal = prometheus.NewCounterVec(
  4. prometheus.CounterOpts{
  5. Namespace: namespace,
  6. Subsystem: subsystem,
  7. Name: "requests_total",
  8. Help: "request(ms) total",
  9. },
  10. []string{"method", "path"},
  11. )
  12. // metricsRequestsCost metrics for requests cost 累积直方图(Histogram)
  13. var metricsRequestsCost = prometheus.NewHistogramVec(
  14. prometheus.HistogramOpts{
  15. Namespace: namespace,
  16. Subsystem: subsystem,
  17. Name: "requests_cost",
  18. Help: "request(ms) cost milliseconds",
  19. },
  20. []string{"method", "path", "success", "http_code", "business_code", "cost_milliseconds", "trace_id"},
  21. )
  22. func init() {
  23. prometheus.MustRegister(metricsRequestsTotal, metricsRequestsCost)
  24. }
  25. // RecordMetrics 记录指标
  26. func RecordMetrics(method, path string, success bool, httpCode, businessCode int, costSeconds float64, traceId string) {
  27. metricsRequestsTotal.With(prometheus.Labels{
  28. "method": method,
  29. "path": path,
  30. }).Inc()
  31. metricsRequestsCost.With(prometheus.Labels{
  32. "method": method,
  33. "path": path,
  34. "success": cast.ToString(success),
  35. "http_code": cast.ToString(httpCode),
  36. "business_code": cast.ToString(businessCode),
  37. "cost_milliseconds": cast.ToString(costSeconds * 1000),
  38. "trace_id": traceId,
  39. }).Observe(costSeconds)
  40. }

prometheus 指标数据的收集地址

http://127.0.0.1:9999/metrics

当时笔记

本地启动服务

启用 promethus

  1. $ prometheus --config.file=./deploy/prometheus/prometheus.yml

访问地址:http://127.0.0.1:9090

启动 grafana

  1. $ brew services start grafana

访问地址:http://127.0.0.1:3000
账号密码:admin/123456

PromQL 示例

Counter 指标数据可以让用户方便的了解事件产生的速率的变化,在 PromQL 内置的相关操作函数可以提供相应的分析,比如以 HTTP 应用请求量来进行说明:

  1. //通过 rate() 函数,获取 5 分钟内 HTTP 请求量的增长率
  2. rate(xinliangnote_go_gin_api_requests_total[5m])
  3. //通过 topk() 函数,获取访问量前 10 的 HTTP 地址
  4. topk(10, xinliangnote_go_gin_api_requests_total)

Histogram 指标,可以通过 histogram_quantile() 函数计算出其值的分位数,举例:

  1. histogram_quantile(0.95, sum(rate(xinliangnote_go_gin_api_requests_cost_bucket[5m])) by (le))

image.png

image.png

文档

Prometheus 中文文档:https://yunlzheng.gitbook.io/prometheus-book/
Prometheus 英文文档:https://prometheus.io/docs/introduction/overview/