github.com/cilium/cilium@v1.16.2/pkg/hubble/exporter/metrics.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package exporter
     5  
     6  import (
     7  	"github.com/prometheus/client_golang/prometheus"
     8  
     9  	"github.com/cilium/cilium/pkg/hubble/metrics"
    10  	"github.com/cilium/cilium/pkg/hubble/metrics/api"
    11  	"github.com/cilium/cilium/pkg/time"
    12  )
    13  
    14  var (
    15  	labelExporterStatus = "status"
    16  	exportersDesc       = prometheus.NewDesc(
    17  		prometheus.BuildFQName(api.DefaultPrometheusNamespace, "dynamic_exporter", "exporters_total"),
    18  		"Number of configured exporters",
    19  		[]string{labelExporterStatus},
    20  		nil,
    21  	)
    22  
    23  	labelExporterName       = "name"
    24  	individualExportersDesc = prometheus.NewDesc(
    25  		prometheus.BuildFQName(api.DefaultPrometheusNamespace, "dynamic_exporter", "up"),
    26  		"Status of individual exporters",
    27  		[]string{labelExporterName},
    28  		nil,
    29  	)
    30  
    31  	labelReconfigurationOperation   = "op"
    32  	DynamicExporterReconfigurations = prometheus.NewCounterVec(prometheus.CounterOpts{
    33  		Namespace: api.DefaultPrometheusNamespace,
    34  		Subsystem: "dynamic_exporter",
    35  		Name:      "reconfigurations_total",
    36  		Help:      "Number of dynamic exporters reconfigurations",
    37  	}, []string{labelReconfigurationOperation})
    38  
    39  	DynamicExporterConfigHash = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    40  		Namespace: api.DefaultPrometheusNamespace,
    41  		Subsystem: "dynamic_exporter",
    42  		Name:      "config_hash",
    43  		Help:      "Hash of last applied config",
    44  	}, []string{})
    45  
    46  	DynamicExporterConfigLastApplied = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    47  		Namespace: api.DefaultPrometheusNamespace,
    48  		Subsystem: "dynamic_exporter",
    49  		Name:      "config_last_applied",
    50  		Help:      "Timestamp of last applied config",
    51  	}, []string{})
    52  )
    53  
    54  func registerMetrics(exp *DynamicExporter) {
    55  	metrics.Register(&dynamicExporterGaugeCollector{exporter: exp})
    56  	metrics.Register(DynamicExporterReconfigurations)
    57  	metrics.Register(DynamicExporterConfigHash)
    58  	metrics.Register(DynamicExporterConfigLastApplied)
    59  }
    60  
    61  type dynamicExporterGaugeCollector struct {
    62  	prometheus.Collector
    63  	exporter *DynamicExporter
    64  }
    65  
    66  // Describe sends the super-set of all possible descriptors of metrics
    67  // collected by this Collector to the provided channel and returns once
    68  // the last descriptor has been sent. The sent descriptors fulfill the
    69  // consistency and uniqueness requirements described in the Desc
    70  // documentation.
    71  func (d *dynamicExporterGaugeCollector) Describe(ch chan<- *prometheus.Desc) {
    72  	ch <- exportersDesc
    73  	ch <- individualExportersDesc
    74  }
    75  
    76  // Collect is called by the Prometheus registry when collecting
    77  // metrics. The implementation sends each collected metric via the
    78  // provided channel and returns once the last metric has been sent. The
    79  // descriptor of each sent metric is one of those returned by Describe.
    80  // Returned metrics that share the same descriptor must differ in their
    81  // variable label values.
    82  func (d *dynamicExporterGaugeCollector) Collect(ch chan<- prometheus.Metric) {
    83  	var activeExporters, inactiveExporters float64
    84  
    85  	for name, me := range d.exporter.managedExporters {
    86  		var value float64
    87  		if me.config.End == nil || me.config.End.After(time.Now()) {
    88  			value = 1
    89  			activeExporters++
    90  		} else {
    91  			inactiveExporters++
    92  		}
    93  		ch <- prometheus.MustNewConstMetric(
    94  			individualExportersDesc, prometheus.GaugeValue, value, name,
    95  		)
    96  	}
    97  
    98  	ch <- prometheus.MustNewConstMetric(
    99  		exportersDesc, prometheus.GaugeValue, activeExporters, "active",
   100  	)
   101  	ch <- prometheus.MustNewConstMetric(
   102  		exportersDesc, prometheus.GaugeValue, inactiveExporters, "inactive",
   103  	)
   104  }