From c6d089090c7941a7235fab751d793fbe469adce0 Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Fri, 11 Oct 2024 15:08:07 +0800 Subject: [PATCH] metrics: Use UnmarshalTo instead of UnmarshalAny Co-authored-by: Sam Lockart Signed-off-by: Iceber Gu --- core/metrics/cgroups/v1/metrics.go | 10 +++------- core/metrics/cgroups/v2/metrics.go | 9 ++------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/core/metrics/cgroups/v1/metrics.go b/core/metrics/cgroups/v1/metrics.go index bdc137c79..dd2c50957 100644 --- a/core/metrics/cgroups/v1/metrics.go +++ b/core/metrics/cgroups/v1/metrics.go @@ -148,16 +148,12 @@ func (c *Collector) collect(entry entry, ch chan<- prometheus.Metric, block bool return } - data, err := typeurl.UnmarshalAny(stats) - if err != nil { + s := &v1.Metrics{} + if err := typeurl.UnmarshalTo(stats, s); err != nil { log.L.WithError(err).Errorf("unmarshal stats for %s", t.ID()) return } - s, ok := data.(*v1.Metrics) - if !ok { - log.L.WithError(err).Errorf("invalid metric type for %s", t.ID()) - return - } + ns := entry.ns if ns == nil { ns = c.ns diff --git a/core/metrics/cgroups/v2/metrics.go b/core/metrics/cgroups/v2/metrics.go index a92c235a9..29916326c 100644 --- a/core/metrics/cgroups/v2/metrics.go +++ b/core/metrics/cgroups/v2/metrics.go @@ -141,16 +141,11 @@ func (c *Collector) collect(entry entry, ch chan<- prometheus.Metric, block bool return } - data, err := typeurl.UnmarshalAny(stats) - if err != nil { + s := &v2.Metrics{} + if err := typeurl.UnmarshalTo(stats, s); err != nil { log.L.WithError(err).Errorf("unmarshal stats for %s", t.ID()) return } - s, ok := data.(*v2.Metrics) - if !ok { - log.L.WithError(err).Errorf("invalid metric type for %s", t.ID()) - return - } ns := entry.ns if ns == nil { ns = c.ns