Providing metrics information to Prometheus

Former-commit-id: 5d84c61c35ecba3aaf7f63f59e6efdf4e2752f99
This commit is contained in:
zhangweiii 2024-03-14 17:04:32 +08:00
commit 6dcb3862d1
10 changed files with 233 additions and 64 deletions

View File

@ -22,7 +22,7 @@ type (
centerResourcesResp {
CentersIndex []CenterIndex `json:"centersIndex"`
}
CenterIndex{
CenterIndex {
name string `json:"name"`
cpu float32 `json:"cpu"`
memory float32 `json:"memory"`
@ -30,6 +30,20 @@ type (
centerType string `json:"centerType"`
}
)
type (
syncClusterLoadReq {
clusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"`
}
ClusterLoadRecord {
ClusterName string `json:"clusterName"`
CpuUsage float64 `json:"cpuUsage"`
MemoryUsage float64 `json:"memoryUsage"`
DiskUsage float64 `json:"diskUsage"`
}
)
type (
getClusterListReq {
Id int64 `form:"id"`
@ -620,7 +634,7 @@ type clusterSumReq {
}
type clusterSumReqResp{
type clusterSumReqResp {
ClusterSum int `json:"ClusterSum,omitempty"`
AdapterSum int `json:"AdapterSum,omitempty"`
TaskSum int `json:"TaskSum,omitempty"`

View File

@ -104,6 +104,14 @@ service pcm {
@doc "Center Resources top3"
@handler centerResourcesHandler
get /core/centerResources returns (centerResourcesResp)
@doc "Synchronize Cluster Load Information"
@handler syncClusterLoadHandler
post /core/syncClusterLoad (syncClusterLoadReq)
@doc "metrics"
@handler metricsHandler
get /core/metrics
}
//hpc二级接口

View File

@ -0,0 +1,12 @@
package core
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
)
func MetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return promhttp.Handler().ServeHTTP
}

View File

@ -0,0 +1,25 @@
package core
import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/core"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
)
func SyncClusterLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SyncClusterLoadReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := core.NewSyncClusterLoadLogic(r.Context(), svcCtx)
err := l.SyncClusterLoad(&req)
result.HttpResult(r, w, nil, err)
}
}

View File

@ -124,6 +124,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/core/centerResources",
Handler: core.CenterResourcesHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/core/syncClusterLoad",
Handler: core.SyncClusterLoadHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/core/metrics",
Handler: core.MetricsHandler(serverCtx),
},
},
rest.WithPrefix("/pcm/v1"),
)

View File

@ -0,0 +1,28 @@
package core
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
)
type MetricsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMetricsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MetricsLogic {
return &MetricsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MetricsLogic) Metrics() error {
// todo: add your logic here and delete this line
return nil
}

View File

@ -0,0 +1,35 @@
package core
import (
"context"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
"github.com/zeromicro/go-zero/core/logx"
)
type SyncClusterLoadLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSyncClusterLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncClusterLoadLogic {
return &SyncClusterLoadLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error {
if len(req.ClusterLoadRecords) != 0 {
for _, record := range req.ClusterLoadRecords {
tracker.ClusterCpuGauge.WithLabelValues(record.ClusterName).Set(record.CpuUsage)
tracker.ClusterMemoryGauge.WithLabelValues(record.ClusterName).Set(record.MemoryUsage)
tracker.ClusterDiskGauge.WithLabelValues(record.ClusterName).Set(record.DiskUsage)
}
}
return nil
}

View File

@ -24,6 +24,17 @@ type CenterIndex struct {
CenterType string `json:"centerType"`
}
type SyncClusterLoadReq struct {
ClusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"`
}
type ClusterLoadRecord struct {
ClusterName string `json:"clusterName"`
CpuUsage float64 `json:"cpuUsage"`
MemoryUsage float64 `json:"memoryUsage"`
DiskUsage float64 `json:"diskUsage"`
}
type GetClusterListReq struct {
Id int64 `form:"id"`
}

View File

@ -254,6 +254,7 @@ type ControllerOption struct {
Namespace string
Kind string
WorkloadName string
PodsName string
Level string
}

View File

@ -18,6 +18,7 @@ import (
"context"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"strconv"
"strings"
@ -25,6 +26,30 @@ import (
"time"
)
var (
ClusterCpuGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_cpu_usage",
Help: "Cluster CPU Utilization Rate.",
}, []string{"cluster_name"})
ClusterMemoryGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_memory_usage",
Help: "Cluster Memory Utilization Rate.",
}, []string{"cluster_name"})
ClusterDiskGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_disk_usage",
Help: "Cluster Disk Utilization Rate.",
}, []string{"cluster_name"})
metrics = []prometheus.Collector{
ClusterCpuGauge,
ClusterMemoryGauge,
ClusterDiskGauge,
}
)
func init() {
prometheus.MustRegister(metrics...)
}
type Prometheus struct {
prometheus Interface
client v1.API