(t *testing.T)
| 80 | } |
| 81 | |
| 82 | func TestCollector_Set_Add(t *testing.T) { |
| 83 | t.Parallel() |
| 84 | |
| 85 | // given |
| 86 | agentsGauge := prometheusmetrics.NewCachedGaugeVec(prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 87 | Namespace: "coderd", |
| 88 | Subsystem: "agents", |
| 89 | Name: "up", |
| 90 | Help: "The number of active agents per workspace.", |
| 91 | }, []string{"username", "workspace_name"})) |
| 92 | |
| 93 | // when |
| 94 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 9, "first user", "my workspace") |
| 95 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 8, "second user", "your workspace") |
| 96 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 7, "first user", "my workspace") |
| 97 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 6, "second user", "your workspace") |
| 98 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet, 5, "first user", "my workspace") |
| 99 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationSet, 4, "second user", "your workspace") |
| 100 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 3, "first user", "my workspace") |
| 101 | agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 2, "second user", "your workspace") |
| 102 | agentsGauge.Commit() |
| 103 | |
| 104 | // then |
| 105 | ch := make(chan prometheus.Metric, 2) |
| 106 | agentsGauge.Collect(ch) |
| 107 | |
| 108 | metrics := collectAndSortMetrics(t, agentsGauge, 2) |
| 109 | |
| 110 | assert.Equal(t, "first user", metrics[0].Label[0].GetValue()) // Username |
| 111 | assert.Equal(t, "my workspace", metrics[0].Label[1].GetValue()) // Workspace name |
| 112 | assert.Equal(t, 8, int(metrics[0].Gauge.GetValue())) // Metric value |
| 113 | |
| 114 | assert.Equal(t, "second user", metrics[1].Label[0].GetValue()) // Username |
| 115 | assert.Equal(t, "your workspace", metrics[1].Label[1].GetValue()) // Workspace name |
| 116 | assert.Equal(t, 6, int(metrics[1].Gauge.GetValue())) // Metric value |
| 117 | } |
| 118 | |
| 119 | func collectAndSortMetrics(t *testing.T, collector prometheus.Collector, count int) []*dto.Metric { |
| 120 | ch := make(chan prometheus.Metric, count) |
nothing calls this directly
no test coverage detected