From 43028076cd3704d625996f3cb8063fe405a5f3b2 Mon Sep 17 00:00:00 2001 From: "helder.junior" Date: Thu, 25 Jul 2024 15:40:59 -0300 Subject: [PATCH] Remove Server metrics from client --- metrics/metrics.go | 121 --------------------------------------------- 1 file changed, 121 deletions(-) diff --git a/metrics/metrics.go b/metrics/metrics.go index c63b239..e38039a 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -24,46 +24,10 @@ package metrics import ( "errors" - "github.com/spf13/viper" - "net/http" - "time" - - log "github.com/sirupsen/logrus" - - "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( - // APIResponseTime summary, observes the API response time as perceived by the server - APIResponseTime *prometheus.HistogramVec - - // APIPayloadSize summary, observes the payload size of requests arriving at the server - APIPayloadSize *prometheus.HistogramVec - - // APIRequestsSuccessCounter counter - APIRequestsSuccessCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "eventsgateway", - Subsystem: "api", - Name: "requests_success_counter", - Help: "A counter of succeeded api requests", - }, - []string{"route", "topic", "retry"}, - ) - - // APIRequestsFailureCounter counter - APIRequestsFailureCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "eventsgateway", - Subsystem: "api", - Name: "requests_failure_counter", - Help: "A counter of failed api requests", - }, - []string{"route", "topic", "retry", "reason"}, - ) - // ClientRequestsResponseTime summary, observes the API response time as perceived by the client ClientRequestsResponseTime *prometheus.HistogramVec @@ -97,33 +61,8 @@ var ( }, []string{"topic"}, ) - - APITopicsSubmission = prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: "eventsgateway", - Subsystem: "api", - Name: "topics_submission_total", - Help: "Topic submissions sent to kafka", - }, - []string{"topic", "success"}, - ) ) -func defaultLatencyBuckets(config *viper.Viper) []float64 { - // in milliseconds - const configKey = "prometheus.buckets.latency" - config.SetDefault(configKey, []float64{3, 5, 10, 50, 100, 300, 500, 1000, 5000}) - - return config.Get(configKey).([]float64) -} - -func defaultPayloadSizeBuckets(config *viper.Viper) []float64 { - // in bytes - configKey := "prometheus.buckets.payloadSize" - config.SetDefault(configKey, []float64{100, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 5000000}) - - return config.Get(configKey).([]float64) -} - // RegisterMetrics is a wrapper to handle prometheus.AlreadyRegisteredError; // it only returns an error if the metric wasn't already registered and there was an // actual error registering it. @@ -140,63 +79,3 @@ func RegisterMetrics(collectors []prometheus.Collector) error { return nil } - -// StartServer runs a metrics server inside a goroutine -// that reports default application metrics in prometheus format. -// Any errors that may occur will stop the server and log.Fatal the error. -func StartServer(config *viper.Viper) { - APIPayloadSize = prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Namespace: "eventsgateway", - Subsystem: "api", - Name: "payload_size", - Help: "payload size of API routes, in bytes", - Buckets: defaultPayloadSizeBuckets(config), - }, - []string{"route", "topic"}, - ) - - APIResponseTime = prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Namespace: "eventsgateway", - Subsystem: "api", - Name: "response_time_ms", - Help: "the response time in ms of api routes", - Buckets: defaultLatencyBuckets(config), - }, - []string{"route", "topic", "retry"}, - ) - - collectors := []prometheus.Collector{ - APIResponseTime, - APIPayloadSize, - APIRequestsFailureCounter, - APIRequestsSuccessCounter, - APITopicsSubmission, - } - - err := RegisterMetrics(collectors) - if err != nil { - log.Fatal(err) - } - - go func() { - envEnabled := config.GetString("prometheus.enabled") - if envEnabled != "true" { - log.Warn("Prometheus web server disabled") - return - } - - r := mux.NewRouter() - r.Handle("/metrics", promhttp.Handler()) - - s := &http.Server{ - Addr: config.GetString("prometheus.port"), - ReadTimeout: 8 * time.Second, - WriteTimeout: 8 * time.Second, - MaxHeaderBytes: 1 << 20, - Handler: r, - } - log.Fatal(s.ListenAndServe()) - }() -}