MCPcopy Index your code
hub / github.com/coder/coder / TestCollector_Add

Function TestCollector_Add

coderd/prometheusmetrics/collector_test.go:16–47  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestCollector_Add(t *testing.T) {
17 t.Parallel()
18
19 // given
20 agentsGauge := prometheusmetrics.NewCachedGaugeVec(prometheus.NewGaugeVec(prometheus.GaugeOpts{
21 Namespace: "coderd",
22 Subsystem: "agents",
23 Name: "up",
24 Help: "The number of active agents per workspace.",
25 }, []string{"username", "workspace_name"}))
26
27 // when
28 agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 7, "first user", "my workspace")
29 agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 23, "second user", "your workspace")
30 agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 1, "first user", "my workspace")
31 agentsGauge.WithLabelValues(prometheusmetrics.VectorOperationAdd, 25, "second user", "your workspace")
32 agentsGauge.Commit()
33
34 // then
35 ch := make(chan prometheus.Metric, 2)
36 agentsGauge.Collect(ch)
37
38 metrics := collectAndSortMetrics(t, agentsGauge, 2)
39
40 assert.Equal(t, "first user", metrics[0].Label[0].GetValue()) // Username
41 assert.Equal(t, "my workspace", metrics[0].Label[1].GetValue()) // Workspace name
42 assert.Equal(t, 8, int(metrics[0].Gauge.GetValue())) // Metric value
43
44 assert.Equal(t, "second user", metrics[1].Label[0].GetValue()) // Username
45 assert.Equal(t, "your workspace", metrics[1].Label[1].GetValue()) // Workspace name
46 assert.Equal(t, 48, int(metrics[1].Gauge.GetValue())) // Metric value
47}
48
49func TestCollector_Set(t *testing.T) {
50 t.Parallel()

Callers

nothing calls this directly

Calls 7

WithLabelValuesMethod · 0.95
CommitMethod · 0.95
CollectMethod · 0.95
NewCachedGaugeVecFunction · 0.92
collectAndSortMetricsFunction · 0.85
EqualMethod · 0.45
GetValueMethod · 0.45

Tested by

no test coverage detected