5
5
"time"
6
6
7
7
"github.com/prometheus/client_golang/prometheus"
8
+ "golang.org/x/exp/slices"
8
9
"golang.org/x/xerrors"
9
10
10
11
"cdr.dev/slog"
@@ -56,7 +57,6 @@ type annotatedMetric struct {
56
57
username string
57
58
workspaceName string
58
59
agentName string
59
- labels []agentsdk.AgentMetricLabel
60
60
61
61
expiryDate time.Time
62
62
}
@@ -123,7 +123,7 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
123
123
UpdateLoop:
124
124
for _ , m := range req .metrics {
125
125
for i , q := range ma .queue {
126
- if q .username == req .username && q .workspaceName == req .workspaceName && q .agentName == req .agentName && q .Name == m .Name && q . labels == m .Labels {
126
+ if q .username == req .username && q .workspaceName == req .workspaceName && q .agentName == req .agentName && q .Name == m .Name && slices . Equal ( q . Labels , m .Labels ) {
127
127
ma .queue [i ].AgentMetric .Value = m .Value
128
128
ma .queue [i ].expiryDate = req .timestamp .Add (ma .metricsCleanupInterval )
129
129
continue UpdateLoop
@@ -147,13 +147,40 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
147
147
148
148
output := make ([]prometheus.Metric , 0 , len (ma .queue ))
149
149
for _ , m := range ma .queue {
150
- desc := prometheus .NewDesc (m .Name , metricHelpForAgent , agentMetricsLabels , nil )
150
+ labels := make ([]string , len (agentMetricsLabels )+ len (m .Labels ))
151
+ var i int
152
+ for _ , l := range agentMetricsLabels {
153
+ labels [i ] = l
154
+ i ++
155
+ }
156
+
157
+ for _ , l := range m .Labels {
158
+ labels [i ] = l .Name
159
+ i ++
160
+ }
161
+
162
+ desc := prometheus .NewDesc (m .Name , metricHelpForAgent , labels , nil )
151
163
valueType , err := asPrometheusValueType (m .Type )
152
164
if err != nil {
153
165
ma .log .Error (ctx , "can't convert Prometheus value type" , slog .F ("name" , m .Name ), slog .F ("type" , m .Type ), slog .F ("value" , m .Value ), slog .Error (err ))
154
166
continue
155
167
}
156
- constMetric := prometheus .MustNewConstMetric (desc , valueType , m .Value , m .username , m .workspaceName , m .agentName )
168
+
169
+ labelValues := make ([]string , len (agentMetricsLabels )+ len (m .Labels ))
170
+ labelValues [0 ] = m .username
171
+ labelValues [1 ] = m .workspaceName
172
+ labelValues [2 ] = m .agentName
173
+
174
+ if len (m .Labels ) > 0 {
175
+ i = 3
176
+
177
+ for _ , l := range m .Labels {
178
+ labelValues [i ] = l .Value
179
+ i ++
180
+ }
181
+ }
182
+
183
+ constMetric := prometheus .MustNewConstMetric (desc , valueType , m .Value , labelValues ... )
157
184
output = append (output , constMetric )
158
185
}
159
186
outputCh <- output
0 commit comments