Skip to content

Commit 023f7d4

Browse files
committed
Review comments & CI appeasement
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 5e89d05 commit 023f7d4

File tree

8 files changed

+121
-93
lines changed

8 files changed

+121
-93
lines changed

cli/testdata/coder_server_--help.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ INTROSPECTION / LOGGING OPTIONS:
119119
--log-stackdriver string, $CODER_LOGGING_STACKDRIVER
120120
Output Stackdriver compatible logs to a given file.
121121

122-
INTROSPECTION / PROMETHEUS OPTIONS:
122+
INTROSPECTION / PROMETHEUS OPTIONS:
123123
--prometheus-address host:port, $CODER_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)
124124
The bind address to serve prometheus metrics.
125125

126-
--prometheus-aggregate-agent-stats-by string-array, $CODER_PROMETHEUS_AGGREGATE_AGENT_STATS_BY
126+
--prometheus-aggregate-agent-stats-by string-array, $CODER_PROMETHEUS_AGGREGATE_AGENT_STATS_BY (default: agent_name,template_name,username,workspace_name)
127127
When collecting agent stats, aggregate metrics by a given set of
128128
comma-separated labels to reduce cardinality. Accepted values are
129-
template_name, agent_name, username, workspace_name.
129+
agent_name, template_name, username, workspace_name.
130130

131131
--prometheus-collect-agent-stats bool, $CODER_PROMETHEUS_COLLECT_AGENT_STATS
132132
Collect agent stats (may increase charges for metrics storage).

cli/testdata/server-config.yaml.golden

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,14 @@ introspection:
189189
# (default: <unset>, type: bool)
190190
collect_agent_stats: false
191191
# When collecting agent stats, aggregate metrics by a given set of comma-separated
192-
# labels to reduce cardinality. Accepted values are template_name, agent_name,
192+
# labels to reduce cardinality. Accepted values are agent_name, template_name,
193193
# username, workspace_name.
194-
# (default: <unset>, type: string-array)
195-
aggregate_agent_stats_by: []
194+
# (default: agent_name,template_name,username,workspace_name, type: string-array)
195+
aggregate_agent_stats_by:
196+
- agent_name
197+
- template_name
198+
- username
199+
- workspace_name
196200
# Collect database metrics (may increase charges for metrics storage).
197201
# (default: false, type: bool)
198202
collect_db_metrics: false

coderd/agentmetrics/labels.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package agentmetrics
22

33
const (
4-
TemplateNameLabel = "template_name"
5-
AgentNameLabel = "agent_name"
6-
UsernameLabel = "username"
7-
WorkspaceNameLabel = "workspace_name"
4+
LabelAgentName = "agent_name"
5+
LabelUsername = "username"
6+
LabelTemplateName = "template_name"
7+
LabelWorkspaceName = "workspace_name"
8+
)
9+
10+
var (
11+
LabelAll = []string{LabelAgentName, LabelTemplateName, LabelUsername, LabelWorkspaceName}
12+
LabelAgentStats = []string{LabelAgentName, LabelUsername, LabelWorkspaceName}
813
)

coderd/prometheusmetrics/aggregator.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (am *annotatedMetric) asPrometheus() (prometheus.Metric, error) {
114114
extraLabels = am.Labels
115115
)
116116

117-
for _, label := range am.aggregateByLabels {
117+
for _, label := range baseLabelNames {
118118
val, err := am.getFieldByLabel(label)
119119
if err != nil {
120120
return nil, err
@@ -147,13 +147,13 @@ func (am *annotatedMetric) asPrometheus() (prometheus.Metric, error) {
147147
func (am *annotatedMetric) getFieldByLabel(label string) (string, error) {
148148
var labelVal string
149149
switch label {
150-
case agentmetrics.WorkspaceNameLabel:
150+
case agentmetrics.LabelWorkspaceName:
151151
labelVal = am.workspaceName
152-
case agentmetrics.TemplateNameLabel:
152+
case agentmetrics.LabelTemplateName:
153153
labelVal = am.templateName
154-
case agentmetrics.AgentNameLabel:
154+
case agentmetrics.LabelAgentName:
155155
labelVal = am.agentName
156-
case agentmetrics.UsernameLabel:
156+
case agentmetrics.LabelUsername:
157157
labelVal = am.username
158158
default:
159159
return "", xerrors.Errorf("unexpected label: %q", label)
@@ -162,7 +162,7 @@ func (am *annotatedMetric) getFieldByLabel(label string) (string, error) {
162162
return labelVal, nil
163163
}
164164

165-
func (am *annotatedMetric) clone() annotatedMetric {
165+
func (am *annotatedMetric) shallowCopy() annotatedMetric {
166166
stats := &agentproto.Stats_Metric{
167167
Name: am.Name,
168168
Type: am.Type,
@@ -273,7 +273,7 @@ func (a *labelAggregator) aggregate(am annotatedMetric, labels []string) error {
273273
metric, found := a.metrics[key]
274274
if !found {
275275
// Take a copy of the given annotatedMetric because it may be manipulated later and contains pointers.
276-
metric = am.clone()
276+
metric = am.shallowCopy()
277277
}
278278

279279
// Store the metric.
@@ -337,8 +337,9 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
337337

338338
// If custom aggregation labels have not been chosen, generate Prometheus metrics without any pre-aggregation.
339339
// This results in higher cardinality, but may be desirable in larger deployments.
340+
//
340341
// Default behavior.
341-
if len(ma.aggregateByLabels) == 0 {
342+
if len(ma.aggregateByLabels) == len(agentmetrics.LabelAll) {
342343
for _, m := range ma.store {
343344
// Aggregate by all available metrics.
344345
m.aggregateByLabels = defaultAgentMetricsLabels
@@ -402,7 +403,7 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
402403
func (*MetricsAggregator) Describe(_ chan<- *prometheus.Desc) {
403404
}
404405

405-
var defaultAgentMetricsLabels = []string{agentmetrics.UsernameLabel, agentmetrics.WorkspaceNameLabel, agentmetrics.AgentNameLabel, agentmetrics.TemplateNameLabel}
406+
var defaultAgentMetricsLabels = []string{agentmetrics.LabelUsername, agentmetrics.LabelWorkspaceName, agentmetrics.LabelAgentName, agentmetrics.LabelTemplateName}
406407

407408
// AgentMetricLabels are the labels used to decorate an agent's metrics.
408409
// This list should match the list of labels in agentMetricsLabels.

0 commit comments

Comments
 (0)