Skip to content

Commit

Permalink
feat(metrics): Add getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Jordan committed Mar 6, 2024
1 parent cd051e5 commit 159d54c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/api/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
io_prometheus_client "github.com/prometheus/client_model/go"
)

const (
Expand Down Expand Up @@ -60,6 +61,30 @@ func (p *PrometheusAPI) HandleRequest(w http.ResponseWriter, r *http.Request) {

var registered map[string]interface{}

// Get current value of a Counter metric
func GetCounter(name string) *float64 {
if _, ok := registered[name]; ok {
m := io_prometheus_client.Metric{}
registered[name].(prometheus.Counter).Write(&m)

Check failure on line 68 in pkg/api/prometheus/prometheus.go

View workflow job for this annotation

GitHub Actions / Validate Go code linting

Error return value of `(github.com/prometheus/client_golang/prometheus.Metric).Write` is not checked (errcheck)
return m.Counter.Value
}
return nil
}

// Get current value of an Average metric
func GetAverage(name string) *float64 {
if _, ok := registered[name]; ok {
m := io_prometheus_client.Metric{}
registered[name].(prometheus.Histogram).Write(&m)

Check failure on line 78 in pkg/api/prometheus/prometheus.go

View workflow job for this annotation

GitHub Actions / Validate Go code linting

Error return value of `(github.com/prometheus/client_golang/prometheus.Metric).Write` is not checked (errcheck)
if m.Histogram.SampleSum != nil && m.Histogram.SampleCountFloat != nil {
average := *m.Histogram.SampleSum / *m.Histogram.SampleCountFloat
return &average
}
return nil
}
return nil
}

// Increment will increment the counter.
func Increment(name string) {
if _, ok := registered[name]; ok {
Expand Down

0 comments on commit 159d54c

Please sign in to comment.