Skip to content

Commit d86496e

Browse files
committed
PoC works
1 parent df80e9b commit d86496e

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

coderd/prometheusmetrics/aggregator.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ const (
2323
type MetricsAggregator struct {
2424
m sync.Mutex
2525
log slog.Logger
26-
queue []annotatedMetrics
26+
queue []annotatedMetric
2727
}
2828

29-
type annotatedMetrics struct {
29+
type annotatedMetric struct {
30+
agentsdk.AgentMetric
31+
3032
username string
3133
workspaceName string
3234
agentName string
33-
34-
metrics []agentsdk.AgentMetric
3535
}
3636

3737
var _ prometheus.Collector = new(MetricsAggregator)
@@ -47,16 +47,14 @@ func (ma *MetricsAggregator) Collect(ch chan<- prometheus.Metric) {
4747
ma.m.Lock()
4848
defer ma.m.Unlock()
4949

50-
for _, annotated := range ma.queue {
51-
for _, m := range annotated.metrics {
52-
desc := prometheus.NewDesc(m.Name, metricHelpForAgent, agentMetricsLabels, nil)
53-
valueType, err := asPrometheusValueType(m.Type)
54-
if err != nil {
55-
ma.log.Error(context.Background(), "can't convert Prometheus value type", slog.F("value_type", m.Type), slog.Error(err))
56-
}
57-
constMetric := prometheus.MustNewConstMetric(desc, valueType, m.Value, annotated.username, annotated.workspaceName, annotated.agentName)
58-
ch <- constMetric
50+
for _, m := range ma.queue {
51+
desc := prometheus.NewDesc(m.Name, metricHelpForAgent, agentMetricsLabels, nil)
52+
valueType, err := asPrometheusValueType(m.Type)
53+
if err != nil {
54+
ma.log.Error(context.Background(), "can't convert Prometheus value type", slog.F("value_type", m.Type), slog.Error(err))
5955
}
56+
constMetric := prometheus.MustNewConstMetric(desc, valueType, m.Value, m.username, m.workspaceName, m.agentName)
57+
ch <- constMetric
6058
}
6159
}
6260

@@ -66,13 +64,23 @@ func (ma *MetricsAggregator) Update(_ context.Context, username, workspaceName,
6664
ma.m.Lock()
6765
defer ma.m.Unlock()
6866

69-
ma.queue = append(ma.queue, annotatedMetrics{
70-
username: username,
71-
workspaceName: workspaceName,
72-
agentName: agentName,
67+
UpdateLoop:
68+
for _, m := range metrics {
69+
for i, q := range ma.queue {
70+
if q.username == username && q.workspaceName == workspaceName && q.agentName == agentName && q.Name == m.Name {
71+
ma.queue[i].AgentMetric.Value = m.Value
72+
continue UpdateLoop
73+
}
74+
}
75+
76+
ma.queue = append(ma.queue, annotatedMetric{
77+
username: username,
78+
workspaceName: workspaceName,
79+
agentName: agentName,
7380

74-
metrics: metrics,
75-
})
81+
AgentMetric: m,
82+
})
83+
}
7684
}
7785

7886
func asPrometheusValueType(metricType agentsdk.AgentMetricType) (prometheus.ValueType, error) {

0 commit comments

Comments
 (0)