Monitoring

The framework doesn’t provide any monitoring, but only provides a Tracer interface. This interface can be implemented by yourself and be injected via WithTracer Option.

// Tracer is executed at the start and finish of an HTTP.
type Tracer interface {
    Start(ctx context.Context, c *app.RequestContext) context.Context
    Finish(ctx context.Context, c *app.RequestContext)
}

hertz-contrib provides a default prometheus monitoring extension,which can be used to do:

  • Server throughput monitoring
  • Request latency monitoring

The default tags include: HTTP Method, statusCode. Requests related information are stored in RequestContext, and this variable can be accessed after monitoring metrics scraped. You can implement and extend the monitoring functions according to your own requirements. Usage example:

Server

import (
  "context"

  "github.com/cloudwego/hertz/pkg/app"
  "github.com/cloudwego/hertz/pkg/app/server"
  "github.com/cloudwego/hertz/pkg/common/utils"
  "github.com/hertz-contrib/monitor-prometheus"
  )

func main() {
    ···
    h := server.Default(server.WithTracer(prometheus.NewServerTracer(":9091", "/hertz")))
    h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
      ctx.JSON(200, utils.H{"ping": "pong"})
    })
    h.Spin()
    ···
}

Currently, Client doesn’t expose Tracer interface, but monitoring capabilities can be implemented through middleware.

Related Repository https://github.com/hertz-contrib/monitor-prometheus


Last modified July 18, 2023 : feat: shortcodes codetab (#709) (6996e7c)