mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
feat: measure pubsub latencies and expose metrics (#13126)
This commit is contained in:
@ -7,29 +7,60 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func PromGaugeHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, label ...string) bool {
|
||||
type kind string
|
||||
|
||||
const (
|
||||
counterKind kind = "counter"
|
||||
gaugeKind kind = "gauge"
|
||||
)
|
||||
|
||||
func PromGaugeHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
for _, family := range metrics {
|
||||
if family.GetName() != name {
|
||||
continue
|
||||
}
|
||||
ms := family.GetMetric()
|
||||
metricsLoop:
|
||||
for _, m := range ms {
|
||||
require.Equal(t, len(label), len(m.GetLabel()))
|
||||
for i, lv := range label {
|
||||
if lv != m.GetLabel()[i].GetValue() {
|
||||
continue metricsLoop
|
||||
}
|
||||
}
|
||||
return value == m.GetGauge().GetValue()
|
||||
}
|
||||
}
|
||||
return false
|
||||
return value == getValue(t, metrics, gaugeKind, name, labels...)
|
||||
}
|
||||
|
||||
func PromCounterHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, label ...string) bool {
|
||||
func PromCounterHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
return value == getValue(t, metrics, counterKind, name, labels...)
|
||||
}
|
||||
|
||||
func PromGaugeAssertion(t testing.TB, metrics []*dto.MetricFamily, assert func(in float64) bool, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
return assert(getValue(t, metrics, gaugeKind, name, labels...))
|
||||
}
|
||||
|
||||
func PromCounterAssertion(t testing.TB, metrics []*dto.MetricFamily, assert func(in float64) bool, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
return assert(getValue(t, metrics, counterKind, name, labels...))
|
||||
}
|
||||
|
||||
func PromCounterGathered(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
return getMetric(t, metrics, name, labels...) != nil
|
||||
}
|
||||
|
||||
func PromGaugeGathered(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) bool {
|
||||
t.Helper()
|
||||
return getMetric(t, metrics, name, labels...) != nil
|
||||
}
|
||||
|
||||
func getValue(t testing.TB, metrics []*dto.MetricFamily, kind kind, name string, labels ...string) float64 {
|
||||
m := getMetric(t, metrics, name, labels...)
|
||||
if m == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case counterKind:
|
||||
return m.GetCounter().GetValue()
|
||||
case gaugeKind:
|
||||
return m.GetGauge().GetValue()
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func getMetric(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) *dto.Metric {
|
||||
for _, family := range metrics {
|
||||
if family.GetName() != name {
|
||||
continue
|
||||
@ -37,14 +68,16 @@ func PromCounterHasValue(t testing.TB, metrics []*dto.MetricFamily, value float6
|
||||
ms := family.GetMetric()
|
||||
metricsLoop:
|
||||
for _, m := range ms {
|
||||
require.Equal(t, len(label), len(m.GetLabel()))
|
||||
for i, lv := range label {
|
||||
require.Equal(t, len(labels), len(m.GetLabel()))
|
||||
for i, lv := range labels {
|
||||
if lv != m.GetLabel()[i].GetValue() {
|
||||
continue metricsLoop
|
||||
}
|
||||
}
|
||||
return value == m.GetCounter().GetValue()
|
||||
|
||||
return m
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user