@@ -10,12 +10,15 @@ import (
10
10
"os"
11
11
"runtime"
12
12
"runtime/debug"
13
+ "strconv"
13
14
"sync"
14
15
"time"
15
16
16
17
"github.com/microsoft/ApplicationInsights-Go/appinsights"
17
18
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
19
+ "github.com/microsoft/retina/pkg/exporter"
18
20
"github.com/microsoft/retina/pkg/log"
21
+ io_prometheus_client "github.com/prometheus/client_model/go"
19
22
)
20
23
21
24
var (
@@ -174,10 +177,55 @@ func (t *TelemetryClient) heartbeat(ctx context.Context) {
174
177
if err != nil {
175
178
t .trackWarning (err , "failed to get cpu usage" )
176
179
}
180
+
181
+ metricscardinality , err := metricsCardinality ()
182
+ if err != nil {
183
+ t .trackWarning (err , "failed to get metrics cardinality" )
184
+ }
185
+
186
+ props ["metricscardinality" ] = strconv .Itoa (metricscardinality )
187
+
177
188
maps .Copy (props , cpuProps )
178
189
maps .Copy (props , t .profile .GetMemoryUsage ())
179
190
t .TrackEvent ("heartbeat" , props )
180
191
}
192
+ func metricsCardinality () (int , error ) {
193
+ metricFamilies , err := exporter .CombinedGatherer .Gather ()
194
+ if err != nil {
195
+ return 0 , fmt .Errorf ("failed to gather metrics: %w" , err )
196
+ }
197
+
198
+ metricscardinality := 0
199
+
200
+ for _ , mf := range metricFamilies {
201
+ switch mf .GetType () { //nolint:exhaustive // 'default' satisfies exhaustiveness
202
+
203
+ case io_prometheus_client .MetricType_HISTOGRAM :
204
+ metrics := mf .GetMetric ()
205
+ for _ , m := range metrics {
206
+ metricscardinality += len (m .GetHistogram ().GetBucket ()) + 3 // +3 for le="+Inf", _sum and _count
207
+ }
208
+
209
+ case io_prometheus_client .MetricType_GAUGE_HISTOGRAM :
210
+ metrics := mf .GetMetric ()
211
+ for _ , m := range metrics {
212
+ metricscardinality += len (m .GetHistogram ().GetBucket ()) + 3 // +3 for le="+Inf", _sum and _count
213
+ }
214
+
215
+ case io_prometheus_client .MetricType_SUMMARY :
216
+ metrics := mf .GetMetric ()
217
+ for _ , m := range metrics {
218
+ metricscardinality += len (m .GetSummary ().GetQuantile ()) + 2 // +2 for _sum and _count
219
+ }
220
+
221
+ default :
222
+ metricscardinality += len (mf .GetMetric ())
223
+
224
+ }
225
+ }
226
+
227
+ return metricscardinality , nil
228
+ }
181
229
182
230
func bToMb (b uint64 ) uint64 {
183
231
return b >> mbShift
0 commit comments