Skip to content

Commit

Permalink
Update promutils workqueue Metrics for newest client-go (flyteorg#78)
Browse files Browse the repository at this point in the history
* Update prometheus workqueue Metrics provider to work with latest Client Go

Signed-off-by: Haytham Abuelfutuh <haytham@afutuh.com>

* Add unit tests

Signed-off-by: Haytham Abuelfutuh <haytham@afutuh.com>
  • Loading branch information
EngHabu authored Apr 2, 2021
1 parent a07acb2 commit 15cac7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/flyteorg/flytestdlib

go 1.13
go 1.16

require (
cloud.google.com/go v0.75.0 // indirect
Expand Down
31 changes: 17 additions & 14 deletions promutils/workqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package promutils

import (
"fmt"

"k8s.io/client-go/util/workqueue"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -28,18 +26,23 @@ import (
// prometheus metrics. To use this package, you just have to import it.

func init() {
var provider interface{} //nolint
provider = prometheusMetricsProvider{}
if p, casted := provider.(workqueue.MetricsProvider); casted {
workqueue.SetProvider(p)
} else {
// This case happens in future versions of client-go where the interface has added methods
fmt.Println("Warn: No metricsProvider set for the workqueue")
}
provider := prometheusMetricsProvider{}
workqueue.SetProvider(provider)
}

type prometheusMetricsProvider struct{}

func (prometheusMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric {
unfinishedWork := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: name,
Name: "longest_running_processor_s",
Help: "How many microseconds longest running processor from workqueue" + name + " takes.",
})

prometheus.MustRegister(unfinishedWork)
return unfinishedWork
}

func (prometheusMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric {
unfinishedWork := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: name,
Expand Down Expand Up @@ -80,8 +83,8 @@ func (prometheusMetricsProvider) NewAddsMetric(name string) workqueue.CounterMet
return adds
}

func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.SummaryMetric {
latency := prometheus.NewSummary(prometheus.SummaryOpts{
func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric {
latency := prometheus.NewHistogram(prometheus.HistogramOpts{
Subsystem: name,
Name: "queue_latency_us",
Help: "How long an item stays in workqueue" + name + " before being requested.",
Expand All @@ -90,8 +93,8 @@ func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.Summary
return latency
}

func (prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.SummaryMetric {
workDuration := prometheus.NewSummary(prometheus.SummaryOpts{
func (prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric {
workDuration := prometheus.NewHistogram(prometheus.HistogramOpts{
Subsystem: name,
Name: "work_duration_us",
Help: "How long processing an item from workqueue" + name + " takes.",
Expand Down
18 changes: 18 additions & 0 deletions promutils/workqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ func TestPrometheusMetricsProvider(t *testing.T) {
_, ok := c.(prometheus.Summary)
assert.True(t, ok)
})

t.Run("NewLongestRunningProcessorSecondsMetric", func(t *testing.T) {
c := provider.NewLongestRunningProcessorSecondsMetric("x")
_, ok := c.(prometheus.Gauge)
assert.True(t, ok)
})

t.Run("NewUnfinishedWorkSecondsMetric", func(t *testing.T) {
c := provider.NewUnfinishedWorkSecondsMetric("x")
_, ok := c.(prometheus.Gauge)
assert.True(t, ok)
})

t.Run("NewLongestRunningProcessorMicrosecondsMetric", func(t *testing.T) {
c := provider.NewLongestRunningProcessorMicrosecondsMetric("x")
_, ok := c.(prometheus.Gauge)
assert.True(t, ok)
})
}

0 comments on commit 15cac7b

Please sign in to comment.