| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074 |
- package metrics
- import (
- "fmt"
- "github.com/opencost/opencost/pkg/clustercache"
- "github.com/opencost/opencost/pkg/log"
- "github.com/opencost/opencost/pkg/prom"
- "github.com/prometheus/client_golang/prometheus"
- dto "github.com/prometheus/client_model/go"
- v1 "k8s.io/api/core/v1"
- )
- //--------------------------------------------------------------------------
- // KubecostPodCollector
- //--------------------------------------------------------------------------
- // KubecostPodCollector is a prometheus collector that emits pod metrics
- type KubecostPodCollector struct {
- KubeClusterCache clustercache.ClusterCache
- metricsConfig MetricsConfig
- }
- // Describe sends the super-set of all possible descriptors of metrics
- // collected by this Collector.
- func (kpmc KubecostPodCollector) Describe(ch chan<- *prometheus.Desc) {
- disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
- return
- }
- ch <- prometheus.NewDesc("kube_pod_annotations", "All annotations for each pod prefix with annotation_", []string{}, nil)
- }
- // Collect is called by the Prometheus registry when collecting metrics.
- func (kpmc KubecostPodCollector) Collect(ch chan<- prometheus.Metric) {
- disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
- return
- }
- pods := kpmc.KubeClusterCache.GetAllPods()
- for _, pod := range pods {
- podName := pod.GetName()
- podNS := pod.GetNamespace()
- // Pod Annotations
- labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
- if len(labels) > 0 {
- ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
- }
- }
- }
- //--------------------------------------------------------------------------
- // KubePodCollector
- //--------------------------------------------------------------------------
- // KubePodMetricCollector is a prometheus collector that emits pod metrics
- type KubePodCollector struct {
- KubeClusterCache clustercache.ClusterCache
- metricsConfig MetricsConfig
- }
- // Describe sends the super-set of all possible descriptors of metrics
- // collected by this Collector.
- func (kpmc KubePodCollector) Describe(ch chan<- *prometheus.Desc) {
- disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_status_running"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_status_running", "Describes whether the container is currently in running state", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_status_terminated_reason"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_status_terminated_reason", "Describes the reason the container is currently in terminated state.", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_status_restarts_total"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_status_restarts_total", "The number of container restarts per container.", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_requests"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_resource_requests", "The number of requested resource by a container", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_resource_limits", "The number of requested limit resource by a container.", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_resource_limits_cpu_cores", "The number of requested limit cpu core resource by a container.", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_container_resource_limits_memory_bytes", "The number of requested limit memory resource by a container.", []string{}, nil)
- }
- if _, disabled := disabledMetrics["kube_pod_status_phase"]; !disabled {
- ch <- prometheus.NewDesc("kube_pod_status_phase", "The pods current phase.", []string{}, nil)
- }
- }
- // Collect is called by the Prometheus registry when collecting metrics.
- func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
- pods := kpmc.KubeClusterCache.GetAllPods()
- disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
- for _, pod := range pods {
- podName := pod.GetName()
- podNS := pod.GetNamespace()
- podUID := string(pod.GetUID())
- node := pod.Spec.NodeName
- phase := pod.Status.Phase
- // Pod Status Phase
- if _, disabled := disabledMetrics["kube_pod_status_phase"]; !disabled {
- if phase != "" {
- phases := []struct {
- v bool
- n string
- }{
- {phase == v1.PodPending, string(v1.PodPending)},
- {phase == v1.PodSucceeded, string(v1.PodSucceeded)},
- {phase == v1.PodFailed, string(v1.PodFailed)},
- {phase == v1.PodUnknown, string(v1.PodUnknown)},
- {phase == v1.PodRunning, string(v1.PodRunning)},
- }
- for _, p := range phases {
- ch <- newKubePodStatusPhaseMetric("kube_pod_status_phase", podNS, podName, podUID, p.n, boolFloat64(p.v))
- }
- }
- }
- // Pod Labels
- if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
- labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
- ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
- }
- // Owner References
- if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
- for _, owner := range pod.OwnerReferences {
- ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
- }
- }
- // Container Status
- for _, status := range pod.Status.ContainerStatuses {
- if _, disabled := disabledMetrics["kube_pod_container_status_restarts_total"]; !disabled {
- ch <- newKubePodContainerStatusRestartsTotalMetric("kube_pod_container_status_restarts_total", podNS, podName, podUID, status.Name, float64(status.RestartCount))
- }
- if status.State.Running != nil {
- if _, disabled := disabledMetrics["kube_pod_container_status_running"]; !disabled {
- ch <- newKubePodContainerStatusRunningMetric("kube_pod_container_status_running", podNS, podName, podUID, status.Name)
- }
- }
- if status.State.Terminated != nil {
- if _, disabled := disabledMetrics["kube_pod_container_status_terminated_reason"]; !disabled {
- ch <- newKubePodContainerStatusTerminatedReasonMetric(
- "kube_pod_container_status_terminated_reason",
- podNS,
- podName,
- podUID,
- status.Name,
- status.State.Terminated.Reason)
- }
- }
- }
- for _, container := range pod.Spec.Containers {
- // Requests
- if _, disabled := disabledMetrics["kube_pod_container_resource_requests"]; !disabled {
- for resourceName, quantity := range container.Resources.Requests {
- resource, unit, value := toResourceUnitValue(resourceName, quantity)
- // failed to parse the resource type
- if resource == "" {
- log.DedupedWarningf(5, "Failed to parse resource units and quantity for resource: %s", resourceName)
- continue
- }
- ch <- newKubePodContainerResourceRequestsMetric(
- "kube_pod_container_resource_requests",
- podNS,
- podName,
- podUID,
- container.Name,
- node,
- resource,
- unit,
- value)
- }
- }
- // Limits
- for resourceName, quantity := range container.Resources.Limits {
- resource, unit, value := toResourceUnitValue(resourceName, quantity)
- // failed to parse the resource type
- if resource == "" {
- log.DedupedWarningf(5, "Failed to parse resource units and quantity for resource: %s", resourceName)
- continue
- }
- // KSM v1 Emission
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !disabled {
- if resource == "cpu" {
- ch <- newKubePodContainerResourceLimitsCPUCoresMetric(
- "kube_pod_container_resource_limits_cpu_cores",
- podNS,
- podName,
- podUID,
- container.Name,
- node,
- value)
- }
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !disabled {
- if resource == "memory" {
- ch <- newKubePodContainerResourceLimitsMemoryBytesMetric(
- "kube_pod_container_resource_limits_memory_bytes",
- podNS,
- podName,
- podUID,
- container.Name,
- node,
- value)
- }
- }
- if _, disabled := disabledMetrics["kube_pod_container_resource_limits"]; !disabled {
- ch <- newKubePodContainerResourceLimitsMetric(
- "kube_pod_container_resource_limits",
- podNS,
- podName,
- podUID,
- container.Name,
- node,
- resource,
- unit,
- value)
- }
- }
- }
- }
- }
- //--------------------------------------------------------------------------
- // PodAnnotationsMetric
- //--------------------------------------------------------------------------
- // PodAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
- type PodAnnotationsMetric struct {
- fqName string
- help string
- namespace string
- pod string
- labelNames []string
- labelValues []string
- }
- // Creates a new PodAnnotationsMetric, implementation of prometheus.Metric
- func newPodAnnotationMetric(fqname, namespace, pod string, labelNames, labelValues []string) PodAnnotationsMetric {
- return PodAnnotationsMetric{
- fqName: fqname,
- help: "kube_pod_annotations Pod Annotations",
- namespace: namespace,
- pod: pod,
- labelNames: labelNames,
- labelValues: labelValues,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (pam PodAnnotationsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": pam.namespace,
- "pod": pam.pod,
- }
- return prometheus.NewDesc(pam.fqName, pam.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (pam PodAnnotationsMetric) Write(m *dto.Metric) error {
- h := float64(1)
- m.Gauge = &dto.Gauge{
- Value: &h,
- }
- var labels []*dto.LabelPair
- for i := range pam.labelNames {
- labels = append(labels, &dto.LabelPair{
- Name: &pam.labelNames[i],
- Value: &pam.labelValues[i],
- })
- }
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &pam.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &pam.pod,
- })
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodLabelsMetric
- //--------------------------------------------------------------------------
- // KubePodLabelsMetric is a prometheus.Metric used to encode
- // a duplicate of the deprecated kube-state-metrics metric
- // kube_pod_labels
- type KubePodLabelsMetric struct {
- fqName string
- help string
- pod string
- namespace string
- uid string
- labelNames []string
- labelValues []string
- }
- // Creates a new KubePodLabelsMetric, implementation of prometheus.Metric
- func newKubePodLabelsMetric(fqname, namespace, pod, uid string, labelNames []string, labelValues []string) KubePodLabelsMetric {
- return KubePodLabelsMetric{
- fqName: fqname,
- help: "kube_pod_labels all labels for each pod prefixed with label_",
- pod: pod,
- namespace: namespace,
- uid: uid,
- labelNames: labelNames,
- labelValues: labelValues,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (nam KubePodLabelsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": nam.namespace,
- "pod": nam.pod,
- "uid": nam.uid,
- }
- return prometheus.NewDesc(nam.fqName, nam.help, nam.labelNames, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (nam KubePodLabelsMetric) Write(m *dto.Metric) error {
- h := float64(1)
- m.Gauge = &dto.Gauge{
- Value: &h,
- }
- var labels []*dto.LabelPair
- for i := range nam.labelNames {
- labels = append(labels, &dto.LabelPair{
- Name: &nam.labelNames[i],
- Value: &nam.labelValues[i],
- })
- }
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &nam.pod,
- },
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &nam.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("uid"),
- Value: &nam.uid,
- },
- )
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerStatusRestartsTotalMetric
- //--------------------------------------------------------------------------
- // KubePodContainerStatusRestartsTotalMetric is a prometheus.Metric emitting container restarts metrics.
- type KubePodContainerStatusRestartsTotalMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- value float64
- }
- // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
- func newKubePodContainerStatusRestartsTotalMetric(fqname, namespace, pod, uid, container string, value float64) KubePodContainerStatusRestartsTotalMetric {
- return KubePodContainerStatusRestartsTotalMetric{
- fqName: fqname,
- help: "kube_pod_container_status_restarts_total total container restarts",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcs KubePodContainerStatusRestartsTotalMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcs.namespace,
- "pod": kpcs.pod,
- "uid": kpcs.uid,
- "container": kpcs.container,
- }
- return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
- func (kpcs KubePodContainerStatusRestartsTotalMetric) Write(m *dto.Metric) error {
- m.Counter = &dto.Counter{
- Value: &kpcs.value,
- }
- var labels []*dto.LabelPair
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &kpcs.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &kpcs.pod,
- },
- &dto.LabelPair{
- Name: toStringPtr("container"),
- Value: &kpcs.container,
- },
- &dto.LabelPair{
- Name: toStringPtr("uid"),
- Value: &kpcs.uid,
- },
- )
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerStatusTerminatedReasonMetric
- //--------------------------------------------------------------------------
- // KubePodContainerStatusTerminatedReasonMetric is a prometheus.Metric emitting container termination reasons.
- type KubePodContainerStatusTerminatedReasonMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- reason string
- }
- // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
- func newKubePodContainerStatusTerminatedReasonMetric(fqname, namespace, pod, uid, container, reason string) KubePodContainerStatusTerminatedReasonMetric {
- return KubePodContainerStatusTerminatedReasonMetric{
- fqName: fqname,
- help: "kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- reason: reason,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcs KubePodContainerStatusTerminatedReasonMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcs.namespace,
- "pod": kpcs.pod,
- "uid": kpcs.uid,
- "container": kpcs.container,
- "reason": kpcs.reason,
- }
- return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
- func (kpcs KubePodContainerStatusTerminatedReasonMetric) Write(m *dto.Metric) error {
- h := float64(1)
- m.Gauge = &dto.Gauge{
- Value: &h,
- }
- var labels []*dto.LabelPair
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &kpcs.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &kpcs.pod,
- },
- &dto.LabelPair{
- Name: toStringPtr("container"),
- Value: &kpcs.container,
- },
- &dto.LabelPair{
- Name: toStringPtr("uid"),
- Value: &kpcs.uid,
- },
- &dto.LabelPair{
- Name: toStringPtr("reason"),
- Value: &kpcs.reason,
- },
- )
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodStatusPhaseMetric
- //--------------------------------------------------------------------------
- // KubePodStatusPhaseMetric is a prometheus.Metric emitting all phases for a pod
- type KubePodStatusPhaseMetric struct {
- fqName string
- help string
- pod string
- namespace string
- uid string
- phase string
- value float64
- }
- // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
- func newKubePodStatusPhaseMetric(fqname, namespace, pod, uid, phase string, value float64) KubePodStatusPhaseMetric {
- return KubePodStatusPhaseMetric{
- fqName: fqname,
- help: "kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.",
- pod: pod,
- namespace: namespace,
- uid: uid,
- phase: phase,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcs KubePodStatusPhaseMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcs.namespace,
- "pod": kpcs.pod,
- "uid": kpcs.uid,
- "phase": kpcs.phase,
- }
- return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
- func (kpcs KubePodStatusPhaseMetric) Write(m *dto.Metric) error {
- m.Gauge = &dto.Gauge{
- Value: &kpcs.value,
- }
- var labels []*dto.LabelPair
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &kpcs.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &kpcs.pod,
- },
- &dto.LabelPair{
- Name: toStringPtr("uid"),
- Value: &kpcs.uid,
- },
- &dto.LabelPair{
- Name: toStringPtr("phase"),
- Value: &kpcs.phase,
- },
- )
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerStatusRunningMetric
- //--------------------------------------------------------------------------
- // KubePodLabelsMetric is a prometheus.Metric used to encode
- // a duplicate of the deprecated kube-state-metrics metric
- // kube_pod_labels
- type KubePodContainerStatusRunningMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- }
- // Creates a new KubePodContainerStatusRunningMetric, implementation of prometheus.Metric
- func newKubePodContainerStatusRunningMetric(fqname, namespace, pod, uid, container string) KubePodContainerStatusRunningMetric {
- return KubePodContainerStatusRunningMetric{
- fqName: fqname,
- help: "kube_pod_container_status_running pods container status",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcs KubePodContainerStatusRunningMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcs.namespace,
- "pod": kpcs.pod,
- "uid": kpcs.uid,
- "container": kpcs.container,
- }
- return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpcs KubePodContainerStatusRunningMetric) Write(m *dto.Metric) error {
- h := float64(1)
- m.Gauge = &dto.Gauge{
- Value: &h,
- }
- var labels []*dto.LabelPair
- labels = append(labels,
- &dto.LabelPair{
- Name: toStringPtr("namespace"),
- Value: &kpcs.namespace,
- },
- &dto.LabelPair{
- Name: toStringPtr("pod"),
- Value: &kpcs.pod,
- },
- &dto.LabelPair{
- Name: toStringPtr("container"),
- Value: &kpcs.container,
- },
- &dto.LabelPair{
- Name: toStringPtr("uid"),
- Value: &kpcs.uid,
- },
- )
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerResourceRequestMetric
- //--------------------------------------------------------------------------
- // KubePodContainerResourceRequestsMetric is a prometheus.Metric
- type KubePodContainerResourceRequestsMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- resource string
- unit string
- node string
- value float64
- }
- // Creates a new newKubePodContainerResourceRequestsMetric, implementation of prometheus.Metric
- func newKubePodContainerResourceRequestsMetric(fqname, namespace, pod, uid, container, node, resource, unit string, value float64) KubePodContainerResourceRequestsMetric {
- return KubePodContainerResourceRequestsMetric{
- fqName: fqname,
- help: "kube_pod_container_resource_requests pods container resource requests",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- node: node,
- resource: resource,
- unit: unit,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcrr KubePodContainerResourceRequestsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcrr.namespace,
- "pod": kpcrr.pod,
- "uid": kpcrr.uid,
- "container": kpcrr.container,
- "node": kpcrr.node,
- "resource": kpcrr.resource,
- "unit": kpcrr.unit,
- }
- return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpcrr KubePodContainerResourceRequestsMetric) Write(m *dto.Metric) error {
- m.Gauge = &dto.Gauge{
- Value: &kpcrr.value,
- }
- m.Label = []*dto.LabelPair{
- {
- Name: toStringPtr("namespace"),
- Value: &kpcrr.namespace,
- },
- {
- Name: toStringPtr("pod"),
- Value: &kpcrr.pod,
- },
- {
- Name: toStringPtr("container"),
- Value: &kpcrr.container,
- },
- {
- Name: toStringPtr("uid"),
- Value: &kpcrr.uid,
- },
- {
- Name: toStringPtr("node"),
- Value: &kpcrr.node,
- },
- {
- Name: toStringPtr("resource"),
- Value: &kpcrr.resource,
- },
- {
- Name: toStringPtr("unit"),
- Value: &kpcrr.unit,
- },
- }
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsMetric
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsMetric is a prometheus.Metric
- type KubePodContainerResourceLimitsMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- resource string
- unit string
- node string
- value float64
- }
- // Creates a new KubePodContainerResourceLimitsMetric, implementation of prometheus.Metric
- func newKubePodContainerResourceLimitsMetric(fqname, namespace, pod, uid, container, node, resource, unit string, value float64) KubePodContainerResourceLimitsMetric {
- return KubePodContainerResourceLimitsMetric{
- fqName: fqname,
- help: "kube_pod_container_resource_limits pods container resource limits",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- node: node,
- resource: resource,
- unit: unit,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcrr KubePodContainerResourceLimitsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcrr.namespace,
- "pod": kpcrr.pod,
- "uid": kpcrr.uid,
- "container": kpcrr.container,
- "node": kpcrr.node,
- "resource": kpcrr.resource,
- "unit": kpcrr.unit,
- }
- return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpcrr KubePodContainerResourceLimitsMetric) Write(m *dto.Metric) error {
- m.Gauge = &dto.Gauge{
- Value: &kpcrr.value,
- }
- m.Label = []*dto.LabelPair{
- {
- Name: toStringPtr("namespace"),
- Value: &kpcrr.namespace,
- },
- {
- Name: toStringPtr("pod"),
- Value: &kpcrr.pod,
- },
- {
- Name: toStringPtr("container"),
- Value: &kpcrr.container,
- },
- {
- Name: toStringPtr("uid"),
- Value: &kpcrr.uid,
- },
- {
- Name: toStringPtr("node"),
- Value: &kpcrr.node,
- },
- {
- Name: toStringPtr("resource"),
- Value: &kpcrr.resource,
- },
- {
- Name: toStringPtr("unit"),
- Value: &kpcrr.unit,
- },
- }
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsCPUCoresMetric (KSM v1)
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsCPUCoresMetric is a prometheus.Metric
- type KubePodContainerResourceLimitsCPUCoresMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- node string
- value float64
- }
- // Creates a new KubePodContainerResourceLimitsMetric, implementation of prometheus.Metric
- func newKubePodContainerResourceLimitsCPUCoresMetric(fqname, namespace, pod, uid, container, node string, value float64) KubePodContainerResourceLimitsCPUCoresMetric {
- return KubePodContainerResourceLimitsCPUCoresMetric{
- fqName: fqname,
- help: "kube_pod_container_resource_limits_cpu_cores pods container cpu cores resource limits",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- node: node,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcrr KubePodContainerResourceLimitsCPUCoresMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcrr.namespace,
- "pod": kpcrr.pod,
- "uid": kpcrr.uid,
- "container": kpcrr.container,
- "node": kpcrr.node,
- }
- return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpcrr KubePodContainerResourceLimitsCPUCoresMetric) Write(m *dto.Metric) error {
- m.Gauge = &dto.Gauge{
- Value: &kpcrr.value,
- }
- m.Label = []*dto.LabelPair{
- {
- Name: toStringPtr("namespace"),
- Value: &kpcrr.namespace,
- },
- {
- Name: toStringPtr("pod"),
- Value: &kpcrr.pod,
- },
- {
- Name: toStringPtr("container"),
- Value: &kpcrr.container,
- },
- {
- Name: toStringPtr("uid"),
- Value: &kpcrr.uid,
- },
- {
- Name: toStringPtr("node"),
- Value: &kpcrr.node,
- },
- }
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsMemoryBytesMetric (KSM v1)
- //--------------------------------------------------------------------------
- // KubePodContainerResourceLimitsMemoryBytesMetric is a prometheus.Metric
- type KubePodContainerResourceLimitsMemoryBytesMetric struct {
- fqName string
- help string
- pod string
- namespace string
- container string
- uid string
- node string
- value float64
- }
- // Creates a new KubePodContainerResourceLimitsMemoryBytesMetric, implementation of prometheus.Metric
- func newKubePodContainerResourceLimitsMemoryBytesMetric(fqname, namespace, pod, uid, container, node string, value float64) KubePodContainerResourceLimitsMemoryBytesMetric {
- return KubePodContainerResourceLimitsMemoryBytesMetric{
- fqName: fqname,
- help: "kube_pod_container_resource_limits_memory_bytes pods container memory bytes resource limits",
- pod: pod,
- namespace: namespace,
- uid: uid,
- container: container,
- node: node,
- value: value,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpcrr KubePodContainerResourceLimitsMemoryBytesMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpcrr.namespace,
- "pod": kpcrr.pod,
- "uid": kpcrr.uid,
- "container": kpcrr.container,
- "node": kpcrr.node,
- }
- return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpcrr KubePodContainerResourceLimitsMemoryBytesMetric) Write(m *dto.Metric) error {
- m.Gauge = &dto.Gauge{
- Value: &kpcrr.value,
- }
- m.Label = []*dto.LabelPair{
- {
- Name: toStringPtr("namespace"),
- Value: &kpcrr.namespace,
- },
- {
- Name: toStringPtr("pod"),
- Value: &kpcrr.pod,
- },
- {
- Name: toStringPtr("container"),
- Value: &kpcrr.container,
- },
- {
- Name: toStringPtr("uid"),
- Value: &kpcrr.uid,
- },
- {
- Name: toStringPtr("node"),
- Value: &kpcrr.node,
- },
- }
- return nil
- }
- //--------------------------------------------------------------------------
- // KubePodOwnerMetric
- //--------------------------------------------------------------------------
- // KubePodOwnerMetric is a prometheus.Metric
- type KubePodOwnerMetric struct {
- fqName string
- help string
- namespace string
- pod string
- ownerIsController bool
- ownerName string
- ownerKind string
- }
- // Creates a new KubePodOwnerMetric, implementation of prometheus.Metric
- func newKubePodOwnerMetric(fqname, namespace, pod, ownerName, ownerKind string, ownerIsController bool) KubePodOwnerMetric {
- return KubePodOwnerMetric{
- fqName: fqname,
- help: "kube_pod_owner Information about the Pod's owner",
- namespace: namespace,
- pod: pod,
- ownerName: ownerName,
- ownerKind: ownerKind,
- ownerIsController: ownerIsController,
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (kpo KubePodOwnerMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": kpo.namespace,
- "pod": kpo.pod,
- "owner_name": kpo.ownerName,
- "owner_kind": kpo.ownerKind,
- "owner_is_controller": fmt.Sprintf("%t", kpo.ownerIsController),
- }
- return prometheus.NewDesc(kpo.fqName, kpo.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (kpo KubePodOwnerMetric) Write(m *dto.Metric) error {
- v := float64(1.0)
- m.Gauge = &dto.Gauge{
- Value: &v,
- }
- m.Label = []*dto.LabelPair{
- {
- Name: toStringPtr("namespace"),
- Value: &kpo.namespace,
- },
- {
- Name: toStringPtr("pod"),
- Value: &kpo.pod,
- },
- {
- Name: toStringPtr("owner_name"),
- Value: &kpo.ownerName,
- },
- {
- Name: toStringPtr("owner_kind"),
- Value: &kpo.ownerKind,
- },
- {
- Name: toStringPtr("owner_is_controller"),
- Value: toStringPtr(fmt.Sprintf("%t", kpo.ownerIsController)),
- },
- }
- return nil
- }
|