mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
This reverts commit 21d1873d97.
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package agentmetrics
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
LabelAgentName = "agent_name"
|
||||
LabelTemplateName = "template_name"
|
||||
LabelUsername = "username"
|
||||
LabelWorkspaceName = "workspace_name"
|
||||
)
|
||||
|
||||
var (
|
||||
LabelAll = []string{LabelAgentName, LabelTemplateName, LabelUsername, LabelWorkspaceName}
|
||||
LabelAgentStats = []string{LabelAgentName, LabelUsername, LabelWorkspaceName}
|
||||
)
|
||||
|
||||
// ValidateAggregationLabels ensures a given set of labels are valid aggregation labels.
|
||||
func ValidateAggregationLabels(labels []string) error {
|
||||
acceptable := LabelAll
|
||||
|
||||
seen := make(map[string]any, len(acceptable))
|
||||
for _, label := range acceptable {
|
||||
seen[label] = nil
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
if _, found := seen[label]; !found {
|
||||
return xerrors.Errorf("%q is not a valid aggregation label; only one or more of %q are acceptable",
|
||||
label, strings.Join(acceptable, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package agentmetrics_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/agentmetrics"
|
||||
)
|
||||
|
||||
func TestValidateAggregationLabels(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
labels []string
|
||||
expectedErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty list is valid",
|
||||
},
|
||||
{
|
||||
name: "single valid entry",
|
||||
labels: []string{agentmetrics.LabelTemplateName},
|
||||
},
|
||||
{
|
||||
name: "multiple valid entries",
|
||||
labels: []string{agentmetrics.LabelTemplateName, agentmetrics.LabelUsername},
|
||||
},
|
||||
{
|
||||
name: "repeated valid entries are not invalid",
|
||||
labels: []string{agentmetrics.LabelTemplateName, agentmetrics.LabelUsername, agentmetrics.LabelUsername, agentmetrics.LabelUsername},
|
||||
},
|
||||
{
|
||||
name: "empty entry is invalid",
|
||||
labels: []string{""},
|
||||
expectedErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := agentmetrics.ValidateAggregationLabels(tc.labels)
|
||||
if tc.expectedErr {
|
||||
require.Error(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user