Skip to content

Commit

Permalink
feat: add metricscardinality to heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Castilio dos Santos <[email protected]>
  • Loading branch information
alexcastilio committed Jan 17, 2025
1 parent 2bdd493 commit 7af0632
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, err

Check failure on line 195 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (linux, amd64)

error returned from external package is unwrapped: sig: func (*github.com/prometheus/client_golang/prometheus.Registry).Gather() ([]*github.com/prometheus/client_model/go.MetricFamily, error) (wrapcheck)

Check failure on line 195 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (linux, arm64)

error returned from external package is unwrapped: sig: func (*github.com/prometheus/client_golang/prometheus.Registry).Gather() ([]*github.com/prometheus/client_model/go.MetricFamily, error) (wrapcheck)

Check failure on line 195 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

error returned from external package is unwrapped: sig: func (*github.com/prometheus/client_golang/prometheus.Registry).Gather() ([]*github.com/prometheus/client_model/go.MetricFamily, error) (wrapcheck)

Check failure on line 195 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

error returned from external package is unwrapped: sig: func (*github.com/prometheus/client_golang/prometheus.Registry).Gather() ([]*github.com/prometheus/client_model/go.MetricFamily, error) (wrapcheck)
}

metricscardinality := 0

for _, mf := range metricFamilies {
switch *mf.Type {

Check failure on line 201 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (linux, amd64)

avoid direct access to proto field *mf.Type, use mf.GetType() instead (protogetter)

Check failure on line 201 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (linux, arm64)

avoid direct access to proto field *mf.Type, use mf.GetType() instead (protogetter)

Check failure on line 201 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

avoid direct access to proto field *mf.Type, use mf.GetType() instead (protogetter)

Check failure on line 201 in pkg/telemetry/telemetry.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

avoid direct access to proto field *mf.Type, use mf.GetType() instead (protogetter)

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 7af0632

Please sign in to comment.