Monitoring

Health Check

Default integrated health check for go-zero enabled service (RPC Server or HTTP Server). The default health check port is6470default Path is/healthzand access to health check address will return on service startupOK

  1. curl -i http://127.0.0.1:6470/healthz
  2. HTTP/1.1 200 OK
  3. Date: Thu, 09 Mar 2023 02:34:17 GMT
  4. Content-Length: 2
  5. Content-Type: text/plain; charset=utf-8
  6. OK

Disable health check configuration below:

  1. srv := rest.MustNewServer(rest.RestConf{
  2. Port: 8080,
  3. ServiceConf: service.ServiceConf{
  4. DevServer: devserver.Config{
  5. Enabled: true,
  6. },
  7. },
  8. })

Configuration turned on:

  1. Name: user-api
  2. Host: 0.0.0.0
  3. Port: 8002
  4. ......
  5. DevServer:
  6. Enabled: true

You can modify your health check ports and Path by configuring to 8080 and ping

  1. srv := rest.MustNewServer(rest.RestConf{
  2. Port: 8080,
  3. ServiceConf: service.ServiceConf{
  4. DevServer: devserver.Config{
  5. Enabled: true,
  6. Port: 8080,
  7. HealthPath: "/ping",
  8. },
  9. },
  10. })

Configuration turned on:

  1. Name: user-api
  2. Host: 0.0.0.0
  3. Port: 8002
  4. ......
  5. DevServer:
  6. Enabled: true
  7. Port: 8080
  8. HealthPath: "/ping"

Log collection

go-zero integrated link tracking based onOpenTelemetryconfigured below:

  1. type Config struct {
  2. Name string `json:",optional"`
  3. Endpoint string `json:",optional"` // trace信息上报的url
  4. Sampler float64 `json:",default=1.0"` // 采样率
  5. Batcher string `json:",default=jaeger,options=jaeger|zipkin|otlpgrpc|otlphttp"`
  6. }

go-zero link tracking support (Jaeger\zipkin) can only be enabled by adding a few lines to the configuration. No code needs to be modified. Example:

1)api配置

  1. Name: user-api
  2. Host: 0.0.0.0
  3. Port: 8002
  4. Mode: dev
  5. ......
  6. Telemetry:
  7. Name: user-api
  8. Endpoint: http://jaeger:14268/api/traces
  9. Sampler: 1.0
  10. Batcher: jaeger

2) rpc configuration

  1. Name: user-rpc
  2. ListenOn: 0.0.0.0:9002
  3. Mode: dev
  4. .....
  5. Telemetry:
  6. Name: user-rpc
  7. Endpoint: http://jaeger:14268/api/traces
  8. Sampler: 1.0
  9. Batcher: jaeger

For more examples, refer to tracing Examples

Merics monitoring

The prometheus indicator in go-zero will be collected by default, with the default port 6470 and the default Path will be /metrics, after starting the service indicator monitoring address below:

  1. $ curl http://127.0.0.1:6470/metrics
  2. # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
  3. # TYPE go_gc_duration_seconds summary
  4. go_gc_duration_seconds{quantile="0"} 7.3427e-05
  5. go_gc_duration_seconds{quantile="0.25"} 7.3427e-05
  6. go_gc_duration_seconds{quantile="0.5"} 7.3427e-05
  7. go_gc_duration_seconds{quantile="0.75"} 7.3427e-05
  8. go_gc_duration_seconds{quantile="1"} 7.3427e-05
  9. go_gc_duration_seconds_sum 7.3427e-05
  10. go_gc_duration_seconds_count 1
  11. # HELP go_goroutines Number of goroutines that currently exist.
  12. # TYPE go_goroutines gauge
  13. go_goroutines 12

Monitor Path with EnableMetrics disabled; change the indicator by MetricsPath below:

  1. srv := rest.MustNewServer(rest.RestConf{
  2. Port: 8080,
  3. ServiceConf: service.ServiceConf{
  4. DevServer: devserver.Config{
  5. Enabled: true,
  6. Port: 6470,
  7. MetricsPath: "/metrics",
  8. EnableMetrics: false,
  9. },
  10. },
  11. })

Prometheus indicators for go-zero default integration are below:

RPC Server

Metric nameLabelDescription
rpc_server_requests_duration_msmethodHistogram, time-consuming statistical unit milliseconds
rpc_server_requests_code_totalmethod、codeCounter, Error Code Statistics

RPC Client

Metric nameLabelDescription
rpc_client_requests_duration_msmethodHistogram, time-consuming statistical unit milliseconds
rpc_client_requests_code_totalmethod、codeCounter, Error Code Statistics

HTTP Server

Metric nameLabelDescription
http_server_requests_duration_mspathHistogram, time-consuming statistical unit milliseconds
http_server_requests_code_totalpath、codeCounter, Error Code Statistics

Mysql

Metric nameLabelDescription
sql_client_requests_duration_mscommandHistogram, time-consuming statistical unit milliseconds
sql_client_requests_error_totalcommand、errorCounter, Error Code Statistics

Redis

Metric nameLabelDescription
redis_client_requests_duration_mscommandHistogram, time-consuming statistical unit milliseconds
redis_client_requests_error_totalcommand、errorCounter, Error Code Statistics

Custom monitoring metrics

In the package go-zero/core/metric the type of data for the Histograms, Counter, Gauge adequate prometheus indicator is available. Users can customize business indicators using the method provided under this package, such as using Counters to measure the total number of users accessing a resource, defined below:

  1. userVisitResourceTotal = metric.NewCounterVec(&metric.CounterVecOpts{
  2. Namespace: "user",
  3. Subsystem: "visit_resource",
  4. Name: "total",
  5. Help: "user visit resource count.",
  6. Labels: []string{"user_id", "resource_id"},
  7. })
  8. userVisitResourceTotal.Inc("userId", "resourceId")