Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Oct 22, 2024
1 parent 2b16c5b commit 473171f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 76 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
36 changes: 20 additions & 16 deletions internal/metrics/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -331,5 +335,5 @@ func newHistogramVec(name, help string, labels []string) HistogramVec {
},
}

return Metrics[MetricTypeHistogramVec][name].(MetricHistogramVec).HistogramVec
return Metrics[MetricTypeHistogram][name].(MetricHistogramVec).HistogramVec
}
23 changes: 15 additions & 8 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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
}
53 changes: 2 additions & 51 deletions tools/doc/generate-doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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"
// }

0 comments on commit 473171f

Please sign in to comment.