|
| 1 | +package prometheusmetrics_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"sort" |
| 5 | +"testing" |
| 6 | + |
| 7 | +"github.com/prometheus/client_golang/prometheus" |
| 8 | +dto"github.com/prometheus/client_model/go" |
| 9 | +"github.com/stretchr/testify/assert" |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | + |
| 12 | +"github.com/coder/coder/coderd/prometheusmetrics" |
| 13 | +) |
| 14 | + |
| 15 | +funcTestCollector_Add(t*testing.T) { |
| 16 | +t.Parallel() |
| 17 | + |
| 18 | +// given |
| 19 | +agentsGauge:=prometheusmetrics.NewCachedGaugeVec(prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 20 | +Namespace:"coderd", |
| 21 | +Subsystem:"agents", |
| 22 | +Name:"up", |
| 23 | +Help:"The number of active agents per workspace.", |
| 24 | +}, []string{"username","workspace_name"})) |
| 25 | + |
| 26 | +// when |
| 27 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,7,"first user","my workspace") |
| 28 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,23,"second user","your workspace") |
| 29 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,1,"first user","my workspace") |
| 30 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,25,"second user","your workspace") |
| 31 | +agentsGauge.Commit() |
| 32 | + |
| 33 | +// then |
| 34 | +ch:=make(chan prometheus.Metric,2) |
| 35 | +agentsGauge.Collect(ch) |
| 36 | + |
| 37 | +metrics:=collectAndSortMetrics(t,agentsGauge,2) |
| 38 | + |
| 39 | +assert.Equal(t,"first user",metrics[0].Label[0].GetValue())// Username |
| 40 | +assert.Equal(t,"my workspace",metrics[0].Label[1].GetValue())// Workspace name |
| 41 | +assert.Equal(t,8,int(metrics[0].Gauge.GetValue()))// Metric value |
| 42 | + |
| 43 | +assert.Equal(t,"second user",metrics[1].Label[0].GetValue())// Username |
| 44 | +assert.Equal(t,"your workspace",metrics[1].Label[1].GetValue())// Workspace name |
| 45 | +assert.Equal(t,48,int(metrics[1].Gauge.GetValue()))// Metric value |
| 46 | +} |
| 47 | + |
| 48 | +funcTestCollector_Set(t*testing.T) { |
| 49 | +t.Parallel() |
| 50 | + |
| 51 | +// given |
| 52 | +agentsGauge:=prometheusmetrics.NewCachedGaugeVec(prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 53 | +Namespace:"coderd", |
| 54 | +Subsystem:"agents", |
| 55 | +Name:"up", |
| 56 | +Help:"The number of active agents per workspace.", |
| 57 | +}, []string{"username","workspace_name"})) |
| 58 | + |
| 59 | +// when |
| 60 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,3,"first user","my workspace") |
| 61 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,4,"second user","your workspace") |
| 62 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,5,"first user","my workspace") |
| 63 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,6,"second user","your workspace") |
| 64 | +agentsGauge.Commit() |
| 65 | + |
| 66 | +// then |
| 67 | +ch:=make(chan prometheus.Metric,2) |
| 68 | +agentsGauge.Collect(ch) |
| 69 | + |
| 70 | +metrics:=collectAndSortMetrics(t,agentsGauge,2) |
| 71 | + |
| 72 | +assert.Equal(t,"first user",metrics[0].Label[0].GetValue())// Username |
| 73 | +assert.Equal(t,"my workspace",metrics[0].Label[1].GetValue())// Workspace name |
| 74 | +assert.Equal(t,5,int(metrics[0].Gauge.GetValue()))// Metric value |
| 75 | + |
| 76 | +assert.Equal(t,"second user",metrics[1].Label[0].GetValue())// Username |
| 77 | +assert.Equal(t,"your workspace",metrics[1].Label[1].GetValue())// Workspace name |
| 78 | +assert.Equal(t,6,int(metrics[1].Gauge.GetValue()))// Metric value |
| 79 | +} |
| 80 | + |
| 81 | +funcTestCollector_Set_Add(t*testing.T) { |
| 82 | +t.Parallel() |
| 83 | + |
| 84 | +// given |
| 85 | +agentsGauge:=prometheusmetrics.NewCachedGaugeVec(prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 86 | +Namespace:"coderd", |
| 87 | +Subsystem:"agents", |
| 88 | +Name:"up", |
| 89 | +Help:"The number of active agents per workspace.", |
| 90 | +}, []string{"username","workspace_name"})) |
| 91 | + |
| 92 | +// when |
| 93 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,9,"first user","my workspace") |
| 94 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,8,"second user","your workspace") |
| 95 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,7,"first user","my workspace") |
| 96 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,6,"second user","your workspace") |
| 97 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,5,"first user","my workspace") |
| 98 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet,4,"second user","your workspace") |
| 99 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,3,"first user","my workspace") |
| 100 | +agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd,2,"second user","your workspace") |
| 101 | +agentsGauge.Commit() |
| 102 | + |
| 103 | +// then |
| 104 | +ch:=make(chan prometheus.Metric,2) |
| 105 | +agentsGauge.Collect(ch) |
| 106 | + |
| 107 | +metrics:=collectAndSortMetrics(t,agentsGauge,2) |
| 108 | + |
| 109 | +assert.Equal(t,"first user",metrics[0].Label[0].GetValue())// Username |
| 110 | +assert.Equal(t,"my workspace",metrics[0].Label[1].GetValue())// Workspace name |
| 111 | +assert.Equal(t,8,int(metrics[0].Gauge.GetValue()))// Metric value |
| 112 | + |
| 113 | +assert.Equal(t,"second user",metrics[1].Label[0].GetValue())// Username |
| 114 | +assert.Equal(t,"your workspace",metrics[1].Label[1].GetValue())// Workspace name |
| 115 | +assert.Equal(t,6,int(metrics[1].Gauge.GetValue()))// Metric value |
| 116 | +} |
| 117 | + |
| 118 | +funccollectAndSortMetrics(t*testing.T,collector prometheus.Collector,countint) []dto.Metric { |
| 119 | +ch:=make(chan prometheus.Metric,count) |
| 120 | +deferclose(ch) |
| 121 | + |
| 122 | +varmetrics []dto.Metric |
| 123 | + |
| 124 | +collector.Collect(ch) |
| 125 | +fori:=0;i<count;i++ { |
| 126 | +m:=<-ch |
| 127 | + |
| 128 | +varmetric dto.Metric |
| 129 | +err:=m.Write(&metric) |
| 130 | +require.NoError(t,err) |
| 131 | + |
| 132 | +metrics=append(metrics,metric) |
| 133 | +} |
| 134 | + |
| 135 | +// Ensure always the same order of metrics |
| 136 | +sort.Slice(metrics,func(i,jint)bool { |
| 137 | +returnsort.StringsAreSorted([]string{metrics[i].Label[0].GetValue(),metrics[j].Label[1].GetValue()}) |
| 138 | +}) |
| 139 | +returnmetrics |
| 140 | +} |