-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
886 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
) |
44 changes: 44 additions & 0 deletions
44
receiver/k8sclusterreceiver/internal/hierarchicalresourcequota/hierarchicalresourcequotas.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
39 changes: 39 additions & 0 deletions
39
.../k8sclusterreceiver/internal/hierarchicalresourcequota/hierarchicalresourcequotas_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
), | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
receiver/k8sclusterreceiver/internal/hierarchicalresourcequota/testdata/expected.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.