github.com/argoproj/argo-cd@v1.8.7/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 ) 45 46 type HasClustersInfo interface { 47 GetClustersInfo() []cache.ClusterInfo 48 } 49 50 type clusterCollector struct { 51 infoSource HasClustersInfo 52 info []cache.ClusterInfo 53 lock sync.Mutex 54 } 55 56 func (c *clusterCollector) Run(ctx context.Context) { 57 // FIXME: complains about SA1015 58 // nolint:staticcheck 59 tick := time.Tick(metricsCollectionInterval) 60 for { 61 select { 62 case <-ctx.Done(): 63 break 64 case <-tick: 65 info := c.infoSource.GetClustersInfo() 66 67 c.lock.Lock() 68 c.info = info 69 c.lock.Unlock() 70 } 71 } 72 } 73 74 // Describe implements the prometheus.Collector interface 75 func (c *clusterCollector) Describe(ch chan<- *prometheus.Desc) { 76 ch <- descClusterInfo 77 ch <- descClusterCacheResources 78 ch <- descClusterAPIs 79 ch <- descClusterCacheAgeSeconds 80 } 81 82 func (c *clusterCollector) Collect(ch chan<- prometheus.Metric) { 83 now := time.Now() 84 for _, c := range c.info { 85 defaultValues := []string{c.Server} 86 ch <- prometheus.MustNewConstMetric(descClusterInfo, prometheus.GaugeValue, 1, append(defaultValues, c.K8SVersion)...) 87 ch <- prometheus.MustNewConstMetric(descClusterCacheResources, prometheus.GaugeValue, float64(c.ResourcesCount), defaultValues...) 88 ch <- prometheus.MustNewConstMetric(descClusterAPIs, prometheus.GaugeValue, float64(c.APIsCount), defaultValues...) 89 cacheAgeSeconds := -1 90 if c.LastCacheSyncTime != nil { 91 cacheAgeSeconds = int(now.Sub(*c.LastCacheSyncTime).Seconds()) 92 } 93 ch <- prometheus.MustNewConstMetric(descClusterCacheAgeSeconds, prometheus.GaugeValue, float64(cacheAgeSeconds), defaultValues...) 94 } 95 }