From 473171f504a6cf90665ae8881ac9bc4cf975e434 Mon Sep 17 00:00:00 2001 From: Mickael Stanislas Date: Tue, 22 Oct 2024 09:22:25 +0200 Subject: [PATCH] fix: fix --- docs/advanced/metrics.md | 2 +- internal/metrics/common.go | 36 ++++++++++++++----------- internal/metrics/metrics.go | 23 ++++++++++------ tools/doc/generate-doc.go | 53 ++----------------------------------- 4 files changed, 38 insertions(+), 76 deletions(-) diff --git a/docs/advanced/metrics.md b/docs/advanced/metrics.md index f3b89c3..ad62184 100644 --- a/docs/advanced/metrics.md +++ b/docs/advanced/metrics.md @@ -36,7 +36,7 @@ The following metrics are exposed: | kimup_events_triggerd_error_total | The total number of events triggered with error. | | kimup_events_triggered_duration | The duration in seconds of events triggered. | | kimup_events_triggered_total | The total number of events triggered. | -| kimup_registry_request_duration | | +| kimup_registry_request_duration | The duration in seconds of registry evaluated. | | kimup_registry_request_error_total | The total number of registry evaluated with error. | | kimup_registry_request_total | The total number of registry evaluated. | | kimup_rules_evaluated_duration | The duration in seconds of rules evaluated. | diff --git a/internal/metrics/common.go b/internal/metrics/common.go index 046a026..953b348 100644 --- a/internal/metrics/common.go +++ b/internal/metrics/common.go @@ -133,12 +133,12 @@ func newGauge(name, help string) prometheus.Gauge { // Help: The description help text of the gauge // Labels: The labels of the gauge func newGaugeWithVec(name, help string, labels []string) *prometheus.GaugeVec { - if Metrics[MetricTypeGaugeVec] == nil { - Metrics[MetricTypeGaugeVec] = make(map[string]interface{}) + if Metrics[MetricTypeGauge] == nil { + Metrics[MetricTypeGauge] = make(map[string]interface{}) } // Add the gauge to the map - Metrics[MetricTypeGaugeVec][name] = MetricGaugeVec{ + Metrics[MetricTypeGauge][name] = MetricGaugeVec{ // Create the metricBase metricBase: metricBase{ Name: name, @@ -151,7 +151,7 @@ func newGaugeWithVec(name, help string, labels []string) *prometheus.GaugeVec { }, labels), } - return Metrics[MetricTypeGaugeVec][name].(MetricGaugeVec).GaugeVec + return Metrics[MetricTypeGauge][name].(MetricGaugeVec).GaugeVec } // * Counter @@ -192,12 +192,12 @@ func newCounter(name, help string) prometheus.Counter { // Help: The description help text of the counter // Labels: The labels of the counter func newCounterWithVec(name, help string, labels []string) *prometheus.CounterVec { - if Metrics[MetricTypeCounterVec] == nil { - Metrics[MetricTypeCounterVec] = make(map[string]interface{}) + if Metrics[MetricTypeCounter] == nil { + Metrics[MetricTypeCounter] = make(map[string]interface{}) } // Add the counter to the map - Metrics[MetricTypeCounterVec][name] = MetricCounterVec{ + Metrics[MetricTypeCounter][name] = MetricCounterVec{ // Create the metricBase metricBase: metricBase{ Name: name, @@ -210,7 +210,7 @@ func newCounterWithVec(name, help string, labels []string) *prometheus.CounterVe }, labels), } - return Metrics[MetricTypeCounterVec][name].(MetricCounterVec).CounterVec + return Metrics[MetricTypeCounter][name].(MetricCounterVec).CounterVec } // * Summary @@ -251,12 +251,12 @@ func newSummary(name, help string) prometheus.Summary { // Help: The description help text of the summary // Labels: The labels of the summary func newSummaryWithVec(name, help string, labels []string) *prometheus.SummaryVec { - if Metrics[MetricTypeSummaryVec] == nil { - Metrics[MetricTypeSummaryVec] = make(map[string]interface{}) + if Metrics[MetricTypeSummary] == nil { + Metrics[MetricTypeSummary] = make(map[string]interface{}) } // Add the summary to the map - Metrics[MetricTypeSummaryVec][name] = MetricSummaryVec{ + Metrics[MetricTypeSummary][name] = MetricSummaryVec{ // Create the metricBase metricBase: metricBase{ Name: name, @@ -269,7 +269,7 @@ func newSummaryWithVec(name, help string, labels []string) *prometheus.SummaryVe }, labels), } - return Metrics[MetricTypeSummaryVec][name].(MetricSummaryVec).SummaryVec + return Metrics[MetricTypeSummary][name].(MetricSummaryVec).SummaryVec } // * Histogram @@ -314,12 +314,16 @@ func newHistogram(name, help string) Histogram { // Help: The description help text of the histogram // Labels: The labels of the histogram func newHistogramVec(name, help string, labels []string) HistogramVec { - if Metrics[MetricTypeHistogramVec] == nil { - Metrics[MetricTypeHistogramVec] = make(map[string]interface{}) + if Metrics[MetricTypeHistogram] == nil { + Metrics[MetricTypeHistogram] = make(map[string]interface{}) } // Add the histogram to the map - Metrics[MetricTypeHistogramVec][name] = MetricHistogramVec{ + Metrics[MetricTypeHistogram][name] = MetricHistogramVec{ + metricBase: metricBase{ + Name: name, + Help: help, + }, // Create the metricBase HistogramVec: HistogramVec{ prometheus.NewHistogramVec(prometheus.HistogramOpts{ @@ -331,5 +335,5 @@ func newHistogramVec(name, help string, labels []string) HistogramVec { }, } - return Metrics[MetricTypeHistogramVec][name].(MetricHistogramVec).HistogramVec + return Metrics[MetricTypeHistogram][name].(MetricHistogramVec).HistogramVec } diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 73e9a6a..c468fb2 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -52,25 +52,22 @@ type ( } MetricType string + + Metric interface { + GetHelp() string + GetName() string + } ) const ( // MetricTypeCounter is the type of the metric counter MetricTypeCounter MetricType = "counter" - // MetricTypeCounterVec is the type of the metric counter with labels - MetricTypeCounterVec MetricType = "counterVec" // MetricTypeGauge is the type of the metric gauge MetricTypeGauge MetricType = "gauge" - // MetricTypeGaugeVec is the type of the metric gauge with labels - MetricTypeGaugeVec MetricType = "gaugeVec" // MetricTypeHistogram is the type of the metric histogram MetricTypeHistogram MetricType = "histogram" - // MetricTypeHistogramVec is the type of the metric histogram with labels - MetricTypeHistogramVec MetricType = "histogramVec" // MetricTypeSummary is the type of the metric summary MetricTypeSummary MetricType = "summary" - // MetricTypeSummaryVec is the type of the metric summary with labels - MetricTypeSummaryVec MetricType = "summaryVec" ) var Metrics = make(map[MetricType]map[string]interface{}) @@ -83,3 +80,13 @@ func InitAll() { Registry() AdmissionController() } + +// GetHelp returns the help text of the metric +func (m metricBase) GetHelp() string { + return m.Help +} + +// GetName returns the name of the metric +func (m metricBase) GetName() string { + return m.Name +} diff --git a/tools/doc/generate-doc.go b/tools/doc/generate-doc.go index e43d1c3..fe25af2 100644 --- a/tools/doc/generate-doc.go +++ b/tools/doc/generate-doc.go @@ -37,26 +37,9 @@ func main() { mMap := map[string]string{} - for mType, mm := range metrics.Metrics { + for _, mm := range metrics.Metrics { for name, m := range mm { - switch mType { - case metrics.MetricTypeCounter: - mMap[name] = m.(metrics.MetricCounter).Help - case metrics.MetricTypeCounterVec: - mMap[name] = m.(metrics.MetricCounterVec).Help - case metrics.MetricTypeGauge: - mMap[name] = m.(metrics.MetricGauge).Help - case metrics.MetricTypeGaugeVec: - mMap[name] = m.(metrics.MetricGaugeVec).Help - case metrics.MetricTypeHistogram: - mMap[name] = m.(metrics.MetricHistogram).Help - case metrics.MetricTypeHistogramVec: - mMap[name] = m.(metrics.MetricHistogramVec).Help - case metrics.MetricTypeSummary: - mMap[name] = m.(metrics.MetricSummary).Help - case metrics.MetricTypeSummaryVec: - mMap[name] = m.(metrics.MetricSummaryVec).Help - } + mMap[name] = m.(metrics.Metric).GetHelp() } } @@ -110,35 +93,3 @@ func main() { os.Exit(0) } - -// func toto() string { -// metrics.InitAll() - -// mMap := map[string]string{} - -// for mType := range metrics.Metrics { -// for name, m := range metrics.Metrics[mType] { -// switch mType { -// case metrics.MetricTypeCounter: -// pp.Sprintf("name: %v, m: %v", name, m) -// mMap[name] = m.(metrics.MetricCounter).Help -// case metrics.MetricTypeCounterVec: -// mMap[name] = m.(metrics.MetricCounterVec).Help -// case metrics.MetricTypeGauge: -// mMap[name] = m.(metrics.MetricGauge).Help -// case metrics.MetricTypeGaugeVec: -// mMap[name] = m.(metrics.MetricGaugeVec).Help -// case metrics.MetricTypeHistogram: -// mMap[name] = m.(metrics.MetricHistogram).Help -// case metrics.MetricTypeHistogramVec: -// mMap[name] = m.(metrics.MetricHistogramVec).Help -// case metrics.MetricTypeSummary: -// mMap[name] = m.(metrics.MetricSummary).Help -// case metrics.MetricTypeSummaryVec: -// mMap[name] = m.(metrics.MetricSummaryVec).Help -// } -// } -// } -// os.Exit(0) -// return "toto" -// }