github.com/argoproj/argo-cd/v2@v2.10.9/controller/metrics/clustercollector.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/cache"
     9  
    10  	"github.com/prometheus/client_golang/prometheus"
    11  )
    12  
    13  const (
    14  	metricsCollectionInterval = 30 * time.Second
    15  )
    16  
    17  var (
    18  	descClusterDefaultLabels = []string{"server"}
    19  
    20  	descClusterInfo = prometheus.NewDesc(
    21  		"argocd_cluster_info",
    22  		"Information about cluster.",
    23  		append(descClusterDefaultLabels, "k8s_version"),
    24  		nil,
    25  	)
    26  	descClusterCacheResources = prometheus.NewDesc(
    27  		"argocd_cluster_api_resource_objects",
    28  		"Number of k8s resource objects in the cache.",
    29  		descClusterDefaultLabels,
    30  		nil,
    31  	)
    32  	descClusterAPIs = prometheus.NewDesc(
    33  		"argocd_cluster_api_resources",
    34  		"Number of monitored kubernetes API resources.",
    35  		descClusterDefaultLabels,
    36  		nil,
    37  	)
    38  	descClusterCacheAgeSeconds = prometheus.NewDesc(
    39  		"argocd_cluster_cache_age_seconds",
    40  		"Cluster cache age in seconds.",
    41  		descClusterDefaultLabels,
    42  		nil,
    43  	)
    44  	descClusterConnectionStatus = prometheus.NewDesc(
    45  		"argocd_cluster_connection_status",
    46  		"The k8s cluster current connection status.",
    47  		append(descClusterDefaultLabels, "k8s_version"),
    48  		nil,
    49  	)
    50  )
    51  
    52  type HasClustersInfo interface {
    53  	GetClustersInfo() []cache.ClusterInfo
    54  }
    55  
    56  type clusterCollector struct {
    57  	infoSource HasClustersInfo
    58  	info       []cache.ClusterInfo
    59  	lock       sync.Mutex
    60  }
    61  
    62  func (c *clusterCollector) Run(ctx context.Context) {
    63  	// FIXME: complains about SA1015
    64  	// nolint:staticcheck
    65  	tick := time.Tick(metricsCollectionInterval)
    66  	for {
    67  		select {
    68  		case <-ctx.Done():
    69  			break
    70  		case <-tick:
    71  			info := c.infoSource.GetClustersInfo()
    72  
    73  			c.lock.Lock()
    74  			c.info = info
    75  			c.lock.Unlock()
    76  		}
    77  	}
    78  }
    79  
    80  // Describe implements the prometheus.Collector interface
    81  func (c *clusterCollector) Describe(ch chan<- *prometheus.Desc) {
    82  	ch <- descClusterInfo
    83  	ch <- descClusterCacheResources
    84  	ch <- descClusterAPIs
    85  	ch <- descClusterCacheAgeSeconds
    86  	ch <- descClusterConnectionStatus
    87  }
    88  
    89  func (c *clusterCollector) Collect(ch chan<- prometheus.Metric) {
    90  
    91  	now := time.Now()
    92  	for _, c := range c.info {
    93  		defaultValues := []string{c.Server}
    94  		ch <- prometheus.MustNewConstMetric(descClusterInfo, prometheus.GaugeValue, 1, append(defaultValues, c.K8SVersion)...)
    95  		ch <- prometheus.MustNewConstMetric(descClusterCacheResources, prometheus.GaugeValue, float64(c.ResourcesCount), defaultValues...)
    96  		ch <- prometheus.MustNewConstMetric(descClusterAPIs, prometheus.GaugeValue, float64(c.APIsCount), defaultValues...)
    97  		cacheAgeSeconds := -1
    98  		if c.LastCacheSyncTime != nil {
    99  			cacheAgeSeconds = int(now.Sub(*c.LastCacheSyncTime).Seconds())
   100  		}
   101  		ch <- prometheus.MustNewConstMetric(descClusterCacheAgeSeconds, prometheus.GaugeValue, float64(cacheAgeSeconds), defaultValues...)
   102  		ch <- prometheus.MustNewConstMetric(descClusterConnectionStatus, prometheus.GaugeValue, boolFloat64(c.SyncError == nil), append(defaultValues, c.K8SVersion)...)
   103  	}
   104  }