Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to customize metricServerReqDur buckets ? #3667

Closed
akalittle opened this issue Oct 24, 2023 · 3 comments
Closed

Is it possible to customize metricServerReqDur buckets ? #3667

akalittle opened this issue Oct 24, 2023 · 3 comments
Labels
area/metrics Categorizes issue or PR as related to metrics. feature Marks an issue or PR as related to a new feature. stale

Comments

@akalittle
Copy link

akalittle commented Oct 24, 2023

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

@kevwan kevwan added area/metrics Categorizes issue or PR as related to metrics. feature Marks an issue or PR as related to a new feature. labels Oct 25, 2023
@fynxiu
Copy link
Contributor

fynxiu commented Nov 12, 2023

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.

Copy link

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

@github-actions github-actions bot added the stale label Nov 12, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/metrics Categorizes issue or PR as related to metrics. feature Marks an issue or PR as related to a new feature. stale
Projects
None yet
Development

No branches or pull requests

3 participants