| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package metrics
- import (
- "github.com/opencost/opencost/pkg/clustercache"
- "github.com/opencost/opencost/pkg/prom"
- "github.com/prometheus/client_golang/prometheus"
- dto "github.com/prometheus/client_model/go"
- )
- //--------------------------------------------------------------------------
- // KubecostNamespaceCollector
- //--------------------------------------------------------------------------
- // KubecostNamespaceCollector is a prometheus collector that generates namespace sourced metrics
- type KubecostNamespaceCollector struct {
- KubeClusterCache clustercache.ClusterCache
- metricsConfig MetricsConfig
- }
- // Describe sends the super-set of all possible descriptors of metrics
- // collected by this Collector.
- func (nsac KubecostNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
- disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_namespace_annotations"]; disabled {
- return
- }
- ch <- prometheus.NewDesc("kube_namespace_annotations", "namespace annotations", []string{}, nil)
- }
- // Collect is called by the Prometheus registry when collecting metrics.
- func (nsac KubecostNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
- disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_namespace_annotations"]; disabled {
- return
- }
- namespaces := nsac.KubeClusterCache.GetAllNamespaces()
- for _, namespace := range namespaces {
- nsName := namespace.GetName()
- labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
- if len(labels) > 0 {
- m := newNamespaceAnnotationsMetric("kube_namespace_annotations", nsName, labels, values)
- ch <- m
- }
- }
- }
- //--------------------------------------------------------------------------
- // NamespaceAnnotationsMetric
- //--------------------------------------------------------------------------
- // NamespaceAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
- type NamespaceAnnotationsMetric struct {
- fqName string
- help string
- namespace string
- labelNames []string
- labelValues []string
- }
- // Creates a new NamespaceAnnotationsMetric, implementation of prometheus.Metric
- func newNamespaceAnnotationsMetric(fqname, namespace string, labelNames []string, labelValues []string) NamespaceAnnotationsMetric {
- return NamespaceAnnotationsMetric{
- fqName: fqname,
- help: "kube_namespace_annotations Namespace Annotations",
- namespace: namespace,
- 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 NamespaceAnnotationsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": nam.namespace,
- }
- return prometheus.NewDesc(nam.fqName, nam.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data
- // transmission object.
- func (nam NamespaceAnnotationsMetric) 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("namespace"),
- Value: &nam.namespace,
- })
- m.Label = labels
- return nil
- }
- //--------------------------------------------------------------------------
- // KubeNamespaceCollector
- //--------------------------------------------------------------------------
- // KubeNamespaceCollector is a prometheus collector that generates namespace sourced metrics
- type KubeNamespaceCollector struct {
- KubeClusterCache clustercache.ClusterCache
- metricsConfig MetricsConfig
- }
- // Describe sends the super-set of all possible descriptors of metrics
- // collected by this Collector.
- func (nsac KubeNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
- disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_namespace_labels"]; disabled {
- return
- }
- ch <- prometheus.NewDesc("kube_namespace_labels", "namespace labels", []string{}, nil)
- }
- // Collect is called by the Prometheus registry when collecting metrics.
- func (nsac KubeNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
- disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
- if _, disabled := disabledMetrics["kube_namespace_labels"]; disabled {
- return
- }
- namespaces := nsac.KubeClusterCache.GetAllNamespaces()
- for _, namespace := range namespaces {
- nsName := namespace.GetName()
- labels, values := prom.KubeLabelsToLabels(namespace.Labels)
- if len(labels) > 0 {
- m := newNamespaceAnnotationsMetric("kube_namespace_labels", nsName, labels, values)
- ch <- m
- }
- }
- }
- //--------------------------------------------------------------------------
- // NamespaceAnnotationsMetric
- //--------------------------------------------------------------------------
- // NamespaceAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
- type KubeNamespaceLabelsMetric struct {
- fqName string
- help string
- namespace string
- labelNames []string
- labelValues []string
- }
- // Creates a new KubeNamespaceLabelsMetric, implementation of prometheus.Metric
- func newKubeNamespaceLabelsMetric(fqname, namespace string, labelNames []string, labelValues []string) KubeNamespaceLabelsMetric {
- return KubeNamespaceLabelsMetric{
- namespace: namespace,
- fqName: fqname,
- labelNames: labelNames,
- labelValues: labelValues,
- help: "kube_namespace_labels Namespace Labels",
- }
- }
- // Desc returns the descriptor for the Metric. This method idempotently
- // returns the same descriptor throughout the lifetime of the Metric.
- func (nam KubeNamespaceLabelsMetric) Desc() *prometheus.Desc {
- l := prometheus.Labels{
- "namespace": nam.namespace,
- }
- return prometheus.NewDesc(nam.fqName, nam.help, []string{}, l)
- }
- // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
- func (nam KubeNamespaceLabelsMetric) 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("namespace"),
- Value: &nam.namespace,
- })
- m.Label = labels
- return nil
- }
|