Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwayama committed Dec 6, 2023
1 parent e1f92c4 commit 263c955
Show file tree
Hide file tree
Showing 17 changed files with 886 additions and 405 deletions.
30 changes: 30 additions & 0 deletions receiver/k8sclusterreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ Number of desired pods in this deployment
| ---- | ----------- | ---------- |
| {pod} | Gauge | Int |
### k8s.hierarchical_resource_quota.hard_limit
The upper limit for a particular resource in a specific namespace. Will only be sent if a quota is specified. CPU requests/limits will be sent as millicores
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {resource} | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| resource | the name of the resource on which the quota is applied | Any Str |
### k8s.hierarchical_resource_quota.used
The usage for a particular resource in a specific namespace. Will only be sent if a quota is specified. CPU requests/limits will be sent as millicores
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {resource} | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| resource | the name of the resource on which the quota is applied | Any Str |
### k8s.hpa.current_replicas
Current number of pod replicas managed by this autoscaler.
Expand Down Expand Up @@ -432,6 +460,8 @@ Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4
| k8s.daemonset.uid | The k8s daemonset uid. | Any Str | true |
| k8s.deployment.name | The name of the Deployment. | Any Str | true |
| k8s.deployment.uid | The UID of the Deployment. | Any Str | true |
| k8s.hierarchicalresourcequota.name | The k8s HierarchicalresourceQuota name. | Any Str | true |
| k8s.hierarchicalresourcequota.uid | The k8s HierarchicalResourceQuota uid. | Any Str | true |
| k8s.hpa.name | The k8s hpa name. | Any Str | true |
| k8s.hpa.uid | The k8s hpa uid. | Any Str | true |
| k8s.job.name | The k8s pod name. | Any Str | true |
Expand Down
5 changes: 5 additions & 0 deletions receiver/k8sclusterreceiver/internal/collection/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/clusterresourcequota"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/cronjob"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/demonset"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/deployment"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/gvk"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/hierarchicalresourcequota"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/hpa"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/jobs"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
Expand Down Expand Up @@ -104,6 +106,9 @@ func (dc *DataCollector) CollectMetricData(currentTime time.Time) pmetric.Metric
dc.metadataStore.ForEach(gvk.ClusterResourceQuota, func(o any) {
clusterresourcequota.RecordMetrics(dc.metricsBuilder, o.(*quotav1.ClusterResourceQuota), ts)
})
dc.metadataStore.ForEach(gvk.HierarchicalResourceQuota, func(o any) {
hierarchicalresourcequota.RecordMetrics(dc.metricsBuilder, o.(*unstructured.Unstructured), ts)
})

m := dc.metricsBuilder.Emit()
customRMs.MoveAndAppendTo(m.ResourceMetrics())
Expand Down
29 changes: 15 additions & 14 deletions receiver/k8sclusterreceiver/internal/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import "k8s.io/apimachinery/pkg/runtime/schema"

// Kubernetes group version kinds
var (
Pod = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}
Node = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Node"}
Namespace = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}
ReplicationController = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ReplicationController"}
ResourceQuota = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ResourceQuota"}
Service = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"}
DaemonSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "DaemonSet"}
Deployment = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
ReplicaSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "ReplicaSet"}
StatefulSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
Job = schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}
CronJob = schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "CronJob"}
HorizontalPodAutoscaler = schema.GroupVersionKind{Group: "autoscaling", Version: "v2", Kind: "HorizontalPodAutoscaler"}
ClusterResourceQuota = schema.GroupVersionKind{Group: "quota", Version: "v1", Kind: "ClusterResourceQuota"}
Pod = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}
Node = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Node"}
Namespace = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}
ReplicationController = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ReplicationController"}
ResourceQuota = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ResourceQuota"}
Service = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"}
DaemonSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "DaemonSet"}
Deployment = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
ReplicaSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "ReplicaSet"}
StatefulSet = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
Job = schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}
CronJob = schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "CronJob"}
HorizontalPodAutoscaler = schema.GroupVersionKind{Group: "autoscaling", Version: "v2", Kind: "HorizontalPodAutoscaler"}
ClusterResourceQuota = schema.GroupVersionKind{Group: "quota", Version: "v1", Kind: "ClusterResourceQuota"}
HierarchicalResourceQuota = schema.GroupVersionKind{Group: "hnc.x-k8s.io", Version: "v1alpha2", Kind: "HierarchicalResourceQuota"}
)
11 changes: 11 additions & 0 deletions receiver/k8sclusterreceiver/internal/gvr/gvr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import "k8s.io/apimachinery/pkg/runtime/schema"

// Kubernetes group version resources
var (
HierarchicalResourceQuota = schema.GroupVersionResource{Group: "hnc.x-k8s.io", Version: "v1alpha2", Resource: "hierarchicalresourcequotas"}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

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

func RecordMetrics(mb *metadata.MetricsBuilder, hrq *unstructured.Unstructured, ts pcommon.Timestamp) {
name, _, _ := unstructured.NestedString(hrq.Object, "metadata", "name")
uid, _, _ := unstructured.NestedString(hrq.Object, "metadata", "uid")
namespace, _, _ := unstructured.NestedString(hrq.Object, "metadata", "namespace")
statusHard, _, _ := unstructured.NestedMap(hrq.Object, "status", "hard")
for k, v := range statusHard {
q := resource.MustParse(v.(string))
val := q.Value()
if strings.HasSuffix(string(k), ".cpu") {
val = q.MilliValue()
}
mb.RecordK8sHierarchicalResourceQuotaHardLimitDataPoint(ts, val, string(k))
}
statusUsed, _, _ := unstructured.NestedMap(hrq.Object, "status", "used")
for k, v := range statusUsed {
q := resource.MustParse(v.(string))
val := q.Value()
if strings.HasSuffix(string(k), ".cpu") {
val = q.MilliValue()
}
mb.RecordK8sHierarchicalResourceQuotaHardLimitDataPoint(ts, val, string(k))
}

rb := mb.NewResourceBuilder()
rb.SetK8sHierarchicalresourcequotaUID(string(uid))
rb.SetK8sHierarchicalresourcequotaName(name)
rb.SetK8sNamespaceName(namespace)
mb.EmitForResource(metadata.WithResource(rb.Emit()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package hierarchicalresourcequota

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver/receivertest"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

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

func TestRequestQuotaMetrics(t *testing.T) {
rq := testutils.NewHierachilalResourceQuota("1")
ts := pcommon.Timestamp(time.Now().UnixNano())
mb := metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings())
RecordMetrics(mb, rq.(*unstructured.Unstructured), ts)
m := mb.Emit()

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(),
),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
resourceMetrics:
- resource:
attributes:
- key: k8s.namespace.name
value:
stringValue: test-namespace
- key: k8s.hierarchicalresourcequota.name
value:
stringValue: test-hierarchicalresourcequota-1
- key: k8s.hierarchicalresourcequota.uid
value:
stringValue: test-hierarchicalresourcequota-1-uid
schemaUrl: https://opentelemetry.io/schemas/1.18.0
scopeMetrics:
- metrics:
- description: The upper limit for a particular resource in a specific namespace. Will only be sent if a quota is specified. CPU requests/limits will be sent as millicores
gauge:
dataPoints:
- asInt: "2000"
attributes:
- key: resource
value:
stringValue: requests.cpu
name: k8s.hierarchical_resource_quota.hard_limit
unit: "{resource}"
- description: The usage for a particular resource in a specific namespace. Will only be sent if a quota is specified. CPU requests/limits will be sent as millicores
gauge:
dataPoints:
- asInt: "1000"
attributes:
- key: resource
value:
stringValue: requests.cpu
name: k8s.hierarchical_resource_quota.used
unit: "{resource}"
scope:
name: otelcol/k8sclusterreceiver
version: latest
Loading

0 comments on commit 263c955

Please sign in to comment.