Skip to content

Commit

Permalink
Change execution_duration_seconds metric type from gauge to histogram (
Browse files Browse the repository at this point in the history
…#169)

* change duration seconds to histogram; fix registerer wrapper; fix typo; fix time.Now() to time.Time{}

* add comment for return time.Time{}
  • Loading branch information
DeimonDB authored Oct 5, 2022
1 parent 9e8f4b7 commit b780095
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 11 additions & 10 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type PerformanceMetric interface {

type performanceMetric struct {
executionStarted *prometheus.GaugeVec
executionDurationSeconds *prometheus.GaugeVec
executionDurationSeconds *prometheus.HistogramVec
executionSucceededTotal *prometheus.CounterVec
executionFailedTotal *prometheus.CounterVec
}
Expand All @@ -36,22 +36,22 @@ func NewPerformanceMetric(namespace string, labels prometheus.Labels, reg promet
Help: "Last Unix time when execution started.",
}, nil)

executionDurationSeconds := prometheus.NewGaugeVec(prometheus.GaugeOpts{
executionDurationSeconds := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: executionDurationSecondsKey,
Help: "Duration of the last execution.",
Help: "Duration of the executions.",
}, nil)

executionSucceededTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: executionSucceededTotalKey,
Help: "Total number of the executions wich succeeded.",
Help: "Total number of the executions which succeeded.",
}, nil)

executionFailedTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: executionFailedTotalKey,
Help: "Total number of the executions wich failed.",
Help: "Total number of the executions which failed.",
}, nil)

Register(labels, reg, executionStarted, executionDurationSeconds, executionSucceededTotal, executionFailedTotal)
Expand All @@ -72,7 +72,7 @@ func (m *performanceMetric) Start() time.Time {

func (m *performanceMetric) Duration(start time.Time) {
duration := time.Since(start)
m.executionDurationSeconds.WithLabelValues().Set(duration.Seconds())
m.executionDurationSeconds.WithLabelValues().Observe(duration.Seconds())
}

func (m *performanceMetric) Success() {
Expand All @@ -88,8 +88,9 @@ func (m *performanceMetric) Failure() {
type NullablePerformanceMetric struct{}

func (NullablePerformanceMetric) Start() time.Time {
return time.Now()
// NullablePerformanceMetric is a no-op, so returning empty value
return time.Time{}
}
func (NullablePerformanceMetric) Duration(start time.Time) {}
func (NullablePerformanceMetric) Success() {}
func (NullablePerformanceMetric) Failure() {}
func (NullablePerformanceMetric) Duration(_ time.Time) {}
func (NullablePerformanceMetric) Success() {}
func (NullablePerformanceMetric) Failure() {}
4 changes: 3 additions & 1 deletion metrics/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package metrics

import (
"github.com/prometheus/client_golang/prometheus"

"github.com/trustwallet/go-libs/logging"
)

func Register(labels prometheus.Labels, reg prometheus.Registerer, collectors ...prometheus.Collector) {
registerer := prometheus.WrapRegistererWith(labels, reg)
for _, c := range collectors {
err := prometheus.WrapRegistererWith(labels, reg).Register(c)
err := registerer.Register(c)
if err != nil {
if _, ok := err.(*prometheus.AlreadyRegisteredError); !ok {
logging.GetLogger().WithError(err).
Expand Down

0 comments on commit b780095

Please sign in to comment.