Skip to content

Commit

Permalink
feat: add metricscardinality to heartbeat (#1235)
Browse files Browse the repository at this point in the history
# Description

Add `metricscardinality` to heartbeat. This will give visibility on the
number of time series being exposed by retina.

## Related Issue

#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]>
  • Loading branch information
alexcastilio authored Jan 23, 2025
1 parent c87f650 commit c5ea179
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"os"
"runtime"
"runtime/debug"
"strconv"
"sync"
"time"

"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
"github.com/microsoft/retina/pkg/exporter"
"github.com/microsoft/retina/pkg/log"
io_prometheus_client "github.com/prometheus/client_model/go"
)

var (
Expand Down Expand Up @@ -174,10 +177,55 @@ func (t *TelemetryClient) heartbeat(ctx context.Context) {
if err != nil {
t.trackWarning(err, "failed to get cpu usage")
}

metricscardinality, err := metricsCardinality()
if err != nil {
t.trackWarning(err, "failed to get metrics cardinality")
}

props["metricscardinality"] = strconv.Itoa(metricscardinality)

maps.Copy(props, cpuProps)
maps.Copy(props, t.profile.GetMemoryUsage())
t.TrackEvent("heartbeat", props)
}
func metricsCardinality() (int, error) {
metricFamilies, err := exporter.CombinedGatherer.Gather()
if err != nil {
return 0, fmt.Errorf("failed to gather metrics: %w", err)
}

metricscardinality := 0

for _, mf := range metricFamilies {
switch mf.GetType() { //nolint:exhaustive // 'default' satisfies exhaustiveness

case io_prometheus_client.MetricType_HISTOGRAM:
metrics := mf.GetMetric()
for _, m := range metrics {
metricscardinality += len(m.GetHistogram().GetBucket()) + 3 // +3 for le="+Inf", _sum and _count
}

case io_prometheus_client.MetricType_GAUGE_HISTOGRAM:
metrics := mf.GetMetric()
for _, m := range metrics {
metricscardinality += len(m.GetHistogram().GetBucket()) + 3 // +3 for le="+Inf", _sum and _count
}

case io_prometheus_client.MetricType_SUMMARY:
metrics := mf.GetMetric()
for _, m := range metrics {
metricscardinality += len(m.GetSummary().GetQuantile()) + 2 // +2 for _sum and _count
}

default:
metricscardinality += len(mf.GetMetric())

}
}

return metricscardinality, nil
}

func bToMb(b uint64) uint64 {
return b >> mbShift
Expand Down

0 comments on commit c5ea179

Please sign in to comment.