zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.31k stars 3.96k forks source link

Is it possible to customize metricServerReqDur buckets ? #3667

Open akalittle opened 1 year ago

akalittle commented 1 year ago

In the rest/handler/prometheushandler.go file

const serverNamespace = "http_server"

var (
    metricServerReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
        Namespace: serverNamespace,
        Subsystem: "requests",
        Name:      "duration_ms",
        Help:      "http server requests duration(ms).",
        Labels:    []string{"path", "method"},
        Buckets:   []float64{5, 10, 25, 50, 100, 250, 500, 1000},
    })

The biggest of bucket is 1000, however, in the service we use, we need to request third-party service, which may last more than 1s. So the metrics are always a straightness line....

But if I write a customized middleware like:

func (m *PromMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        startTime := timex.Now()
        defer func() {
            prometheus.MetricServerReqDur.WithLabelValues(
                m.App,
                r.URL.Path, r.Method).Observe(float64(timex.Since(startTime) / time.Millisecond))
        }()

        next(w, r)
    }
}

It works. But if a request URL is /resource/:id, there will be too many metrics like resource/1 resource/2. Not stored as resource/:id

fynxiu commented 1 year ago

Regarding the customization of Prometheus buckets in go-zero, adding a buckets option in prometheus.Config seems like a feasible feature. For an immediate workaround, consider these steps:

  1. Disable go-zero's Built-In Prometheus Handler: This prevents conflicts with the default metrics collection.

  2. Implement a Custom Handler: Implement your handler on the built-in one but with your desired bucket sizes for the Prometheus histogram.

This is a personal suggestion and may need testing in your environment. For a long-term solution, you might want to raise a feature request on the go-zero repository.

github-actions[bot] commented 5 days ago

This issue is stale because it has been open for 30 days with no activity.