package metric
import (
"context"
"fmt"
"github.com/goodrain/rainbond/pkg/gogo"
"time"
"github.com/goodrain/rainbond/api/handler"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "rbd_api"
exporter = "exporter"
)
func NewExporter() *Exporter {
return &Exporter{
apiRequest: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "api_request",
Help: "rainbond cluster api request metric",
}, []string{"code", "path"}),
tenantLimit: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "tenant_memory_limit",
Help: "rainbond tenant memory limit",
}, []string{"tenant_id", "namespace"}),
clusterMemoryTotal: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_memory_total",
Help: "rainbond cluster memory total",
}),
clusterCPUTotal: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_cpu_total",
Help: "rainbond cluster cpu total",
}),
clusterPodsNumber: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_pod_number",
Help: "rainbond cluster pods number",
}),
clusterPodMemory: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_pod_memory",
Help: "rainbond cluster pod memory",
}, []string{"node_name", "app_id", "service_id", "resource_version"}),
clusterPodCPU: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_pod_cpu",
Help: "rainbond cluster pod CPU",
}, []string{"node_name", "app_id", "service_id", "resource_version"}),
clusterPodStorageEphemeral: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "cluster_pod_ephemeral_storage",
Help: "rainbond cluster pod StorageEphemeral",
}, []string{"node_name", "app_id", "service_id", "resource_version"}),
}
}
type Exporter struct {
apiRequest *prometheus.CounterVec
tenantLimit *prometheus.GaugeVec
clusterPodMemory *prometheus.GaugeVec
clusterPodCPU *prometheus.GaugeVec
clusterPodStorageEphemeral *prometheus.GaugeVec
clusterPodsNumber prometheus.Gauge
clusterCPUTotal prometheus.Gauge
clusterMemoryTotal prometheus.Gauge
}
func (e *Exporter) RequestInc(code int, path string) {
e.apiRequest.WithLabelValues(fmt.Sprintf("%d", code), path).Inc()
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
_ = gogo.Go(func(ctx context.Context) error {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
return nil
})
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.apiRequest.Collect(ch)
tenants, _ := handler.GetTenantManager().GetTenants("")
for _, t := range tenants {
e.tenantLimit.WithLabelValues(t.UUID, t.UUID).Set(float64(t.LimitMemory))
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
resource := handler.GetTenantManager().GetClusterResource(ctx)
if resource != nil {
e.clusterMemoryTotal.Set(float64(resource.AllMemory))
e.clusterCPUTotal.Set(float64(resource.AllCPU))
e.clusterPodsNumber.Set(float64(resource.AllPods))
e.clusterPodMemory.Reset()
e.clusterPodCPU.Reset()
e.clusterPodStorageEphemeral.Reset()
for _, pod := range resource.NodePods {
e.clusterPodMemory.WithLabelValues(pod.NodeName, pod.AppID, pod.ServiceID, pod.ResourceVersion).Set(float64(pod.Memory))
e.clusterPodCPU.WithLabelValues(pod.NodeName, pod.AppID, pod.ServiceID, pod.ResourceVersion).Set(float64(pod.CPU))
e.clusterPodStorageEphemeral.WithLabelValues(pod.NodeName, pod.AppID, pod.ServiceID, pod.ResourceVersion).Set(float64(pod.StorageEphemeral))
}
}
e.tenantLimit.Collect(ch)
e.clusterMemoryTotal.Collect(ch)
e.clusterCPUTotal.Collect(ch)
e.clusterPodsNumber.Collect(ch)
e.clusterPodStorageEphemeral.Collect(ch)
e.clusterPodCPU.Collect(ch)
e.clusterPodMemory.Collect(ch)
}