Prometheus metrics are encoded as text, not JSON (#1101)

Co-authored-by: Sanchit Mehta <sanchit.mehta602@gmail.com>
This commit is contained in:
fnuarnav
2023-04-06 00:03:43 -07:00
committed by GitHub
parent 9c32bfb0ae
commit 2c155accb7
7 changed files with 207 additions and 286 deletions

View File

@@ -1,11 +1,13 @@
package e2e
import (
"bytes"
"context"
"fmt"
"testing"
"time"
"github.com/prometheus/common/expfmt"
"github.com/virtual-kubelet/virtual-kubelet/internal/podutils"
stats "github.com/virtual-kubelet/virtual-kubelet/node/api/statsv1alpha1"
"gotest.tools/assert"
@@ -139,18 +141,26 @@ func (ts *EndToEndTestSuite) TestGetMetricsResource(t *testing.T) {
t.Fatal(err)
}
// decode metrics response bytes to metric family
reader := bytes.NewReader(metricsResourceResponse)
parser := expfmt.TextParser{}
metricsFamilyMap, err := parser.TextToMetricFamilies(reader)
if err != nil {
t.Fatal(err)
}
// Make sure the "nginx-" pod exists in the metrics returned.
currentContainerStatsCount := 0
found := false
for _, metricFamily := range metricsResourceResponse {
if *metricFamily.Name == "pod_cpu_usage_seconds_total" {
for metricName, metricFamily := range metricsFamilyMap {
if metricName == "pod_cpu_usage_seconds_total" {
for _, metric := range metricFamily.Metric {
if *metric.Label[1].Value == pod.Name {
found = true
}
}
}
if *metricFamily.Name == "container_cpu_usage_seconds_total" {
if metricName == "container_cpu_usage_seconds_total" {
for _, metric := range metricFamily.Metric {
if *metric.Label[1].Value == pod.Name {
currentContainerStatsCount += 1