You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to disable certain type of metrics at the moment? For example, disable request_size_bytes_bucket or just completely disable histograms for any type of metric.
In my case, i am doing a proxy type of a setup with at least 20 routes, so having histogram, bytes, etc generates tons of metrics.
Nope, requests_total, request_duration_seconds, response_size_bytes, request_size_bytes are built in.
but if you are OK not having histograms then it leaves you only with requests_total as this is counter. You could strip down the middleware to something like that and add your custom metrics if needed.
funcmyPrometheusRoute() echo.HandlerFunc {
gatherer:=prometheus.DefaultGathererh:=promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{DisableCompression: true})
ifr, ok:=gatherer.(prometheus.Registerer); ok {
h=promhttp.InstrumentMetricHandler(r, h)
}
returnfunc(c echo.Context) error {
h.ServeHTTP(c.Response(), c.Request())
returnnil
}
}
funcmyPrometheusMW() (echo.MiddlewareFunc, error) {
requestCount:=prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "",
Subsystem: "MY_APP",
Name: "requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
[]string{"code", "method", "host", "url"},
)
iferr:=prometheus.DefaultRegisterer.Register(requestCount); err!=nil {
returnnil, err
}
returnfunc(next echo.HandlerFunc) echo.HandlerFunc {
returnfunc(c echo.Context) error {
err:=next(c)
status:=c.Response().Statusiferr!=nil {
varhttpError*echo.HTTPErroriferrors.As(err, &httpError) {
status=httpError.Code
}
ifstatus==0||status==http.StatusOK {
status=http.StatusInternalServerError
}
}
url:=c.Path() // contains route path ala `/users/:id`ifurl=="" {
// as of Echo v4.10.1 path is empty for 404 cases (when router did not find any matching routes)// in this case we use actual path from request to have some distinction in Prometheusurl=c.Request().URL.Path
}
requestCount.WithLabelValues(
strconv.Itoa(status),
c.Request().Method,
c.Request().Host,
url,
).Inc()
returnerr
}
}, nil
}
Activity
aldas commentedon Feb 1, 2024
Nope,
requests_total,request_duration_seconds,response_size_bytes,request_size_bytesare built in.but if you are OK not having histograms then it leaves you only with
requests_totalas this is counter. You could strip down the middleware to something like that and add your custom metrics if needed.This is https://github.com/labstack/echo-contrib/tree/master/echoprometheus stripped down
aldas commentedon Feb 1, 2024
NB: you might need custom registry. This does not contain prometheus own default metrics
and make sure that you Route takes that registry in as gatherer
and
myPrometheusMWneeds to use that samecustomRegistryto register metricsivanovaleksandar commentedon Feb 1, 2024
@aldas thank you for providing a workaround. I replicated almost the same locally, but was wondering if there is something that I am missing here.
Anyways, is it something that you want implemented? I mean, the optional disabling of these types of metrics.