Skip to content

Commit c5ea179

Browse files
authored
feat: add metricscardinality to heartbeat (microsoft#1235)
# Description Add `metricscardinality` to heartbeat. This will give visibility on the number of time series being exposed by retina. ## Related Issue microsoft#1040 ## Checklist - [x] I have read the [contributing documentation](https://retina.sh/docs/contributing). - [x] I signed and signed-off the commits (`git commit -S -s ...`). See [this documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification) on signing commits. - [x] I have correctly attributed the author(s) of the code. - [x] I have tested the changes locally. - [x] I have followed the project's style guidelines. - [x] I have updated the documentation, if necessary. - [x] I have added tests, if applicable. ## Screenshots (if applicable) or Testing Completed Metrics exported with heartbeat: ![image](https://github.com/user-attachments/assets/6fb4d76b-5780-4751-91c0-46e3c4f0fb85) ## Additional Notes Metrics of types `histogram` and `summary` expose multiple time series during a scrape. Code is counting according to number of time series exposed at /metrics endpoint. Ref: https://prometheus.io/docs/concepts/metric_types/#histogram https://prometheus.io/docs/concepts/metric_types/#summary --- Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more information on how to contribute to this project. Signed-off-by: Alex Castilio dos Santos <[email protected]>
1 parent c87f650 commit c5ea179

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

pkg/telemetry/telemetry.go

+48
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import (
1010
"os"
1111
"runtime"
1212
"runtime/debug"
13+
"strconv"
1314
"sync"
1415
"time"
1516

1617
"github.com/microsoft/ApplicationInsights-Go/appinsights"
1718
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
19+
"github.com/microsoft/retina/pkg/exporter"
1820
"github.com/microsoft/retina/pkg/log"
21+
io_prometheus_client "github.com/prometheus/client_model/go"
1922
)
2023

2124
var (
@@ -174,10 +177,55 @@ func (t *TelemetryClient) heartbeat(ctx context.Context) {
174177
if err != nil {
175178
t.trackWarning(err, "failed to get cpu usage")
176179
}
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+
177188
maps.Copy(props, cpuProps)
178189
maps.Copy(props, t.profile.GetMemoryUsage())
179190
t.TrackEvent("heartbeat", props)
180191
}
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+
}
181229

182230
func bToMb(b uint64) uint64 {
183231
return b >> mbShift

0 commit comments

Comments
 (0)