github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/metrics/instances_collector.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/kyma-project/kyma-environment-broker/internal"
     5  	"github.com/prometheus/client_golang/prometheus"
     6  	"github.com/sirupsen/logrus"
     7  )
     8  
     9  // InstancesStatsGetter provides number of all instances failed, succeeded or orphaned
    10  //
    11  //	(instance exists but the cluster was removed manually from the gardener):
    12  //
    13  // - compass_keb_instances_total - total number of all instances
    14  // - compass_keb_global_account_id_instances_total - total number of all instances per global account
    15  // - compass_keb_ers_context_license_type_total - count of instances grouped by license types
    16  type InstancesStatsGetter interface {
    17  	GetInstanceStats() (internal.InstanceStats, error)
    18  	GetERSContextStats() (internal.ERSContextStats, error)
    19  }
    20  
    21  type InstancesCollector struct {
    22  	statsGetter InstancesStatsGetter
    23  
    24  	instancesDesc        *prometheus.Desc
    25  	instancesPerGAIDDesc *prometheus.Desc
    26  	licenseTypeDesc      *prometheus.Desc
    27  }
    28  
    29  func NewInstancesCollector(statsGetter InstancesStatsGetter) *InstancesCollector {
    30  	return &InstancesCollector{
    31  		statsGetter: statsGetter,
    32  
    33  		instancesDesc: prometheus.NewDesc(
    34  			prometheus.BuildFQName(prometheusNamespace, prometheusSubsystem, "instances_total"),
    35  			"The total number of instances",
    36  			[]string{},
    37  			nil),
    38  		instancesPerGAIDDesc: prometheus.NewDesc(
    39  			prometheus.BuildFQName(prometheusNamespace, prometheusSubsystem, "global_account_id_instances_total"),
    40  			"The total number of instances by Global Account ID",
    41  			[]string{"global_account_id"},
    42  			nil),
    43  		licenseTypeDesc: prometheus.NewDesc(
    44  			prometheus.BuildFQName(prometheusNamespace, prometheusSubsystem, "ers_context_license_type_total"),
    45  			"count of instances grouped by license types",
    46  			[]string{"license_type"},
    47  			nil),
    48  	}
    49  }
    50  
    51  func (c *InstancesCollector) Describe(ch chan<- *prometheus.Desc) {
    52  	ch <- c.instancesDesc
    53  	ch <- c.instancesPerGAIDDesc
    54  	ch <- c.licenseTypeDesc
    55  }
    56  
    57  // Collect implements the prometheus.Collector interface.
    58  func (c *InstancesCollector) Collect(ch chan<- prometheus.Metric) {
    59  	stats, err := c.statsGetter.GetInstanceStats()
    60  	if err != nil {
    61  		logrus.Error(err)
    62  	} else {
    63  		collect(ch, c.instancesDesc, stats.TotalNumberOfInstances)
    64  
    65  		for globalAccountID, num := range stats.PerGlobalAccountID {
    66  			collect(ch, c.instancesPerGAIDDesc, num, globalAccountID)
    67  		}
    68  	}
    69  
    70  	stats2, err := c.statsGetter.GetERSContextStats()
    71  	if err != nil {
    72  		logrus.Error(err)
    73  		return
    74  	}
    75  	for t, num := range stats2.LicenseType {
    76  		collect(ch, c.licenseTypeDesc, num, t)
    77  	}
    78  }