replicasets.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package replicaset // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/replicaset"
  4. import (
  5. "go.opentelemetry.io/collector/pdata/pcommon"
  6. appsv1 "k8s.io/api/apps/v1"
  7. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
  10. )
  11. // Transform transforms the replica set to remove the fields that we don't use to reduce RAM utilization.
  12. // IMPORTANT: Make sure to update this function before using new replicaset fields.
  13. func Transform(rs *appsv1.ReplicaSet) *appsv1.ReplicaSet {
  14. return &appsv1.ReplicaSet{
  15. ObjectMeta: metadata.TransformObjectMeta(rs.ObjectMeta),
  16. Spec: appsv1.ReplicaSetSpec{
  17. Replicas: rs.Spec.Replicas,
  18. },
  19. Status: appsv1.ReplicaSetStatus{
  20. AvailableReplicas: rs.Status.AvailableReplicas,
  21. },
  22. }
  23. }
  24. func RecordMetrics(mb *metadata.MetricsBuilder, rs *appsv1.ReplicaSet, ts pcommon.Timestamp) {
  25. if rs.Spec.Replicas != nil {
  26. mb.RecordK8sReplicasetDesiredDataPoint(ts, int64(*rs.Spec.Replicas))
  27. mb.RecordK8sReplicasetAvailableDataPoint(ts, int64(rs.Status.AvailableReplicas))
  28. }
  29. rb := mb.NewResourceBuilder()
  30. rb.SetK8sNamespaceName(rs.Namespace)
  31. rb.SetK8sReplicasetName(rs.Name)
  32. rb.SetK8sReplicasetUID(string(rs.UID))
  33. mb.EmitForResource(metadata.WithResource(rb.Emit()))
  34. }
  35. func GetMetadata(rs *appsv1.ReplicaSet) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
  36. return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
  37. experimentalmetricmetadata.ResourceID(rs.UID): metadata.GetGenericMetadata(&rs.ObjectMeta, constants.K8sKindReplicaSet),
  38. }
  39. }