diff --git a/.chloggen/k8s-cluster-receiver-clusterresourcequota.yaml b/.chloggen/k8s-cluster-receiver-clusterresourcequota.yaml new file mode 100644 index 000000000000..3b7e69e75a8c --- /dev/null +++ b/.chloggen/k8s-cluster-receiver-clusterresourcequota.yaml @@ -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: \ No newline at end of file diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusteresourcequotas.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusteresourcequotas.go deleted file mode 100644 index dc77c0d5f5d4..000000000000 --- a/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusteresourcequotas.go +++ /dev/null @@ -1,114 +0,0 @@ -// 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" - - agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1" - metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" - resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" - quotav1 "github.com/openshift/api/quota/v1" - conventions "go.opentelemetry.io/collector/semconv/v1.6.1" - corev1 "k8s.io/api/core/v1" - - "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants" - "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils" -) - -var clusterResourceQuotaLimitMetric = &metricspb.MetricDescriptor{ - Name: "openshift.clusterquota.limit", - Description: "The configured upper limit for a particular resource.", - Type: metricspb.MetricDescriptor_GAUGE_INT64, - LabelKeys: []*metricspb.LabelKey{{ - Key: "resource", - }}, -} - -var clusterResourceQuotaUsedMetric = &metricspb.MetricDescriptor{ - Name: "openshift.clusterquota.used", - Description: "The usage for a particular resource with a configured limit.", - Type: metricspb.MetricDescriptor_GAUGE_INT64, - LabelKeys: []*metricspb.LabelKey{{ - Key: "resource", - }}, -} - -var appliedClusterResourceQuotaLimitMetric = &metricspb.MetricDescriptor{ - Name: "openshift.appliedclusterquota.limit", - Description: "The upper limit for a particular resource in a specific namespace.", - Type: metricspb.MetricDescriptor_GAUGE_INT64, - LabelKeys: []*metricspb.LabelKey{ - { - Key: "resource", - }, - { - Key: conventions.AttributeK8SNamespaceName, - }, - }, -} - -var appliedClusterResourceQuotaUsedMetric = &metricspb.MetricDescriptor{ - Name: "openshift.appliedclusterquota.used", - Description: "The usage for a particular resource in a specific namespace.", - Type: metricspb.MetricDescriptor_GAUGE_INT64, - LabelKeys: []*metricspb.LabelKey{ - { - Key: "resource", - }, - { - Key: conventions.AttributeK8SNamespaceName, - }, - }, -} - -func GetMetrics(rq *quotav1.ClusterResourceQuota) []*agentmetricspb.ExportMetricsServiceRequest { - var metrics []*metricspb.Metric - - metrics = appendClusterQuotaMetrics(metrics, clusterResourceQuotaLimitMetric, rq.Status.Total.Hard, "") - metrics = appendClusterQuotaMetrics(metrics, clusterResourceQuotaUsedMetric, rq.Status.Total.Used, "") - for _, ns := range rq.Status.Namespaces { - metrics = appendClusterQuotaMetrics(metrics, appliedClusterResourceQuotaLimitMetric, ns.Status.Hard, ns.Namespace) - metrics = appendClusterQuotaMetrics(metrics, appliedClusterResourceQuotaUsedMetric, ns.Status.Used, ns.Namespace) - } - return []*agentmetricspb.ExportMetricsServiceRequest{ - { - Resource: getResource(rq), - Metrics: metrics, - }, - } -} - -func appendClusterQuotaMetrics(metrics []*metricspb.Metric, metric *metricspb.MetricDescriptor, rl corev1.ResourceList, namespace string) []*metricspb.Metric { - for k, v := range rl { - val := v.Value() - if strings.HasSuffix(string(k), ".cpu") { - val = v.MilliValue() - } - - labels := []*metricspb.LabelValue{{Value: string(k), HasValue: true}} - if namespace != "" { - labels = append(labels, &metricspb.LabelValue{Value: namespace, HasValue: true}) - } - metrics = append(metrics, - &metricspb.Metric{ - MetricDescriptor: metric, - Timeseries: []*metricspb.TimeSeries{ - utils.GetInt64TimeSeriesWithLabels(val, labels), - }, - }, - ) - } - return metrics -} - -func getResource(rq *quotav1.ClusterResourceQuota) *resourcepb.Resource { - return &resourcepb.Resource{ - Type: constants.K8sType, - Labels: map[string]string{ - constants.K8sKeyClusterResourceQuotaUID: string(rq.UID), - constants.K8sKeyClusterResourceQuotaName: rq.Name, - }, - } -} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas.go new file mode 100644 index 000000000000..687dac5c6cb4 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas.go @@ -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 +} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas_test.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas_test.go index d3bdb6b56608..e0edf7110919 100644 --- a/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas_test.go +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/clusterresourcequotas_test.go @@ -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 "av1.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), - }, - }, - }, - }, - }, - } } diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/doc.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/doc.go new file mode 100644 index 000000000000..a30fe432c28d --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/doc.go @@ -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" diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/documentation.md b/receiver/k8sclusterreceiver/internal/clusterresourcequota/documentation.md new file mode 100644 index 000000000000..cc54ab308380 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/documentation.md @@ -0,0 +1,81 @@ +[comment]: <> (Code generated by mdatagen. DO NOT EDIT.) + +# k8s/clusterresourcequota + +**Parent Component:** k8s_cluster + +## Default Metrics + +The following metrics are emitted by default. Each of them can be disabled by applying the following configuration: + +```yaml +metrics: + : + enabled: false +``` + +### openshift.appliedclusterquota.limit + +The upper limit for a particular resource in a specific namespace. + +| Unit | Metric Type | Value Type | +| ---- | ----------- | ---------- | +| 1 | Gauge | Int | + +#### Attributes + +| Name | Description | Values | +| ---- | ----------- | ------ | +| k8s.namespace.name | The k8s namespace name. | Any Str | +| resource | The name of the resource on which the cluster quota is applied | Any Str | + +### openshift.appliedclusterquota.used + +The usage for a particular resource in a specific namespace. + +| Unit | Metric Type | Value Type | +| ---- | ----------- | ---------- | +| 1 | Gauge | Int | + +#### Attributes + +| Name | Description | Values | +| ---- | ----------- | ------ | +| k8s.namespace.name | The k8s namespace name. | Any Str | +| resource | The name of the resource on which the cluster quota is applied | Any Str | + +### openshift.clusterquota.limit + +The configured upper limit for a particular resource. + +| Unit | Metric Type | Value Type | +| ---- | ----------- | ---------- | +| 1 | Gauge | Int | + +#### Attributes + +| Name | Description | Values | +| ---- | ----------- | ------ | +| resource | The name of the resource on which the cluster quota is applied | Any Str | + +### openshift.clusterquota.used + +The usage for a particular resource with a configured limit. + +| Unit | Metric Type | Value Type | +| ---- | ----------- | ---------- | +| 1 | Gauge | Int | + +#### Attributes + +| Name | Description | Values | +| ---- | ----------- | ------ | +| resource | The name of the resource on which the cluster quota is applied | Any Str | + +## Resource Attributes + +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| opencensus.resourcetype | The OpenCensus resource type. | Any Str | true | +| openshift.clusterquota.name | The k8s ClusterResourceQuota name. | Any Str | true | +| openshift.clusterquota.uid | The k8s ClusterResourceQuota uid. | Any Str | true | diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config.go new file mode 100644 index 000000000000..cee0b047101b --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config.go @@ -0,0 +1,88 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import "go.opentelemetry.io/collector/confmap" + +// MetricConfig provides common config for a particular metric. +type MetricConfig struct { + Enabled bool `mapstructure:"enabled"` + + enabledSetByUser bool +} + +func (ms *MetricConfig) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ms, confmap.WithErrorUnused()) + if err != nil { + return err + } + ms.enabledSetByUser = parser.IsSet("enabled") + return nil +} + +// MetricsConfig provides config for k8s/clusterresourcequota metrics. +type MetricsConfig struct { + OpenshiftAppliedclusterquotaLimit MetricConfig `mapstructure:"openshift.appliedclusterquota.limit"` + OpenshiftAppliedclusterquotaUsed MetricConfig `mapstructure:"openshift.appliedclusterquota.used"` + OpenshiftClusterquotaLimit MetricConfig `mapstructure:"openshift.clusterquota.limit"` + OpenshiftClusterquotaUsed MetricConfig `mapstructure:"openshift.clusterquota.used"` +} + +func DefaultMetricsConfig() MetricsConfig { + return MetricsConfig{ + OpenshiftAppliedclusterquotaLimit: MetricConfig{ + Enabled: true, + }, + OpenshiftAppliedclusterquotaUsed: MetricConfig{ + Enabled: true, + }, + OpenshiftClusterquotaLimit: MetricConfig{ + Enabled: true, + }, + OpenshiftClusterquotaUsed: MetricConfig{ + Enabled: true, + }, + } +} + +// ResourceAttributeConfig provides common config for a particular resource attribute. +type ResourceAttributeConfig struct { + Enabled bool `mapstructure:"enabled"` +} + +// ResourceAttributesConfig provides config for k8s/clusterresourcequota resource attributes. +type ResourceAttributesConfig struct { + OpencensusResourcetype ResourceAttributeConfig `mapstructure:"opencensus.resourcetype"` + OpenshiftClusterquotaName ResourceAttributeConfig `mapstructure:"openshift.clusterquota.name"` + OpenshiftClusterquotaUID ResourceAttributeConfig `mapstructure:"openshift.clusterquota.uid"` +} + +func DefaultResourceAttributesConfig() ResourceAttributesConfig { + return ResourceAttributesConfig{ + OpencensusResourcetype: ResourceAttributeConfig{ + Enabled: true, + }, + OpenshiftClusterquotaName: ResourceAttributeConfig{ + Enabled: true, + }, + OpenshiftClusterquotaUID: ResourceAttributeConfig{ + Enabled: true, + }, + } +} + +// MetricsBuilderConfig is a configuration for k8s/clusterresourcequota metrics builder. +type MetricsBuilderConfig struct { + Metrics MetricsConfig `mapstructure:"metrics"` + ResourceAttributes ResourceAttributesConfig `mapstructure:"resource_attributes"` +} + +func DefaultMetricsBuilderConfig() MetricsBuilderConfig { + return MetricsBuilderConfig{ + Metrics: DefaultMetricsConfig(), + ResourceAttributes: DefaultResourceAttributesConfig(), + } +} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config_test.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config_test.go new file mode 100644 index 000000000000..5758ce83ebc8 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_config_test.go @@ -0,0 +1,76 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "path/filepath" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +func TestMetricsBuilderConfig(t *testing.T) { + tests := []struct { + name string + want MetricsBuilderConfig + }{ + { + name: "default", + want: DefaultMetricsBuilderConfig(), + }, + { + name: "all_set", + want: MetricsBuilderConfig{ + Metrics: MetricsConfig{ + OpenshiftAppliedclusterquotaLimit: MetricConfig{Enabled: true}, + OpenshiftAppliedclusterquotaUsed: MetricConfig{Enabled: true}, + OpenshiftClusterquotaLimit: MetricConfig{Enabled: true}, + OpenshiftClusterquotaUsed: MetricConfig{Enabled: true}, + }, + ResourceAttributes: ResourceAttributesConfig{ + OpencensusResourcetype: ResourceAttributeConfig{Enabled: true}, + OpenshiftClusterquotaName: ResourceAttributeConfig{Enabled: true}, + OpenshiftClusterquotaUID: ResourceAttributeConfig{Enabled: true}, + }, + }, + }, + { + name: "none_set", + want: MetricsBuilderConfig{ + Metrics: MetricsConfig{ + OpenshiftAppliedclusterquotaLimit: MetricConfig{Enabled: false}, + OpenshiftAppliedclusterquotaUsed: MetricConfig{Enabled: false}, + OpenshiftClusterquotaLimit: MetricConfig{Enabled: false}, + OpenshiftClusterquotaUsed: MetricConfig{Enabled: false}, + }, + ResourceAttributes: ResourceAttributesConfig{ + OpencensusResourcetype: ResourceAttributeConfig{Enabled: false}, + OpenshiftClusterquotaName: ResourceAttributeConfig{Enabled: false}, + OpenshiftClusterquotaUID: ResourceAttributeConfig{Enabled: false}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := loadMetricsBuilderConfig(t, tt.name) + if diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(MetricConfig{}, ResourceAttributeConfig{})); diff != "" { + t.Errorf("Config mismatch (-expected +actual):\n%s", diff) + } + }) + } +} + +func loadMetricsBuilderConfig(t *testing.T, name string) MetricsBuilderConfig { + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) + require.NoError(t, err) + sub, err := cm.Sub(name) + require.NoError(t, err) + cfg := DefaultMetricsBuilderConfig() + require.NoError(t, component.UnmarshalConfig(sub, &cfg)) + return cfg +} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics.go new file mode 100644 index 000000000000..08e3431d9572 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics.go @@ -0,0 +1,387 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "time" + + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" + "go.opentelemetry.io/collector/receiver" + conventions "go.opentelemetry.io/collector/semconv/v1.18.0" +) + +type metricOpenshiftAppliedclusterquotaLimit struct { + data pmetric.Metric // data buffer for generated metric. + config MetricConfig // metric config provided by user. + capacity int // max observed number of data points added to the metric. +} + +// init fills openshift.appliedclusterquota.limit metric with initial data. +func (m *metricOpenshiftAppliedclusterquotaLimit) init() { + m.data.SetName("openshift.appliedclusterquota.limit") + m.data.SetDescription("The upper limit for a particular resource in a specific namespace.") + m.data.SetUnit("1") + m.data.SetEmptyGauge() + m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) +} + +func (m *metricOpenshiftAppliedclusterquotaLimit) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, k8sNamespaceNameAttributeValue string, resourceAttributeValue string) { + if !m.config.Enabled { + return + } + dp := m.data.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(start) + dp.SetTimestamp(ts) + dp.SetIntValue(val) + dp.Attributes().PutStr("k8s.namespace.name", k8sNamespaceNameAttributeValue) + dp.Attributes().PutStr("resource", resourceAttributeValue) +} + +// updateCapacity saves max length of data point slices that will be used for the slice capacity. +func (m *metricOpenshiftAppliedclusterquotaLimit) updateCapacity() { + if m.data.Gauge().DataPoints().Len() > m.capacity { + m.capacity = m.data.Gauge().DataPoints().Len() + } +} + +// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. +func (m *metricOpenshiftAppliedclusterquotaLimit) emit(metrics pmetric.MetricSlice) { + if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + m.updateCapacity() + m.data.MoveTo(metrics.AppendEmpty()) + m.init() + } +} + +func newMetricOpenshiftAppliedclusterquotaLimit(cfg MetricConfig) metricOpenshiftAppliedclusterquotaLimit { + m := metricOpenshiftAppliedclusterquotaLimit{config: cfg} + if cfg.Enabled { + m.data = pmetric.NewMetric() + m.init() + } + return m +} + +type metricOpenshiftAppliedclusterquotaUsed struct { + data pmetric.Metric // data buffer for generated metric. + config MetricConfig // metric config provided by user. + capacity int // max observed number of data points added to the metric. +} + +// init fills openshift.appliedclusterquota.used metric with initial data. +func (m *metricOpenshiftAppliedclusterquotaUsed) init() { + m.data.SetName("openshift.appliedclusterquota.used") + m.data.SetDescription("The usage for a particular resource in a specific namespace.") + m.data.SetUnit("1") + m.data.SetEmptyGauge() + m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) +} + +func (m *metricOpenshiftAppliedclusterquotaUsed) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, k8sNamespaceNameAttributeValue string, resourceAttributeValue string) { + if !m.config.Enabled { + return + } + dp := m.data.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(start) + dp.SetTimestamp(ts) + dp.SetIntValue(val) + dp.Attributes().PutStr("k8s.namespace.name", k8sNamespaceNameAttributeValue) + dp.Attributes().PutStr("resource", resourceAttributeValue) +} + +// updateCapacity saves max length of data point slices that will be used for the slice capacity. +func (m *metricOpenshiftAppliedclusterquotaUsed) updateCapacity() { + if m.data.Gauge().DataPoints().Len() > m.capacity { + m.capacity = m.data.Gauge().DataPoints().Len() + } +} + +// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. +func (m *metricOpenshiftAppliedclusterquotaUsed) emit(metrics pmetric.MetricSlice) { + if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + m.updateCapacity() + m.data.MoveTo(metrics.AppendEmpty()) + m.init() + } +} + +func newMetricOpenshiftAppliedclusterquotaUsed(cfg MetricConfig) metricOpenshiftAppliedclusterquotaUsed { + m := metricOpenshiftAppliedclusterquotaUsed{config: cfg} + if cfg.Enabled { + m.data = pmetric.NewMetric() + m.init() + } + return m +} + +type metricOpenshiftClusterquotaLimit struct { + data pmetric.Metric // data buffer for generated metric. + config MetricConfig // metric config provided by user. + capacity int // max observed number of data points added to the metric. +} + +// init fills openshift.clusterquota.limit metric with initial data. +func (m *metricOpenshiftClusterquotaLimit) init() { + m.data.SetName("openshift.clusterquota.limit") + m.data.SetDescription("The configured upper limit for a particular resource.") + m.data.SetUnit("1") + m.data.SetEmptyGauge() + m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) +} + +func (m *metricOpenshiftClusterquotaLimit) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, resourceAttributeValue string) { + if !m.config.Enabled { + return + } + dp := m.data.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(start) + dp.SetTimestamp(ts) + dp.SetIntValue(val) + dp.Attributes().PutStr("resource", resourceAttributeValue) +} + +// updateCapacity saves max length of data point slices that will be used for the slice capacity. +func (m *metricOpenshiftClusterquotaLimit) updateCapacity() { + if m.data.Gauge().DataPoints().Len() > m.capacity { + m.capacity = m.data.Gauge().DataPoints().Len() + } +} + +// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. +func (m *metricOpenshiftClusterquotaLimit) emit(metrics pmetric.MetricSlice) { + if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + m.updateCapacity() + m.data.MoveTo(metrics.AppendEmpty()) + m.init() + } +} + +func newMetricOpenshiftClusterquotaLimit(cfg MetricConfig) metricOpenshiftClusterquotaLimit { + m := metricOpenshiftClusterquotaLimit{config: cfg} + if cfg.Enabled { + m.data = pmetric.NewMetric() + m.init() + } + return m +} + +type metricOpenshiftClusterquotaUsed struct { + data pmetric.Metric // data buffer for generated metric. + config MetricConfig // metric config provided by user. + capacity int // max observed number of data points added to the metric. +} + +// init fills openshift.clusterquota.used metric with initial data. +func (m *metricOpenshiftClusterquotaUsed) init() { + m.data.SetName("openshift.clusterquota.used") + m.data.SetDescription("The usage for a particular resource with a configured limit.") + m.data.SetUnit("1") + m.data.SetEmptyGauge() + m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) +} + +func (m *metricOpenshiftClusterquotaUsed) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, resourceAttributeValue string) { + if !m.config.Enabled { + return + } + dp := m.data.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(start) + dp.SetTimestamp(ts) + dp.SetIntValue(val) + dp.Attributes().PutStr("resource", resourceAttributeValue) +} + +// updateCapacity saves max length of data point slices that will be used for the slice capacity. +func (m *metricOpenshiftClusterquotaUsed) updateCapacity() { + if m.data.Gauge().DataPoints().Len() > m.capacity { + m.capacity = m.data.Gauge().DataPoints().Len() + } +} + +// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. +func (m *metricOpenshiftClusterquotaUsed) emit(metrics pmetric.MetricSlice) { + if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + m.updateCapacity() + m.data.MoveTo(metrics.AppendEmpty()) + m.init() + } +} + +func newMetricOpenshiftClusterquotaUsed(cfg MetricConfig) metricOpenshiftClusterquotaUsed { + m := metricOpenshiftClusterquotaUsed{config: cfg} + if cfg.Enabled { + m.data = pmetric.NewMetric() + m.init() + } + return m +} + +// MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations +// required to produce metric representation defined in metadata and user config. +type MetricsBuilder struct { + startTime pcommon.Timestamp // start time that will be applied to all recorded data points. + metricsCapacity int // maximum observed number of metrics per resource. + resourceCapacity int // maximum observed number of resource attributes. + metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. + buildInfo component.BuildInfo // contains version information + resourceAttributesConfig ResourceAttributesConfig + metricOpenshiftAppliedclusterquotaLimit metricOpenshiftAppliedclusterquotaLimit + metricOpenshiftAppliedclusterquotaUsed metricOpenshiftAppliedclusterquotaUsed + metricOpenshiftClusterquotaLimit metricOpenshiftClusterquotaLimit + metricOpenshiftClusterquotaUsed metricOpenshiftClusterquotaUsed +} + +// metricBuilderOption applies changes to default metrics builder. +type metricBuilderOption func(*MetricsBuilder) + +// WithStartTime sets startTime on the metrics builder. +func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.startTime = startTime + } +} + +func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { + mb := &MetricsBuilder{ + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + resourceAttributesConfig: mbc.ResourceAttributes, + metricOpenshiftAppliedclusterquotaLimit: newMetricOpenshiftAppliedclusterquotaLimit(mbc.Metrics.OpenshiftAppliedclusterquotaLimit), + metricOpenshiftAppliedclusterquotaUsed: newMetricOpenshiftAppliedclusterquotaUsed(mbc.Metrics.OpenshiftAppliedclusterquotaUsed), + metricOpenshiftClusterquotaLimit: newMetricOpenshiftClusterquotaLimit(mbc.Metrics.OpenshiftClusterquotaLimit), + metricOpenshiftClusterquotaUsed: newMetricOpenshiftClusterquotaUsed(mbc.Metrics.OpenshiftClusterquotaUsed), + } + for _, op := range options { + op(mb) + } + return mb +} + +// updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity. +func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { + if mb.metricsCapacity < rm.ScopeMetrics().At(0).Metrics().Len() { + mb.metricsCapacity = rm.ScopeMetrics().At(0).Metrics().Len() + } + if mb.resourceCapacity < rm.Resource().Attributes().Len() { + mb.resourceCapacity = rm.Resource().Attributes().Len() + } +} + +// ResourceMetricsOption applies changes to provided resource metrics. +type ResourceMetricsOption func(ResourceAttributesConfig, pmetric.ResourceMetrics) + +// WithOpencensusResourcetype sets provided value as "opencensus.resourcetype" attribute for current resource. +func WithOpencensusResourcetype(val string) ResourceMetricsOption { + return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { + if rac.OpencensusResourcetype.Enabled { + rm.Resource().Attributes().PutStr("opencensus.resourcetype", val) + } + } +} + +// WithOpenshiftClusterquotaName sets provided value as "openshift.clusterquota.name" attribute for current resource. +func WithOpenshiftClusterquotaName(val string) ResourceMetricsOption { + return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { + if rac.OpenshiftClusterquotaName.Enabled { + rm.Resource().Attributes().PutStr("openshift.clusterquota.name", val) + } + } +} + +// WithOpenshiftClusterquotaUID sets provided value as "openshift.clusterquota.uid" attribute for current resource. +func WithOpenshiftClusterquotaUID(val string) ResourceMetricsOption { + return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { + if rac.OpenshiftClusterquotaUID.Enabled { + rm.Resource().Attributes().PutStr("openshift.clusterquota.uid", val) + } + } +} + +// WithStartTimeOverride overrides start time for all the resource metrics data points. +// This option should be only used if different start time has to be set on metrics coming from different resources. +func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { + return func(_ ResourceAttributesConfig, rm pmetric.ResourceMetrics) { + var dps pmetric.NumberDataPointSlice + metrics := rm.ScopeMetrics().At(0).Metrics() + for i := 0; i < metrics.Len(); i++ { + switch metrics.At(i).Type() { + case pmetric.MetricTypeGauge: + dps = metrics.At(i).Gauge().DataPoints() + case pmetric.MetricTypeSum: + dps = metrics.At(i).Sum().DataPoints() + } + for j := 0; j < dps.Len(); j++ { + dps.At(j).SetStartTimestamp(start) + } + } + } +} + +// EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for +// recording another set of data points as part of another resource. This function can be helpful when one scraper +// needs to emit metrics from several resources. Otherwise calling this function is not required, +// just `Emit` function can be called instead. +// Resource attributes should be provided as ResourceMetricsOption arguments. +func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { + rm := pmetric.NewResourceMetrics() + rm.SetSchemaUrl(conventions.SchemaURL) + rm.Resource().Attributes().EnsureCapacity(mb.resourceCapacity) + ils := rm.ScopeMetrics().AppendEmpty() + ils.Scope().SetName("otelcol/k8sclusterreceiver") + ils.Scope().SetVersion(mb.buildInfo.Version) + ils.Metrics().EnsureCapacity(mb.metricsCapacity) + mb.metricOpenshiftAppliedclusterquotaLimit.emit(ils.Metrics()) + mb.metricOpenshiftAppliedclusterquotaUsed.emit(ils.Metrics()) + mb.metricOpenshiftClusterquotaLimit.emit(ils.Metrics()) + mb.metricOpenshiftClusterquotaUsed.emit(ils.Metrics()) + + for _, op := range rmo { + op(mb.resourceAttributesConfig, rm) + } + if ils.Metrics().Len() > 0 { + mb.updateCapacity(rm) + rm.MoveTo(mb.metricsBuffer.ResourceMetrics().AppendEmpty()) + } +} + +// Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for +// recording another set of metrics. This function will be responsible for applying all the transformations required to +// produce metric representation defined in metadata and user config, e.g. delta or cumulative. +func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics { + mb.EmitForResource(rmo...) + metrics := mb.metricsBuffer + mb.metricsBuffer = pmetric.NewMetrics() + return metrics +} + +// RecordOpenshiftAppliedclusterquotaLimitDataPoint adds a data point to openshift.appliedclusterquota.limit metric. +func (mb *MetricsBuilder) RecordOpenshiftAppliedclusterquotaLimitDataPoint(ts pcommon.Timestamp, val int64, k8sNamespaceNameAttributeValue string, resourceAttributeValue string) { + mb.metricOpenshiftAppliedclusterquotaLimit.recordDataPoint(mb.startTime, ts, val, k8sNamespaceNameAttributeValue, resourceAttributeValue) +} + +// RecordOpenshiftAppliedclusterquotaUsedDataPoint adds a data point to openshift.appliedclusterquota.used metric. +func (mb *MetricsBuilder) RecordOpenshiftAppliedclusterquotaUsedDataPoint(ts pcommon.Timestamp, val int64, k8sNamespaceNameAttributeValue string, resourceAttributeValue string) { + mb.metricOpenshiftAppliedclusterquotaUsed.recordDataPoint(mb.startTime, ts, val, k8sNamespaceNameAttributeValue, resourceAttributeValue) +} + +// RecordOpenshiftClusterquotaLimitDataPoint adds a data point to openshift.clusterquota.limit metric. +func (mb *MetricsBuilder) RecordOpenshiftClusterquotaLimitDataPoint(ts pcommon.Timestamp, val int64, resourceAttributeValue string) { + mb.metricOpenshiftClusterquotaLimit.recordDataPoint(mb.startTime, ts, val, resourceAttributeValue) +} + +// RecordOpenshiftClusterquotaUsedDataPoint adds a data point to openshift.clusterquota.used metric. +func (mb *MetricsBuilder) RecordOpenshiftClusterquotaUsedDataPoint(ts pcommon.Timestamp, val int64, resourceAttributeValue string) { + mb.metricOpenshiftClusterquotaUsed.recordDataPoint(mb.startTime, ts, val, resourceAttributeValue) +} + +// Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted, +// and metrics builder should update its startTime and reset it's internal state accordingly. +func (mb *MetricsBuilder) Reset(options ...metricBuilderOption) { + mb.startTime = pcommon.NewTimestampFromTime(time.Now()) + for _, op := range options { + op(mb) + } +} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics_test.go b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics_test.go new file mode 100644 index 000000000000..88afbd526cc3 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/generated_metrics_test.go @@ -0,0 +1,189 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" + "go.opentelemetry.io/collector/receiver/receivertest" + "go.uber.org/zap" + "go.uber.org/zap/zaptest/observer" +) + +type testConfigCollection int + +const ( + testSetDefault testConfigCollection = iota + testSetAll + testSetNone +) + +func TestMetricsBuilder(t *testing.T) { + tests := []struct { + name string + configSet testConfigCollection + }{ + { + name: "default", + configSet: testSetDefault, + }, + { + name: "all_set", + configSet: testSetAll, + }, + { + name: "none_set", + configSet: testSetNone, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + start := pcommon.Timestamp(1_000_000_000) + ts := pcommon.Timestamp(1_000_001_000) + observedZapCore, observedLogs := observer.New(zap.WarnLevel) + settings := receivertest.NewNopCreateSettings() + settings.Logger = zap.New(observedZapCore) + mb := NewMetricsBuilder(loadMetricsBuilderConfig(t, test.name), settings, WithStartTime(start)) + + expectedWarnings := 0 + assert.Equal(t, expectedWarnings, observedLogs.Len()) + + defaultMetricsCount := 0 + allMetricsCount := 0 + + defaultMetricsCount++ + allMetricsCount++ + mb.RecordOpenshiftAppliedclusterquotaLimitDataPoint(ts, 1, "k8s.namespace.name-val", "resource-val") + + defaultMetricsCount++ + allMetricsCount++ + mb.RecordOpenshiftAppliedclusterquotaUsedDataPoint(ts, 1, "k8s.namespace.name-val", "resource-val") + + defaultMetricsCount++ + allMetricsCount++ + mb.RecordOpenshiftClusterquotaLimitDataPoint(ts, 1, "resource-val") + + defaultMetricsCount++ + allMetricsCount++ + mb.RecordOpenshiftClusterquotaUsedDataPoint(ts, 1, "resource-val") + + metrics := mb.Emit(WithOpencensusResourcetype("opencensus.resourcetype-val"), WithOpenshiftClusterquotaName("openshift.clusterquota.name-val"), WithOpenshiftClusterquotaUID("openshift.clusterquota.uid-val")) + + if test.configSet == testSetNone { + assert.Equal(t, 0, metrics.ResourceMetrics().Len()) + return + } + + assert.Equal(t, 1, metrics.ResourceMetrics().Len()) + rm := metrics.ResourceMetrics().At(0) + attrCount := 0 + enabledAttrCount := 0 + attrVal, ok := rm.Resource().Attributes().Get("opencensus.resourcetype") + attrCount++ + assert.Equal(t, mb.resourceAttributesConfig.OpencensusResourcetype.Enabled, ok) + if mb.resourceAttributesConfig.OpencensusResourcetype.Enabled { + enabledAttrCount++ + assert.EqualValues(t, "opencensus.resourcetype-val", attrVal.Str()) + } + attrVal, ok = rm.Resource().Attributes().Get("openshift.clusterquota.name") + attrCount++ + assert.Equal(t, mb.resourceAttributesConfig.OpenshiftClusterquotaName.Enabled, ok) + if mb.resourceAttributesConfig.OpenshiftClusterquotaName.Enabled { + enabledAttrCount++ + assert.EqualValues(t, "openshift.clusterquota.name-val", attrVal.Str()) + } + attrVal, ok = rm.Resource().Attributes().Get("openshift.clusterquota.uid") + attrCount++ + assert.Equal(t, mb.resourceAttributesConfig.OpenshiftClusterquotaUID.Enabled, ok) + if mb.resourceAttributesConfig.OpenshiftClusterquotaUID.Enabled { + enabledAttrCount++ + assert.EqualValues(t, "openshift.clusterquota.uid-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 3) + + assert.Equal(t, 1, rm.ScopeMetrics().Len()) + ms := rm.ScopeMetrics().At(0).Metrics() + if test.configSet == testSetDefault { + assert.Equal(t, defaultMetricsCount, ms.Len()) + } + if test.configSet == testSetAll { + assert.Equal(t, allMetricsCount, ms.Len()) + } + validatedMetrics := make(map[string]bool) + for i := 0; i < ms.Len(); i++ { + switch ms.At(i).Name() { + case "openshift.appliedclusterquota.limit": + assert.False(t, validatedMetrics["openshift.appliedclusterquota.limit"], "Found a duplicate in the metrics slice: openshift.appliedclusterquota.limit") + validatedMetrics["openshift.appliedclusterquota.limit"] = true + assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, "The upper limit for a particular resource in a specific namespace.", ms.At(i).Description()) + assert.Equal(t, "1", ms.At(i).Unit()) + dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, start, dp.StartTimestamp()) + assert.Equal(t, ts, dp.Timestamp()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("k8s.namespace.name") + assert.True(t, ok) + assert.EqualValues(t, "k8s.namespace.name-val", attrVal.Str()) + attrVal, ok = dp.Attributes().Get("resource") + assert.True(t, ok) + assert.EqualValues(t, "resource-val", attrVal.Str()) + case "openshift.appliedclusterquota.used": + assert.False(t, validatedMetrics["openshift.appliedclusterquota.used"], "Found a duplicate in the metrics slice: openshift.appliedclusterquota.used") + validatedMetrics["openshift.appliedclusterquota.used"] = true + assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, "The usage for a particular resource in a specific namespace.", ms.At(i).Description()) + assert.Equal(t, "1", ms.At(i).Unit()) + dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, start, dp.StartTimestamp()) + assert.Equal(t, ts, dp.Timestamp()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("k8s.namespace.name") + assert.True(t, ok) + assert.EqualValues(t, "k8s.namespace.name-val", attrVal.Str()) + attrVal, ok = dp.Attributes().Get("resource") + assert.True(t, ok) + assert.EqualValues(t, "resource-val", attrVal.Str()) + case "openshift.clusterquota.limit": + assert.False(t, validatedMetrics["openshift.clusterquota.limit"], "Found a duplicate in the metrics slice: openshift.clusterquota.limit") + validatedMetrics["openshift.clusterquota.limit"] = true + assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, "The configured upper limit for a particular resource.", ms.At(i).Description()) + assert.Equal(t, "1", ms.At(i).Unit()) + dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, start, dp.StartTimestamp()) + assert.Equal(t, ts, dp.Timestamp()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("resource") + assert.True(t, ok) + assert.EqualValues(t, "resource-val", attrVal.Str()) + case "openshift.clusterquota.used": + assert.False(t, validatedMetrics["openshift.clusterquota.used"], "Found a duplicate in the metrics slice: openshift.clusterquota.used") + validatedMetrics["openshift.clusterquota.used"] = true + assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, "The usage for a particular resource with a configured limit.", ms.At(i).Description()) + assert.Equal(t, "1", ms.At(i).Unit()) + dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, start, dp.StartTimestamp()) + assert.Equal(t, ts, dp.Timestamp()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("resource") + assert.True(t, ok) + assert.EqualValues(t, "resource-val", attrVal.Str()) + } + } + }) + } +} diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/testdata/config.yaml b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/testdata/config.yaml new file mode 100644 index 000000000000..385232b2f6a2 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/internal/metadata/testdata/config.yaml @@ -0,0 +1,35 @@ +default: +all_set: + metrics: + openshift.appliedclusterquota.limit: + enabled: true + openshift.appliedclusterquota.used: + enabled: true + openshift.clusterquota.limit: + enabled: true + openshift.clusterquota.used: + enabled: true + resource_attributes: + opencensus.resourcetype: + enabled: true + openshift.clusterquota.name: + enabled: true + openshift.clusterquota.uid: + enabled: true +none_set: + metrics: + openshift.appliedclusterquota.limit: + enabled: false + openshift.appliedclusterquota.used: + enabled: false + openshift.clusterquota.limit: + enabled: false + openshift.clusterquota.used: + enabled: false + resource_attributes: + opencensus.resourcetype: + enabled: false + openshift.clusterquota.name: + enabled: false + openshift.clusterquota.uid: + enabled: false diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/metadata.yaml b/receiver/k8sclusterreceiver/internal/clusterresourcequota/metadata.yaml new file mode 100644 index 000000000000..10cd59e40044 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/metadata.yaml @@ -0,0 +1,67 @@ +type: k8s/clusterresourcequota + +sem_conv_version: 1.18.0 + +parent: k8s_cluster + +resource_attributes: + openshift.clusterquota.uid: + description: The k8s ClusterResourceQuota uid. + type: string + enabled: true + + openshift.clusterquota.name: + description: The k8s ClusterResourceQuota name. + type: string + enabled: true + + opencensus.resourcetype: + description: The OpenCensus resource type. + type: string + enabled: true + +attributes: + k8s.namespace.name: + description: The k8s namespace name. + type: string + enabled: true + resource: + description: The name of the resource on which the cluster quota is applied + type: string + enabled: true + +metrics: + openshift.clusterquota.limit: + enabled: true + description: The configured upper limit for a particular resource. + unit: "1" + gauge: + value_type: int + attributes: + - resource + openshift.clusterquota.used: + enabled: true + description: The usage for a particular resource with a configured limit. + unit: "1" + gauge: + value_type: int + attributes: + - resource + openshift.appliedclusterquota.limit: + enabled: true + description: The upper limit for a particular resource in a specific namespace. + unit: "1" + gauge: + value_type: int + attributes: + - k8s.namespace.name + - resource + openshift.appliedclusterquota.used: + enabled: true + description: The usage for a particular resource in a specific namespace. + unit: "1" + gauge: + value_type: int + attributes: + - k8s.namespace.name + - resource diff --git a/receiver/k8sclusterreceiver/internal/clusterresourcequota/testdata/expected.yaml b/receiver/k8sclusterreceiver/internal/clusterresourcequota/testdata/expected.yaml new file mode 100644 index 000000000000..1fdcd0580e33 --- /dev/null +++ b/receiver/k8sclusterreceiver/internal/clusterresourcequota/testdata/expected.yaml @@ -0,0 +1,81 @@ +resourceMetrics: + - resource: + attributes: + - key: openshift.clusterquota.name + value: + stringValue: test-clusterquota-1 + - key: openshift.clusterquota.uid + value: + stringValue: test-clusterquota-1-uid + - key: opencensus.resourcetype + value: + stringValue: k8s + schemaUrl: https://opentelemetry.io/schemas/1.18.0 + scopeMetrics: + - metrics: + - description: The configured upper limit for a particular resource. + gauge: + dataPoints: + - asInt: "10000" + attributes: + - key: resource + value: + stringValue: requests.cpu + name: openshift.clusterquota.limit + unit: "1" + - description: The usage for a particular resource with a configured limit. + gauge: + dataPoints: + - asInt: "6000" + attributes: + - key: resource + value: + stringValue: requests.cpu + name: openshift.clusterquota.used + unit: "1" + - description: The upper limit for a particular resource in a specific namespace. + gauge: + dataPoints: + - asInt: "6000" + attributes: + - key: resource + value: + stringValue: requests.cpu + - key: k8s.namespace.name + value: + stringValue: "ns1" + - asInt: "4000" + attributes: + - key: resource + value: + stringValue: requests.cpu + - key: k8s.namespace.name + value: + stringValue: "ns2" + name: openshift.appliedclusterquota.limit + unit: "1" + - description: The usage for a particular resource in a specific namespace. + gauge: + dataPoints: + - asInt: "1000" + attributes: + - key: resource + value: + stringValue: requests.cpu + - key: k8s.namespace.name + value: + stringValue: "ns1" + - asInt: "5000" + attributes: + - key: resource + value: + stringValue: requests.cpu + - key: + k8s.namespace.name + value: + stringValue: "ns2" + name: openshift.appliedclusterquota.used + unit: "1" + scope: + name: otelcol/k8sclusterreceiver + version: latest \ No newline at end of file diff --git a/receiver/k8sclusterreceiver/internal/collection/collector.go b/receiver/k8sclusterreceiver/internal/collection/collector.go index 008d25bfb73b..c15455d15b89 100644 --- a/receiver/k8sclusterreceiver/internal/collection/collector.go +++ b/receiver/k8sclusterreceiver/internal/collection/collector.go @@ -132,7 +132,7 @@ func (dc *DataCollector) SyncMetrics(obj interface{}) { case *autoscalingv2beta2.HorizontalPodAutoscaler: md = hpa.GetMetricsBeta(dc.settings, o) case *quotav1.ClusterResourceQuota: - md = ocsToMetrics(clusterresourcequota.GetMetrics(o)) + md = clusterresourcequota.GetMetrics(dc.settings, o) default: return } diff --git a/receiver/k8sclusterreceiver/internal/testutils/objects.go b/receiver/k8sclusterreceiver/internal/testutils/objects.go index 17c33651a48f..f8f67d8a0eda 100644 --- a/receiver/k8sclusterreceiver/internal/testutils/objects.go +++ b/receiver/k8sclusterreceiver/internal/testutils/objects.go @@ -4,6 +4,7 @@ package testutils // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/testutils" import ( + quotav1 "github.com/openshift/api/quota/v1" appsv1 "k8s.io/api/apps/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" @@ -77,6 +78,50 @@ func NewJob(id string) *batchv1.Job { } } +func NewClusterResourceQuota(id string) *quotav1.ClusterResourceQuota { + return "av1.ClusterResourceQuota{ + ObjectMeta: v1.ObjectMeta{ + Name: "test-clusterquota-" + id, + Namespace: "test-namespace", + 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), + }, + }, + }, + }, + }, + } +} + func NewDaemonset(id string) *appsv1.DaemonSet { return &appsv1.DaemonSet{ ObjectMeta: v1.ObjectMeta{