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
Hello. I'm new to Go. I was wondering if there is a way to add custom labels to the standard prometheus metrics that are collected by the echo middleware.
I have tried to do add middleware using the CurryWith(Labels) API on respective MetricVec classes.
I run into this error when the middleware gets run.
2 unknown label(s) found during currying
I've attached some sample code of what I'm trying to accomplish. My end goal is adding my two custom labels to all metrics being generated.
Appreciate the time.
Checklist
Dependencies installed
No typos
Searched existing issues and docs
Expected behaviour
Actual behaviour
Steps to reproduce
Working code to debug
varerrorCount=&prometheus.Metric{
ID: "err_cnt",
Name: "error_count",
Description: "Error count",
Type: "counter_vec",
Args: []string{"code", "method", "host", "url", "myLabel1", "myLabel2"},
}
typeMyMetricsstruct {
errorCount*prometheus.Metric// my own custom defined metricstandardMetrics []*prometheus.Metric// populated with prometheus.MetricsList
}
func (metrics*MyMetrics) addLabels() {
labels:= ptheus.Labels{"myLabel1": "value1", "myLabel2": "value2"}
errorCount.MetricCollector.(*ptheus.CounterVec).With(labels)
// attempt to loop over standard metrics and add my custom labelsfori:=0; i<len(metrics.standardMetrics); i++ {
standardMetric:=metrics.standardMetrics[i]
ifstandardMetric.MetricCollector!=nil {
switchstandardMetric.Type {
case"histogram_vec":
curriedMetric, err:=standardMetric.MetricCollector.(*ptheus.GaugeVec).CurryWith(labels)
ifcurriedMetric!=nil {
standardMetric.MetricCollector=curriedMetric
} elseiferr!=nil {
fmt.Printf("Histogram Error %s with metric named %s\n", err.Error(), standardMetric.Name)
}
case"counter_vec":
standardMetric.MetricCollector.(*ptheus.CounterVec).With(labels)
curriedMetric, err:=standardMetric.MetricCollector.(*ptheus.GaugeVec).CurryWith(labels)
curriedMetric, err:=standardMetric.MetricCollector.(*ptheus.CounterVec).CurryWith(labels)
ifcurriedMetric!=nil {
standardMetric.MetricCollector=curriedMetric
} elseiferr!=nil {
fmt.Printf("Counter Error %s\n", err.Error())
}
.....
fmt.Printf("Type of %s metric is %s\n", standardMetric.Name, standardMetric.Type)
}
}
}
// middleware function that I've attached to my echo instancefunc (metrics*MyMetrics) customMiddleware(nextecho.HandlerFunc) echo.HandlerFunc {
returnfunc(c echo.Context) error {
metrics.addLabels()
returnnext(c)
}
}
funccreateMetricsServer(e*echo.Echo, portstring) error {
echoPrometheus:=echo.New()
echoPrometheus.HideBanner=trueechoPrometheus.HidePort=truecustomMetricsList:= []*prometheus.Metric{
errorCount,
}
prom:=prometheus.NewPrometheus("myService", urlSkipper, customMetricsList)
myMetrics:=&MyMetrics{
errorCount: errorCount,
standardMetrics: prom.MetricsList,
}
// Middleware that replaces the values of uuids in pathnames for more consistent triaging and metricsprom.RequestCounterURLLabelMappingFunc=func(c echo.Context) string {
url:=c.Request().URL.Pathfor_, p:=rangec.ParamNames() {
ifp=="uuid" {
url=strings.Replace(url, c.Param(p), ":uuid", 1)
} elseifp=="attachmentUuid" {
url=strings.Replace(url, c.Param(p), ":attachmentUuid", 1)
}
}
returnurl
}
e.Use(myMetrics.customMiddleware)
e.Use(prom.HandlerFunc)
prom.SetMetricsPath(echoPrometheus)
// Starts a new goroutine to manage the metrics endpointgofunc() { echoPrometheus.Logger.Fatal(echoPrometheus.Start(port)) }()
log.Info().Msgf("Prometheus Metrics established started on localhost%s", port)
returnnil
}
Version/commit
The text was updated successfully, but these errors were encountered:
Issue Description
Hello. I'm new to Go. I was wondering if there is a way to add custom labels to the standard prometheus metrics that are collected by the echo middleware.
I have tried to do add middleware using the CurryWith(Labels) API on respective MetricVec classes.
I run into this error when the middleware gets run.
2 unknown label(s) found during currying
I've attached some sample code of what I'm trying to accomplish. My end goal is adding my two custom labels to all metrics being generated.
Appreciate the time.
Checklist
Expected behaviour
Actual behaviour
Steps to reproduce
Working code to debug
Version/commit
The text was updated successfully, but these errors were encountered: