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

Function TestCollector_Set_Add

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

Source from the content-addressed store, hash-verified

80}
81
82func 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
119func collectAndSortMetrics(t *testing.T, collector prometheus.Collector, count int) []*dto.Metric {
120 ch := make(chan prometheus.Metric, count)

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