cleaning functionalities, adding arguments at start
This commit is contained in:
@@ -3,6 +3,9 @@ package prometheus
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Metrics structure for storing counter
|
||||
@@ -12,11 +15,36 @@ type Metrics struct {
|
||||
}
|
||||
|
||||
var (
|
||||
//HTTPCounter Metrics
|
||||
HTTPCounter = InitializeHTTPReqCounter(Reg)
|
||||
Reg = prometheus.NewRegistry()
|
||||
// HTTPCounter Metrics
|
||||
HTTPCounter = initializeHTTPReqCounter()
|
||||
// Reg is a custom registry to have more control on metrics exported.
|
||||
Reg = prometheus.NewRegistry()
|
||||
// MetricsEnabled is the flag used to enable the prometheus metrics backend.
|
||||
MetricsEnabled = false
|
||||
)
|
||||
|
||||
// InitMetrics enable the metrics functionality if the flags is passed as an argument
|
||||
func InitMetrics(m bool) {
|
||||
if m {
|
||||
MetricsEnabled = true
|
||||
initPrometheusRegistry()
|
||||
// Define custom promhttp handler that expose just our custom registry.
|
||||
http.Handle("/metrics", promhttp.HandlerFor(Reg, promhttp.HandlerOpts{
|
||||
EnableOpenMetrics: true,
|
||||
Registry: Reg,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
// InitPrometheusRegistry initialize registry and counters if metrics flag pass as argument.
|
||||
func initPrometheusRegistry() {
|
||||
// We use a custom registry to better now what metrics are exposed.
|
||||
Reg = prometheus.NewRegistry()
|
||||
Reg.MustRegister(HTTPCounter.ClientHTTPReqs)
|
||||
Reg.MustRegister(HTTPCounter.BackendHTTPReqs)
|
||||
Reg.MustRegister(collectors.NewBuildInfoCollector())
|
||||
}
|
||||
|
||||
// IncrementClientCounterVec increments the counter with method label provided.
|
||||
func IncrementClientCounterVec(m string) {
|
||||
HTTPCounter.ClientHTTPReqs.WithLabelValues(m).Inc()
|
||||
@@ -30,7 +58,7 @@ func IncrementBackendCounterVec(m string) {
|
||||
}
|
||||
|
||||
// InitializeHTTPReqCounter inits the httpReqs counter that will be exported.
|
||||
func InitializeHTTPReqCounter(reg prometheus.Registerer) *Metrics {
|
||||
func initializeHTTPReqCounter() *Metrics {
|
||||
HTTPCounters := &Metrics{
|
||||
ClientHTTPReqs: prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "client_http_requests_total",
|
||||
|
||||
Reference in New Issue
Block a user