Skip to content

Commit 92be1d6

Browse files
committed
More review comments & CI appeasement
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 9aedd97 commit 92be1d6

File tree

6 files changed

+97
-14
lines changed

6 files changed

+97
-14
lines changed

coderd/agentmetrics/labels.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package agentmetrics
22

3+
import (
4+
"strings"
5+
6+
"golang.org/x/xerrors"
7+
)
8+
39
const (
410
LabelAgentName = "agent_name"
511
LabelUsername = "username"
@@ -11,3 +17,22 @@ var (
1117
LabelAll = []string{LabelAgentName, LabelTemplateName, LabelUsername, LabelWorkspaceName}
1218
LabelAgentStats = []string{LabelAgentName, LabelUsername, LabelWorkspaceName}
1319
)
20+
21+
// ValidateAggregationLabels ensures a given set of labels are valid aggregation labels.
22+
func ValidateAggregationLabels(labels []string) error {
23+
acceptable := LabelAll
24+
25+
seen := make(map[string]any, len(acceptable))
26+
for _, label := range acceptable {
27+
seen[label] = nil
28+
}
29+
30+
for _, label := range labels {
31+
if _, found := seen[label]; !found {
32+
return xerrors.Errorf("%q is not a valid aggregation label; only one or more of %q are acceptable",
33+
label, strings.Join(acceptable, ", "))
34+
}
35+
}
36+
37+
return nil
38+
}

coderd/agentmetrics/labels_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package agentmetrics_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/coder/v2/coderd/agentmetrics"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestValidateAggregationLabels(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
labels []string
14+
expectedErr bool
15+
}{
16+
{
17+
name: "empty list is valid",
18+
},
19+
{
20+
name: "single valid entry",
21+
labels: []string{agentmetrics.LabelTemplateName},
22+
},
23+
{
24+
name: "multiple valid entries",
25+
labels: []string{agentmetrics.LabelTemplateName, agentmetrics.LabelUsername},
26+
},
27+
{
28+
name: "repeated valid entries are not invalid",
29+
labels: []string{agentmetrics.LabelTemplateName, agentmetrics.LabelUsername, agentmetrics.LabelUsername, agentmetrics.LabelUsername},
30+
},
31+
{
32+
name: "empty entry is invalid",
33+
labels: []string{""},
34+
expectedErr: true,
35+
},
36+
}
37+
38+
for _, tc := range tests {
39+
t.Run(tc.name, func(t *testing.T) {
40+
t.Parallel()
41+
42+
err := agentmetrics.ValidateAggregationLabels(tc.labels)
43+
if tc.expectedErr {
44+
require.Error(t, err)
45+
}
46+
})
47+
}
48+
}

coderd/prometheusmetrics/aggregator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
335335
var input []annotatedMetric
336336
output := make([]prometheus.Metric, 0, len(ma.store))
337337

338+
if len(ma.aggregateByLabels) == 0 {
339+
ma.aggregateByLabels = agentmetrics.LabelAll
340+
}
341+
338342
// If custom aggregation labels have not been chosen, generate Prometheus metrics without any pre-aggregation.
339343
// This results in higher cardinality, but may be desirable in larger deployments.
340344
//

coderd/prometheusmetrics/aggregator_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,23 @@ func TestLabelsAggregation(t *testing.T) {
561561
// Nothing will be returned.
562562
expected: []*agentproto.Stats_Metric{},
563563
},
564+
{
565+
// Scenario: validation fails and an empty list is given for aggregation.
566+
name: "empty label aggregation list",
567+
aggregateOn: []string{},
568+
given: []statCollection{
569+
{
570+
labels: testLabels,
571+
metrics: []*agentproto.Stats_Metric{
572+
{Name: "user_counter", Type: agentproto.Stats_Metric_COUNTER, Value: 1},
573+
},
574+
},
575+
},
576+
// Default aggregation will be used.
577+
expected: []*agentproto.Stats_Metric{
578+
{Name: "user_counter", Type: agentproto.Stats_Metric_COUNTER, Value: 1, Labels: commonLabels},
579+
},
580+
},
564581
}
565582

566583
for _, tc := range tests {

codersdk/deployment.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -956,19 +956,7 @@ when required by your organization's security policy.`,
956956
return nil
957957
}
958958

959-
acceptable := make(map[string]any, len(agentmetrics.LabelAll))
960-
for _, label := range agentmetrics.LabelAll {
961-
acceptable[label] = nil
962-
}
963-
964-
for _, label := range value.Value() {
965-
if _, found := acceptable[label]; !found {
966-
return xerrors.Errorf("%q is not a valid aggregation label; only one or more of %q are acceptable",
967-
label, strings.Join(agentmetrics.LabelAll, ", "))
968-
}
969-
}
970-
971-
return nil
959+
return agentmetrics.ValidateAggregationLabels(value.Value())
972960
}),
973961
Group: &deploymentGroupIntrospectionPrometheus,
974962
YAML: "aggregate_agent_stats_by",

docs/cli/server.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)