Skip to content

Commit

Permalink
[receiver/k8scluster] Convert clusterresourcequota to use pdata (open…
Browse files Browse the repository at this point in the history
…-telemetry#24416)

**Description:**

This converts the clusterresourcequota metrics of the k8scluster
receiver to use the pdata metric format instead of opencensus.

**Link to tracking Issue:**  open-telemetry#4367
  • Loading branch information
crobert-1 committed Jul 24, 2023
1 parent 13ff018 commit 25aa923
Show file tree
Hide file tree
Showing 15 changed files with 1,149 additions and 234 deletions.
21 changes: 21 additions & 0 deletions .chloggen/k8s-cluster-receiver-clusterresourcequota.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: "k8sclusterreceiver"

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Change k8s.clusterresourcequota metrics to use mdatagen"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [4367]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package clusterresourcequota // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/clusterresourcequota"

import (
"strings"
"time"

quotav1 "github.com/openshift/api/quota/v1"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

imetadataphase "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata"
)

func GetMetrics(set receiver.CreateSettings, crq *quotav1.ClusterResourceQuota) pmetric.Metrics {
mbphase := imetadataphase.NewMetricsBuilder(imetadataphase.DefaultMetricsBuilderConfig(), set)
ts := pcommon.NewTimestampFromTime(time.Now())

for k, v := range crq.Status.Total.Hard {
val := extractValue(k, v)
mbphase.RecordOpenshiftClusterquotaLimitDataPoint(ts, val, string(k))
}

for k, v := range crq.Status.Total.Used {
val := extractValue(k, v)
mbphase.RecordOpenshiftClusterquotaUsedDataPoint(ts, val, string(k))
}

for _, ns := range crq.Status.Namespaces {
for k, v := range ns.Status.Hard {
val := extractValue(k, v)
mbphase.RecordOpenshiftAppliedclusterquotaLimitDataPoint(ts, val, ns.Namespace, string(k))
}

for k, v := range ns.Status.Used {
val := extractValue(k, v)
mbphase.RecordOpenshiftAppliedclusterquotaUsedDataPoint(ts, val, ns.Namespace, string(k))
}
}

return mbphase.Emit(imetadataphase.WithOpenshiftClusterquotaName(crq.Name), imetadataphase.WithOpenshiftClusterquotaUID(string(crq.UID)), imetadataphase.WithOpencensusResourcetype("k8s"))
}

func extractValue(k v1.ResourceName, v resource.Quantity) int64 {
val := v.Value()
if strings.HasSuffix(string(k), ".cpu") {
val = v.MilliValue()
}
return val
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,132 +4,30 @@
package clusterresourcequota

import (
"path/filepath"
"testing"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
quotav1 "github.com/openshift/api/quota/v1"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/testutils"
)

func TestClusterRequestQuotaMetrics(t *testing.T) {
rq := newMockClusterResourceQuota("1")

actualResourceMetrics := GetMetrics(rq)

require.Equal(t, 1, len(actualResourceMetrics))

metrics := actualResourceMetrics[0].Metrics
require.Equal(t, 6, len(metrics))
testutils.AssertResource(t, actualResourceMetrics[0].Resource, constants.K8sType,
map[string]string{
"openshift.clusterquota.uid": "test-clusterquota-1-uid",
"openshift.clusterquota.name": "test-clusterquota-1",
},
crq := testutils.NewClusterResourceQuota("1")

m := GetMetrics(receivertest.NewNopCreateSettings(), crq)

expected, err := golden.ReadMetrics(filepath.Join("testdata", "expected.yaml"))
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expected, m,
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
),
)

for i, tc := range []struct {
name string
value int64
labels map[string]string
}{
{
"openshift.clusterquota.limit",
10000,
map[string]string{
"resource": "requests.cpu",
},
},
{
"openshift.clusterquota.used",
6000,
map[string]string{
"resource": "requests.cpu",
},
},
{
"openshift.appliedclusterquota.limit",
6000,
map[string]string{
"resource": "requests.cpu",
"k8s.namespace.name": "ns1",
},
},
{
"openshift.appliedclusterquota.used",
1000,
map[string]string{
"resource": "requests.cpu",
"k8s.namespace.name": "ns1",
},
},
{
"openshift.appliedclusterquota.limit",
4000,
map[string]string{
"resource": "requests.cpu",
"k8s.namespace.name": "ns2",
},
},
{
"openshift.appliedclusterquota.used",
5000,
map[string]string{
"resource": "requests.cpu",
"k8s.namespace.name": "ns2",
},
},
} {
testutils.AssertMetricsWithLabels(t, metrics[i], tc.name,
metricspb.MetricDescriptor_GAUGE_INT64, tc.labels, tc.value)
}
}

func newMockClusterResourceQuota(id string) *quotav1.ClusterResourceQuota {
return &quotav1.ClusterResourceQuota{
ObjectMeta: v1.ObjectMeta{
Name: "test-clusterquota-" + id,
UID: types.UID("test-clusterquota-" + id + "-uid"),
},
Status: quotav1.ClusterResourceQuotaStatus{
Total: corev1.ResourceQuotaStatus{
Hard: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(10, resource.DecimalSI),
},
Used: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(6, resource.DecimalSI),
},
},
Namespaces: quotav1.ResourceQuotasStatusByNamespace{
{
Namespace: "ns1",
Status: corev1.ResourceQuotaStatus{
Hard: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(6, resource.DecimalSI),
},
Used: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(1, resource.DecimalSI),
},
},
},
{
Namespace: "ns2",
Status: corev1.ResourceQuotaStatus{
Hard: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(4, resource.DecimalSI),
},
Used: corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(5, resource.DecimalSI),
},
},
},
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

package clusterresourcequota // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/clusterresourcequota"
Loading

0 comments on commit 25aa923

Please sign in to comment.