go-gin-prometheus icon indicating copy to clipboard operation
go-gin-prometheus copied to clipboard

How to use custom metrics?

Open thisroot opened this issue 6 years ago • 5 comments

Hi, i read sourse code and could not understand how to use custom metrics? Does exist way do it, or i should rewrite this library?

thisroot avatar Oct 03 '18 15:10 thisroot

Any update on this issue? It seems you can add custom metrics but the question is how to trigger them?

Thanks in advance!

nizarayari avatar Nov 19 '18 23:11 nizarayari

@nizarayari Yes, you should use gin metrics with you custom mectrics build with standart prometheus library methods. Gin prometheus library collect base metrics and bind it into common metrics report

And you will see this example

thisroot avatar Nov 20 '18 08:11 thisroot

ok.. so im a bit stumped here.

i have the custom metrics implemented (so i can see them show up when i have my server running):

image

However, even with looking with the answer provided to nizarayari, i dont see how to apply the .Inc() method to the custom metric.

Here is my code snippet: image

I have tried as well looking at the other example, however the prometheus.Register(numErrors) example never ends up showing up in the gin metrics list (as the test ones outlined above are).

any pointers would be wonderful! D

lmcdasm avatar Aug 28 '21 05:08 lmcdasm

I change MetricsList []*Metric to MetricsList map[string]*Metric, when i use the custom metrics. Make a easy way to get custom metric.

customMetrics := []*ginprometheus.Metric{
	&ginprometheus.Metric{
		ID:	"1234",				// optional string
		Name:	"test_metric",			// required string
		Description:	"Counter test metric",	// required string
		Type:	"counter",			// required string
	},
	&ginprometheus.Metric{
		ID:	"1235",				// Identifier
		Name:	"test_metric_2",		// Metric Name
		Description:	"Summary test metric",	// Help Description
		Type:	"summary", // type associated with prometheus collector
	},
	// Type Options:
	//	counter, counter_vec, gauge, gauge_vec,
	//	histogram, histogram_vec, summary, summary_vec
}
p := ginprometheus.NewPrometheus("gin", customMetrics)
// add data to custom metrics
m := p.MetricsList["1234"].MetricCollector.(prometheus.Counter)
m.Inc()

#53

snowlyg avatar Apr 26 '22 10:04 snowlyg

Thanks @snowlyg

lmcdasm avatar Jun 10 '22 04:06 lmcdasm