diff --git a/status/metrics.go b/status/metrics.go index 0bda46e..ed97324 100644 --- a/status/metrics.go +++ b/status/metrics.go @@ -5,7 +5,7 @@ import ( ) // M is the Metrics instance -var M = NewMetrics() +var M = newMetrics() const ( namespace = "contentserver" @@ -17,6 +17,7 @@ const ( metricLabelError = "error" ) +// Metrics is the structure that holds all prometheus metrics type Metrics struct { ServiceRequestCounter *prometheus.CounterVec // count the number of requests for each service function ServiceRequestDuration *prometheus.SummaryVec // observe the duration of requests for each service function @@ -28,96 +29,85 @@ type Metrics struct { NumSocketsGauge *prometheus.GaugeVec // keep track of the total number of open sockets } -func NewMetrics() *Metrics { +// newMetrics can be used to instantiate a metrics instance +// since this function will also register each metric and metrics should only be registered once +// it is private +// the package exposes the initialized Metrics instance as the variable M. +func newMetrics() *Metrics { return &Metrics{ - ServiceRequestCounter: serviceRequestCounter(), - ServiceRequestDuration: serviceRequestDuration(), - UpdatesRejectedCounter: updatesRejectedCounter(), - UpdatesCompletedCounter: updatesCompletedCounter(), - UpdatesFailedCounter: updatesFailedCounter(), - UpdateDuration: updateDuration(), - ContentRequestCounter: contentRequestCounter(), - NumSocketsGauge: numSocketsGauge(), + ServiceRequestCounter: newCounterVec( + "service_request_count", + "Count of requests for each handler", + metricLabelHandler, metricLabelStatus, metricLabelSource, + ), + ServiceRequestDuration: newSummaryVec( + "service_request_duration_seconds", + "Seconds to unmarshal requests, execute a service function and marshal its reponses", + metricLabelHandler, metricLabelStatus, metricLabelSource, + ), + UpdatesRejectedCounter: newCounterVec( + "updates_rejected_count", + "Number of updates that were rejected because the queue was full", + ), + UpdatesCompletedCounter: newCounterVec( + "updates_completed_count", + "Number of updates that were successfully completed", + ), + UpdatesFailedCounter: newCounterVec( + "updates_failed_count", + "Number of updates that failed due to an error", + metricLabelError, + ), + UpdateDuration: newSummaryVec( + "update_duration_seconds", + "Duration in seconds for each successful repo.update() call", + ), + ContentRequestCounter: newCounterVec( + "num_sockets_total", + "Total number of currently open socket connections", + metricLabelRemote, + ), + NumSocketsGauge: newGaugeVec( + "content_request_count", + "Number of requests for content", + metricLabelSource, + ), } } -func serviceRequestCounter() *prometheus.CounterVec { +/* + * Metric constructors + */ + +func newSummaryVec(name, help string, labels ...string) *prometheus.SummaryVec { + vec := prometheus.NewSummaryVec( + prometheus.SummaryOpts{ + Namespace: namespace, + Name: name, + Help: help, + }, labels) + prometheus.MustRegister(vec) + return vec +} + +func newCounterVec(name, help string, labels ...string) *prometheus.CounterVec { vec := prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: namespace, - Name: "service_request_count", - Help: "Count of requests for each handler", - }, []string{metricLabelHandler, metricLabelStatus, metricLabelSource}) + Name: name, + Help: help, + }, labels) prometheus.MustRegister(vec) return vec } -func serviceRequestDuration() *prometheus.SummaryVec { - vec := prometheus.NewSummaryVec(prometheus.SummaryOpts{ - Namespace: namespace, - Name: "service_request_duration_seconds", - Help: "Seconds to unmarshal requests, execute a service function and marshal its reponses", - }, []string{metricLabelHandler, metricLabelStatus, metricLabelSource}) - prometheus.MustRegister(vec) - return vec -} - -func updatesRejectedCounter() *prometheus.CounterVec { - vec := prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: namespace, - Name: "updates_rejected_count", - Help: "Number of updates that were rejected because the queue was full", - }, []string{}) - prometheus.MustRegister(vec) - return vec -} - -func updatesCompletedCounter() *prometheus.CounterVec { - vec := prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: namespace, - Name: "updates_completed_count", - Help: "Number of updates that were successfully completed", - }, []string{}) - prometheus.MustRegister(vec) - return vec -} - -func updatesFailedCounter() *prometheus.CounterVec { - vec := prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: namespace, - Name: "updates_failed_count", - Help: "Number of updates that failed due to an error", - }, []string{metricLabelError}) - prometheus.MustRegister(vec) - return vec -} - -func updateDuration() *prometheus.SummaryVec { - vec := prometheus.NewSummaryVec(prometheus.SummaryOpts{ - Namespace: namespace, - Name: "update_duration_seconds", - Help: "Duration in seconds for each successful repo.update() call", - }, []string{}) - prometheus.MustRegister(vec) - return vec -} - -func numSocketsGauge() *prometheus.GaugeVec { - vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: namespace, - Name: "num_sockets_total", - Help: "Total number of currently open socket connections", - }, []string{metricLabelRemote}) - prometheus.MustRegister(vec) - return vec -} - -func contentRequestCounter() *prometheus.CounterVec { - vec := prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: namespace, - Name: "content_request_count", - Help: "Number of requests for content", - }, []string{metricLabelSource}) +func newGaugeVec(name, help string, labels ...string) *prometheus.GaugeVec { + vec := prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: namespace, + Name: name, + Help: help, + }, labels) prometheus.MustRegister(vec) return vec }